17db96d56Sopenharmony_ci:mod:`pkgutil` --- Package extension utility 27db96d56Sopenharmony_ci============================================ 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: pkgutil 57db96d56Sopenharmony_ci :synopsis: Utilities for the import system. 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci**Source code:** :source:`Lib/pkgutil.py` 87db96d56Sopenharmony_ci 97db96d56Sopenharmony_ci-------------- 107db96d56Sopenharmony_ci 117db96d56Sopenharmony_ciThis module provides utilities for the import system, in particular package 127db96d56Sopenharmony_cisupport. 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ci.. class:: ModuleInfo(module_finder, name, ispkg) 157db96d56Sopenharmony_ci 167db96d56Sopenharmony_ci A namedtuple that holds a brief summary of a module's info. 177db96d56Sopenharmony_ci 187db96d56Sopenharmony_ci .. versionadded:: 3.6 197db96d56Sopenharmony_ci 207db96d56Sopenharmony_ci.. function:: extend_path(path, name) 217db96d56Sopenharmony_ci 227db96d56Sopenharmony_ci Extend the search path for the modules which comprise a package. Intended 237db96d56Sopenharmony_ci use is to place the following code in a package's :file:`__init__.py`:: 247db96d56Sopenharmony_ci 257db96d56Sopenharmony_ci from pkgutil import extend_path 267db96d56Sopenharmony_ci __path__ = extend_path(__path__, __name__) 277db96d56Sopenharmony_ci 287db96d56Sopenharmony_ci For each directory on :data:`sys.path` that has a subdirectory that matches the 297db96d56Sopenharmony_ci package name, add the subdirectory to the package's :attr:`__path__`. This is useful 307db96d56Sopenharmony_ci if one wants to distribute different parts of a single logical package as multiple 317db96d56Sopenharmony_ci directories. 327db96d56Sopenharmony_ci 337db96d56Sopenharmony_ci It also looks for :file:`\*.pkg` files beginning where ``*`` matches the 347db96d56Sopenharmony_ci *name* argument. This feature is similar to :file:`\*.pth` files (see the 357db96d56Sopenharmony_ci :mod:`site` module for more information), except that it doesn't special-case 367db96d56Sopenharmony_ci lines starting with ``import``. A :file:`\*.pkg` file is trusted at face 377db96d56Sopenharmony_ci value: apart from checking for duplicates, all entries found in a 387db96d56Sopenharmony_ci :file:`\*.pkg` file are added to the path, regardless of whether they exist 397db96d56Sopenharmony_ci on the filesystem. (This is a feature.) 407db96d56Sopenharmony_ci 417db96d56Sopenharmony_ci If the input path is not a list (as is the case for frozen packages) it is 427db96d56Sopenharmony_ci returned unchanged. The input path is not modified; an extended copy is 437db96d56Sopenharmony_ci returned. Items are only appended to the copy at the end. 447db96d56Sopenharmony_ci 457db96d56Sopenharmony_ci It is assumed that :data:`sys.path` is a sequence. Items of :data:`sys.path` 467db96d56Sopenharmony_ci that are not strings referring to existing directories are ignored. Unicode 477db96d56Sopenharmony_ci items on :data:`sys.path` that cause errors when used as filenames may cause 487db96d56Sopenharmony_ci this function to raise an exception (in line with :func:`os.path.isdir` 497db96d56Sopenharmony_ci behavior). 507db96d56Sopenharmony_ci 517db96d56Sopenharmony_ci 527db96d56Sopenharmony_ci.. class:: ImpImporter(dirname=None) 537db96d56Sopenharmony_ci 547db96d56Sopenharmony_ci :pep:`302` Finder that wraps Python's "classic" import algorithm. 557db96d56Sopenharmony_ci 567db96d56Sopenharmony_ci If *dirname* is a string, a :pep:`302` finder is created that searches that 577db96d56Sopenharmony_ci directory. If *dirname* is ``None``, a :pep:`302` finder is created that 587db96d56Sopenharmony_ci searches the current :data:`sys.path`, plus any modules that are frozen or 597db96d56Sopenharmony_ci built-in. 607db96d56Sopenharmony_ci 617db96d56Sopenharmony_ci Note that :class:`ImpImporter` does not currently support being used by 627db96d56Sopenharmony_ci placement on :data:`sys.meta_path`. 637db96d56Sopenharmony_ci 647db96d56Sopenharmony_ci .. deprecated:: 3.3 657db96d56Sopenharmony_ci This emulation is no longer needed, as the standard import mechanism 667db96d56Sopenharmony_ci is now fully :pep:`302` compliant and available in :mod:`importlib`. 677db96d56Sopenharmony_ci 687db96d56Sopenharmony_ci 697db96d56Sopenharmony_ci.. class:: ImpLoader(fullname, file, filename, etc) 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ci :term:`Loader <loader>` that wraps Python's "classic" import algorithm. 727db96d56Sopenharmony_ci 737db96d56Sopenharmony_ci .. deprecated:: 3.3 747db96d56Sopenharmony_ci This emulation is no longer needed, as the standard import mechanism 757db96d56Sopenharmony_ci is now fully :pep:`302` compliant and available in :mod:`importlib`. 767db96d56Sopenharmony_ci 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ci.. function:: find_loader(fullname) 797db96d56Sopenharmony_ci 807db96d56Sopenharmony_ci Retrieve a module :term:`loader` for the given *fullname*. 817db96d56Sopenharmony_ci 827db96d56Sopenharmony_ci This is a backwards compatibility wrapper around 837db96d56Sopenharmony_ci :func:`importlib.util.find_spec` that converts most failures to 847db96d56Sopenharmony_ci :exc:`ImportError` and only returns the loader rather than the full 857db96d56Sopenharmony_ci :class:`importlib.machinery.ModuleSpec`. 867db96d56Sopenharmony_ci 877db96d56Sopenharmony_ci .. versionchanged:: 3.3 887db96d56Sopenharmony_ci Updated to be based directly on :mod:`importlib` rather than relying 897db96d56Sopenharmony_ci on the package internal :pep:`302` import emulation. 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ci .. versionchanged:: 3.4 927db96d56Sopenharmony_ci Updated to be based on :pep:`451` 937db96d56Sopenharmony_ci 947db96d56Sopenharmony_ci.. function:: get_importer(path_item) 957db96d56Sopenharmony_ci 967db96d56Sopenharmony_ci Retrieve a :term:`finder` for the given *path_item*. 977db96d56Sopenharmony_ci 987db96d56Sopenharmony_ci The returned finder is cached in :data:`sys.path_importer_cache` if it was 997db96d56Sopenharmony_ci newly created by a path hook. 1007db96d56Sopenharmony_ci 1017db96d56Sopenharmony_ci The cache (or part of it) can be cleared manually if a rescan of 1027db96d56Sopenharmony_ci :data:`sys.path_hooks` is necessary. 1037db96d56Sopenharmony_ci 1047db96d56Sopenharmony_ci .. versionchanged:: 3.3 1057db96d56Sopenharmony_ci Updated to be based directly on :mod:`importlib` rather than relying 1067db96d56Sopenharmony_ci on the package internal :pep:`302` import emulation. 1077db96d56Sopenharmony_ci 1087db96d56Sopenharmony_ci 1097db96d56Sopenharmony_ci.. function:: get_loader(module_or_name) 1107db96d56Sopenharmony_ci 1117db96d56Sopenharmony_ci Get a :term:`loader` object for *module_or_name*. 1127db96d56Sopenharmony_ci 1137db96d56Sopenharmony_ci If the module or package is accessible via the normal import mechanism, a 1147db96d56Sopenharmony_ci wrapper around the relevant part of that machinery is returned. Returns 1157db96d56Sopenharmony_ci ``None`` if the module cannot be found or imported. If the named module is 1167db96d56Sopenharmony_ci not already imported, its containing package (if any) is imported, in order 1177db96d56Sopenharmony_ci to establish the package ``__path__``. 1187db96d56Sopenharmony_ci 1197db96d56Sopenharmony_ci .. versionchanged:: 3.3 1207db96d56Sopenharmony_ci Updated to be based directly on :mod:`importlib` rather than relying 1217db96d56Sopenharmony_ci on the package internal :pep:`302` import emulation. 1227db96d56Sopenharmony_ci 1237db96d56Sopenharmony_ci .. versionchanged:: 3.4 1247db96d56Sopenharmony_ci Updated to be based on :pep:`451` 1257db96d56Sopenharmony_ci 1267db96d56Sopenharmony_ci 1277db96d56Sopenharmony_ci.. function:: iter_importers(fullname='') 1287db96d56Sopenharmony_ci 1297db96d56Sopenharmony_ci Yield :term:`finder` objects for the given module name. 1307db96d56Sopenharmony_ci 1317db96d56Sopenharmony_ci If fullname contains a ``'.'``, the finders will be for the package 1327db96d56Sopenharmony_ci containing fullname, otherwise they will be all registered top level 1337db96d56Sopenharmony_ci finders (i.e. those on both :data:`sys.meta_path` and :data:`sys.path_hooks`). 1347db96d56Sopenharmony_ci 1357db96d56Sopenharmony_ci If the named module is in a package, that package is imported as a side 1367db96d56Sopenharmony_ci effect of invoking this function. 1377db96d56Sopenharmony_ci 1387db96d56Sopenharmony_ci If no module name is specified, all top level finders are produced. 1397db96d56Sopenharmony_ci 1407db96d56Sopenharmony_ci .. versionchanged:: 3.3 1417db96d56Sopenharmony_ci Updated to be based directly on :mod:`importlib` rather than relying 1427db96d56Sopenharmony_ci on the package internal :pep:`302` import emulation. 1437db96d56Sopenharmony_ci 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ci.. function:: iter_modules(path=None, prefix='') 1467db96d56Sopenharmony_ci 1477db96d56Sopenharmony_ci Yields :class:`ModuleInfo` for all submodules on *path*, or, if 1487db96d56Sopenharmony_ci *path* is ``None``, all top-level modules on :data:`sys.path`. 1497db96d56Sopenharmony_ci 1507db96d56Sopenharmony_ci *path* should be either ``None`` or a list of paths to look for modules in. 1517db96d56Sopenharmony_ci 1527db96d56Sopenharmony_ci *prefix* is a string to output on the front of every module name on output. 1537db96d56Sopenharmony_ci 1547db96d56Sopenharmony_ci .. note:: 1557db96d56Sopenharmony_ci 1567db96d56Sopenharmony_ci Only works for a :term:`finder` which defines an ``iter_modules()`` 1577db96d56Sopenharmony_ci method. This interface is non-standard, so the module also provides 1587db96d56Sopenharmony_ci implementations for :class:`importlib.machinery.FileFinder` and 1597db96d56Sopenharmony_ci :class:`zipimport.zipimporter`. 1607db96d56Sopenharmony_ci 1617db96d56Sopenharmony_ci .. versionchanged:: 3.3 1627db96d56Sopenharmony_ci Updated to be based directly on :mod:`importlib` rather than relying 1637db96d56Sopenharmony_ci on the package internal :pep:`302` import emulation. 1647db96d56Sopenharmony_ci 1657db96d56Sopenharmony_ci 1667db96d56Sopenharmony_ci.. function:: walk_packages(path=None, prefix='', onerror=None) 1677db96d56Sopenharmony_ci 1687db96d56Sopenharmony_ci Yields :class:`ModuleInfo` for all modules recursively on 1697db96d56Sopenharmony_ci *path*, or, if *path* is ``None``, all accessible modules. 1707db96d56Sopenharmony_ci 1717db96d56Sopenharmony_ci *path* should be either ``None`` or a list of paths to look for modules in. 1727db96d56Sopenharmony_ci 1737db96d56Sopenharmony_ci *prefix* is a string to output on the front of every module name on output. 1747db96d56Sopenharmony_ci 1757db96d56Sopenharmony_ci Note that this function must import all *packages* (*not* all modules!) on 1767db96d56Sopenharmony_ci the given *path*, in order to access the ``__path__`` attribute to find 1777db96d56Sopenharmony_ci submodules. 1787db96d56Sopenharmony_ci 1797db96d56Sopenharmony_ci *onerror* is a function which gets called with one argument (the name of the 1807db96d56Sopenharmony_ci package which was being imported) if any exception occurs while trying to 1817db96d56Sopenharmony_ci import a package. If no *onerror* function is supplied, :exc:`ImportError`\s 1827db96d56Sopenharmony_ci are caught and ignored, while all other exceptions are propagated, 1837db96d56Sopenharmony_ci terminating the search. 1847db96d56Sopenharmony_ci 1857db96d56Sopenharmony_ci Examples:: 1867db96d56Sopenharmony_ci 1877db96d56Sopenharmony_ci # list all modules python can access 1887db96d56Sopenharmony_ci walk_packages() 1897db96d56Sopenharmony_ci 1907db96d56Sopenharmony_ci # list all submodules of ctypes 1917db96d56Sopenharmony_ci walk_packages(ctypes.__path__, ctypes.__name__ + '.') 1927db96d56Sopenharmony_ci 1937db96d56Sopenharmony_ci .. note:: 1947db96d56Sopenharmony_ci 1957db96d56Sopenharmony_ci Only works for a :term:`finder` which defines an ``iter_modules()`` 1967db96d56Sopenharmony_ci method. This interface is non-standard, so the module also provides 1977db96d56Sopenharmony_ci implementations for :class:`importlib.machinery.FileFinder` and 1987db96d56Sopenharmony_ci :class:`zipimport.zipimporter`. 1997db96d56Sopenharmony_ci 2007db96d56Sopenharmony_ci .. versionchanged:: 3.3 2017db96d56Sopenharmony_ci Updated to be based directly on :mod:`importlib` rather than relying 2027db96d56Sopenharmony_ci on the package internal :pep:`302` import emulation. 2037db96d56Sopenharmony_ci 2047db96d56Sopenharmony_ci 2057db96d56Sopenharmony_ci.. function:: get_data(package, resource) 2067db96d56Sopenharmony_ci 2077db96d56Sopenharmony_ci Get a resource from a package. 2087db96d56Sopenharmony_ci 2097db96d56Sopenharmony_ci This is a wrapper for the :term:`loader` 2107db96d56Sopenharmony_ci :meth:`get_data <importlib.abc.ResourceLoader.get_data>` API. The 2117db96d56Sopenharmony_ci *package* argument should be the name of a package, in standard module format 2127db96d56Sopenharmony_ci (``foo.bar``). The *resource* argument should be in the form of a relative 2137db96d56Sopenharmony_ci filename, using ``/`` as the path separator. The parent directory name 2147db96d56Sopenharmony_ci ``..`` is not allowed, and nor is a rooted name (starting with a ``/``). 2157db96d56Sopenharmony_ci 2167db96d56Sopenharmony_ci The function returns a binary string that is the contents of the specified 2177db96d56Sopenharmony_ci resource. 2187db96d56Sopenharmony_ci 2197db96d56Sopenharmony_ci For packages located in the filesystem, which have already been imported, 2207db96d56Sopenharmony_ci this is the rough equivalent of:: 2217db96d56Sopenharmony_ci 2227db96d56Sopenharmony_ci d = os.path.dirname(sys.modules[package].__file__) 2237db96d56Sopenharmony_ci data = open(os.path.join(d, resource), 'rb').read() 2247db96d56Sopenharmony_ci 2257db96d56Sopenharmony_ci If the package cannot be located or loaded, or it uses a :term:`loader` 2267db96d56Sopenharmony_ci which does not support :meth:`get_data <importlib.abc.ResourceLoader.get_data>`, 2277db96d56Sopenharmony_ci then ``None`` is returned. In particular, the :term:`loader` for 2287db96d56Sopenharmony_ci :term:`namespace packages <namespace package>` does not support 2297db96d56Sopenharmony_ci :meth:`get_data <importlib.abc.ResourceLoader.get_data>`. 2307db96d56Sopenharmony_ci 2317db96d56Sopenharmony_ci 2327db96d56Sopenharmony_ci.. function:: resolve_name(name) 2337db96d56Sopenharmony_ci 2347db96d56Sopenharmony_ci Resolve a name to an object. 2357db96d56Sopenharmony_ci 2367db96d56Sopenharmony_ci This functionality is used in numerous places in the standard library (see 2377db96d56Sopenharmony_ci :issue:`12915`) - and equivalent functionality is also in widely used 2387db96d56Sopenharmony_ci third-party packages such as setuptools, Django and Pyramid. 2397db96d56Sopenharmony_ci 2407db96d56Sopenharmony_ci It is expected that *name* will be a string in one of the following 2417db96d56Sopenharmony_ci formats, where W is shorthand for a valid Python identifier and dot stands 2427db96d56Sopenharmony_ci for a literal period in these pseudo-regexes: 2437db96d56Sopenharmony_ci 2447db96d56Sopenharmony_ci * ``W(.W)*`` 2457db96d56Sopenharmony_ci * ``W(.W)*:(W(.W)*)?`` 2467db96d56Sopenharmony_ci 2477db96d56Sopenharmony_ci The first form is intended for backward compatibility only. It assumes that 2487db96d56Sopenharmony_ci some part of the dotted name is a package, and the rest is an object 2497db96d56Sopenharmony_ci somewhere within that package, possibly nested inside other objects. 2507db96d56Sopenharmony_ci Because the place where the package stops and the object hierarchy starts 2517db96d56Sopenharmony_ci can't be inferred by inspection, repeated attempts to import must be done 2527db96d56Sopenharmony_ci with this form. 2537db96d56Sopenharmony_ci 2547db96d56Sopenharmony_ci In the second form, the caller makes the division point clear through the 2557db96d56Sopenharmony_ci provision of a single colon: the dotted name to the left of the colon is a 2567db96d56Sopenharmony_ci package to be imported, and the dotted name to the right is the object 2577db96d56Sopenharmony_ci hierarchy within that package. Only one import is needed in this form. If 2587db96d56Sopenharmony_ci it ends with the colon, then a module object is returned. 2597db96d56Sopenharmony_ci 2607db96d56Sopenharmony_ci The function will return an object (which might be a module), or raise one 2617db96d56Sopenharmony_ci of the following exceptions: 2627db96d56Sopenharmony_ci 2637db96d56Sopenharmony_ci :exc:`ValueError` -- if *name* isn't in a recognised format. 2647db96d56Sopenharmony_ci 2657db96d56Sopenharmony_ci :exc:`ImportError` -- if an import failed when it shouldn't have. 2667db96d56Sopenharmony_ci 2677db96d56Sopenharmony_ci :exc:`AttributeError` -- If a failure occurred when traversing the object 2687db96d56Sopenharmony_ci hierarchy within the imported package to get to the desired object. 2697db96d56Sopenharmony_ci 2707db96d56Sopenharmony_ci .. versionadded:: 3.9 271