17db96d56Sopenharmony_ci:mod:`imp` --- Access the :ref:`import <importsystem>` internals
27db96d56Sopenharmony_ci================================================================
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: imp
57db96d56Sopenharmony_ci   :synopsis: Access the implementation of the import statement.
67db96d56Sopenharmony_ci   :deprecated:
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci**Source code:** :source:`Lib/imp.py`
97db96d56Sopenharmony_ci
107db96d56Sopenharmony_ci.. deprecated-removed:: 3.4 3.12
117db96d56Sopenharmony_ci   The :mod:`imp` module is deprecated in favor of :mod:`importlib`.
127db96d56Sopenharmony_ci
137db96d56Sopenharmony_ci.. index:: statement: import
147db96d56Sopenharmony_ci
157db96d56Sopenharmony_ci--------------
167db96d56Sopenharmony_ci
177db96d56Sopenharmony_ciThis module provides an interface to the mechanisms used to implement the
187db96d56Sopenharmony_ci:keyword:`import` statement.  It defines the following constants and functions:
197db96d56Sopenharmony_ci
207db96d56Sopenharmony_ci
217db96d56Sopenharmony_ci.. function:: get_magic()
227db96d56Sopenharmony_ci
237db96d56Sopenharmony_ci   .. index:: pair: file; byte-code
247db96d56Sopenharmony_ci
257db96d56Sopenharmony_ci   Return the magic string value used to recognize byte-compiled code files
267db96d56Sopenharmony_ci   (:file:`.pyc` files).  (This value may be different for each Python version.)
277db96d56Sopenharmony_ci
287db96d56Sopenharmony_ci   .. deprecated:: 3.4
297db96d56Sopenharmony_ci       Use :attr:`importlib.util.MAGIC_NUMBER` instead.
307db96d56Sopenharmony_ci
317db96d56Sopenharmony_ci
327db96d56Sopenharmony_ci.. function:: get_suffixes()
337db96d56Sopenharmony_ci
347db96d56Sopenharmony_ci   Return a list of 3-element tuples, each describing a particular type of
357db96d56Sopenharmony_ci   module. Each triple has the form ``(suffix, mode, type)``, where *suffix* is
367db96d56Sopenharmony_ci   a string to be appended to the module name to form the filename to search
377db96d56Sopenharmony_ci   for, *mode* is the mode string to pass to the built-in :func:`open` function
387db96d56Sopenharmony_ci   to open the file (this can be ``'r'`` for text files or ``'rb'`` for binary
397db96d56Sopenharmony_ci   files), and *type* is the file type, which has one of the values
407db96d56Sopenharmony_ci   :const:`PY_SOURCE`, :const:`PY_COMPILED`, or :const:`C_EXTENSION`, described
417db96d56Sopenharmony_ci   below.
427db96d56Sopenharmony_ci
437db96d56Sopenharmony_ci   .. deprecated:: 3.3
447db96d56Sopenharmony_ci      Use the constants defined on :mod:`importlib.machinery` instead.
457db96d56Sopenharmony_ci
467db96d56Sopenharmony_ci
477db96d56Sopenharmony_ci.. function:: find_module(name[, path])
487db96d56Sopenharmony_ci
497db96d56Sopenharmony_ci   Try to find the module *name*.  If *path* is omitted or ``None``, the list of
507db96d56Sopenharmony_ci   directory names given by ``sys.path`` is searched, but first a few special
517db96d56Sopenharmony_ci   places are searched: the function tries to find a built-in module with the
527db96d56Sopenharmony_ci   given name (:const:`C_BUILTIN`), then a frozen module (:const:`PY_FROZEN`),
537db96d56Sopenharmony_ci   and on some systems some other places are looked in as well (on Windows, it
547db96d56Sopenharmony_ci   looks in the registry which may point to a specific file).
557db96d56Sopenharmony_ci
567db96d56Sopenharmony_ci   Otherwise, *path* must be a list of directory names; each directory is
577db96d56Sopenharmony_ci   searched for files with any of the suffixes returned by :func:`get_suffixes`
587db96d56Sopenharmony_ci   above.  Invalid names in the list are silently ignored (but all list items
597db96d56Sopenharmony_ci   must be strings).
607db96d56Sopenharmony_ci
617db96d56Sopenharmony_ci   If search is successful, the return value is a 3-element tuple ``(file,
627db96d56Sopenharmony_ci   pathname, description)``:
637db96d56Sopenharmony_ci
647db96d56Sopenharmony_ci   *file* is an open :term:`file object` positioned at the beginning, *pathname*
657db96d56Sopenharmony_ci   is the pathname of the file found, and *description* is a 3-element tuple as
667db96d56Sopenharmony_ci   contained in the list returned by :func:`get_suffixes` describing the kind of
677db96d56Sopenharmony_ci   module found.
687db96d56Sopenharmony_ci
697db96d56Sopenharmony_ci   If the module is built-in or frozen then *file* and *pathname* are both ``None``
707db96d56Sopenharmony_ci   and the *description* tuple contains empty strings for its suffix and mode;
717db96d56Sopenharmony_ci   the module type is indicated as given in parentheses above.  If the search
727db96d56Sopenharmony_ci   is unsuccessful, :exc:`ImportError` is raised.  Other exceptions indicate
737db96d56Sopenharmony_ci   problems with the arguments or environment.
747db96d56Sopenharmony_ci
757db96d56Sopenharmony_ci   If the module is a package, *file* is ``None``, *pathname* is the package
767db96d56Sopenharmony_ci   path and the last item in the *description* tuple is :const:`PKG_DIRECTORY`.
777db96d56Sopenharmony_ci
787db96d56Sopenharmony_ci   This function does not handle hierarchical module names (names containing
797db96d56Sopenharmony_ci   dots).  In order to find *P.M*, that is, submodule *M* of package *P*, use
807db96d56Sopenharmony_ci   :func:`find_module` and :func:`load_module` to find and load package *P*, and
817db96d56Sopenharmony_ci   then use :func:`find_module` with the *path* argument set to ``P.__path__``.
827db96d56Sopenharmony_ci   When *P* itself has a dotted name, apply this recipe recursively.
837db96d56Sopenharmony_ci
847db96d56Sopenharmony_ci   .. deprecated:: 3.3
857db96d56Sopenharmony_ci      Use :func:`importlib.util.find_spec` instead unless Python 3.3
867db96d56Sopenharmony_ci      compatibility is required, in which case use
877db96d56Sopenharmony_ci      :func:`importlib.find_loader`. For example usage of the former case,
887db96d56Sopenharmony_ci      see the :ref:`importlib-examples` section of the :mod:`importlib`
897db96d56Sopenharmony_ci      documentation.
907db96d56Sopenharmony_ci
917db96d56Sopenharmony_ci
927db96d56Sopenharmony_ci.. function:: load_module(name, file, pathname, description)
937db96d56Sopenharmony_ci
947db96d56Sopenharmony_ci   Load a module that was previously found by :func:`find_module` (or by an
957db96d56Sopenharmony_ci   otherwise conducted search yielding compatible results).  This function does
967db96d56Sopenharmony_ci   more than importing the module: if the module was already imported, it will
977db96d56Sopenharmony_ci   reload the module!  The *name* argument indicates the full
987db96d56Sopenharmony_ci   module name (including the package name, if this is a submodule of a
997db96d56Sopenharmony_ci   package).  The *file* argument is an open file, and *pathname* is the
1007db96d56Sopenharmony_ci   corresponding file name; these can be ``None`` and ``''``, respectively, when
1017db96d56Sopenharmony_ci   the module is a package or not being loaded from a file.  The *description*
1027db96d56Sopenharmony_ci   argument is a tuple, as would be returned by :func:`get_suffixes`, describing
1037db96d56Sopenharmony_ci   what kind of module must be loaded.
1047db96d56Sopenharmony_ci
1057db96d56Sopenharmony_ci   If the load is successful, the return value is the module object; otherwise,
1067db96d56Sopenharmony_ci   an exception (usually :exc:`ImportError`) is raised.
1077db96d56Sopenharmony_ci
1087db96d56Sopenharmony_ci   **Important:** the caller is responsible for closing the *file* argument, if
1097db96d56Sopenharmony_ci   it was not ``None``, even when an exception is raised.  This is best done
1107db96d56Sopenharmony_ci   using a :keyword:`try` ... :keyword:`finally` statement.
1117db96d56Sopenharmony_ci
1127db96d56Sopenharmony_ci   .. deprecated:: 3.3
1137db96d56Sopenharmony_ci      If previously used in conjunction with :func:`imp.find_module` then
1147db96d56Sopenharmony_ci      consider using :func:`importlib.import_module`, otherwise use the loader
1157db96d56Sopenharmony_ci      returned by the replacement you chose for :func:`imp.find_module`. If you
1167db96d56Sopenharmony_ci      called :func:`imp.load_module` and related functions directly with file
1177db96d56Sopenharmony_ci      path arguments then use a combination of
1187db96d56Sopenharmony_ci      :func:`importlib.util.spec_from_file_location` and
1197db96d56Sopenharmony_ci      :func:`importlib.util.module_from_spec`. See the :ref:`importlib-examples`
1207db96d56Sopenharmony_ci      section of the :mod:`importlib` documentation for details of the various
1217db96d56Sopenharmony_ci      approaches.
1227db96d56Sopenharmony_ci
1237db96d56Sopenharmony_ci
1247db96d56Sopenharmony_ci.. function:: new_module(name)
1257db96d56Sopenharmony_ci
1267db96d56Sopenharmony_ci   Return a new empty module object called *name*.  This object is *not* inserted
1277db96d56Sopenharmony_ci   in ``sys.modules``.
1287db96d56Sopenharmony_ci
1297db96d56Sopenharmony_ci   .. deprecated:: 3.4
1307db96d56Sopenharmony_ci      Use :func:`importlib.util.module_from_spec` instead.
1317db96d56Sopenharmony_ci
1327db96d56Sopenharmony_ci
1337db96d56Sopenharmony_ci.. function:: reload(module)
1347db96d56Sopenharmony_ci
1357db96d56Sopenharmony_ci   Reload a previously imported *module*.  The argument must be a module object, so
1367db96d56Sopenharmony_ci   it must have been successfully imported before.  This is useful if you have
1377db96d56Sopenharmony_ci   edited the module source file using an external editor and want to try out the
1387db96d56Sopenharmony_ci   new version without leaving the Python interpreter.  The return value is the
1397db96d56Sopenharmony_ci   module object (the same as the *module* argument).
1407db96d56Sopenharmony_ci
1417db96d56Sopenharmony_ci   When ``reload(module)`` is executed:
1427db96d56Sopenharmony_ci
1437db96d56Sopenharmony_ci   * Python modules' code is recompiled and the module-level code reexecuted,
1447db96d56Sopenharmony_ci     defining a new set of objects which are bound to names in the module's
1457db96d56Sopenharmony_ci     dictionary.  The ``init`` function of extension modules is not called a second
1467db96d56Sopenharmony_ci     time.
1477db96d56Sopenharmony_ci
1487db96d56Sopenharmony_ci   * As with all other objects in Python the old objects are only reclaimed after
1497db96d56Sopenharmony_ci     their reference counts drop to zero.
1507db96d56Sopenharmony_ci
1517db96d56Sopenharmony_ci   * The names in the module namespace are updated to point to any new or changed
1527db96d56Sopenharmony_ci     objects.
1537db96d56Sopenharmony_ci
1547db96d56Sopenharmony_ci   * Other references to the old objects (such as names external to the module) are
1557db96d56Sopenharmony_ci     not rebound to refer to the new objects and must be updated in each namespace
1567db96d56Sopenharmony_ci     where they occur if that is desired.
1577db96d56Sopenharmony_ci
1587db96d56Sopenharmony_ci   There are a number of other caveats:
1597db96d56Sopenharmony_ci
1607db96d56Sopenharmony_ci   When a module is reloaded, its dictionary (containing the module's global
1617db96d56Sopenharmony_ci   variables) is retained.  Redefinitions of names will override the old
1627db96d56Sopenharmony_ci   definitions, so this is generally not a problem.  If the new version of a module
1637db96d56Sopenharmony_ci   does not define a name that was defined by the old version, the old definition
1647db96d56Sopenharmony_ci   remains.  This feature can be used to the module's advantage if it maintains a
1657db96d56Sopenharmony_ci   global table or cache of objects --- with a :keyword:`try` statement it can test
1667db96d56Sopenharmony_ci   for the table's presence and skip its initialization if desired::
1677db96d56Sopenharmony_ci
1687db96d56Sopenharmony_ci      try:
1697db96d56Sopenharmony_ci          cache
1707db96d56Sopenharmony_ci      except NameError:
1717db96d56Sopenharmony_ci          cache = {}
1727db96d56Sopenharmony_ci
1737db96d56Sopenharmony_ci   It is legal though generally not very useful to reload built-in or dynamically
1747db96d56Sopenharmony_ci   loaded modules, except for :mod:`sys`, :mod:`__main__` and :mod:`builtins`.
1757db96d56Sopenharmony_ci   In many cases, however, extension modules are not designed to be initialized
1767db96d56Sopenharmony_ci   more than once, and may fail in arbitrary ways when reloaded.
1777db96d56Sopenharmony_ci
1787db96d56Sopenharmony_ci   If a module imports objects from another module using :keyword:`from` ...
1797db96d56Sopenharmony_ci   :keyword:`import` ..., calling :func:`reload` for the other module does not
1807db96d56Sopenharmony_ci   redefine the objects imported from it --- one way around this is to re-execute
1817db96d56Sopenharmony_ci   the :keyword:`!from` statement, another is to use :keyword:`!import` and qualified
1827db96d56Sopenharmony_ci   names (*module*.*name*) instead.
1837db96d56Sopenharmony_ci
1847db96d56Sopenharmony_ci   If a module instantiates instances of a class, reloading the module that defines
1857db96d56Sopenharmony_ci   the class does not affect the method definitions of the instances --- they
1867db96d56Sopenharmony_ci   continue to use the old class definition.  The same is true for derived classes.
1877db96d56Sopenharmony_ci
1887db96d56Sopenharmony_ci   .. versionchanged:: 3.3
1897db96d56Sopenharmony_ci      Relies on both ``__name__`` and ``__loader__`` being defined on the module
1907db96d56Sopenharmony_ci      being reloaded instead of just ``__name__``.
1917db96d56Sopenharmony_ci
1927db96d56Sopenharmony_ci   .. deprecated:: 3.4
1937db96d56Sopenharmony_ci      Use :func:`importlib.reload` instead.
1947db96d56Sopenharmony_ci
1957db96d56Sopenharmony_ci
1967db96d56Sopenharmony_ciThe following functions are conveniences for handling :pep:`3147` byte-compiled
1977db96d56Sopenharmony_cifile paths.
1987db96d56Sopenharmony_ci
1997db96d56Sopenharmony_ci.. versionadded:: 3.2
2007db96d56Sopenharmony_ci
2017db96d56Sopenharmony_ci.. function:: cache_from_source(path, debug_override=None)
2027db96d56Sopenharmony_ci
2037db96d56Sopenharmony_ci   Return the :pep:`3147` path to the byte-compiled file associated with the
2047db96d56Sopenharmony_ci   source *path*.  For example, if *path* is ``/foo/bar/baz.py`` the return
2057db96d56Sopenharmony_ci   value would be ``/foo/bar/__pycache__/baz.cpython-32.pyc`` for Python 3.2.
2067db96d56Sopenharmony_ci   The ``cpython-32`` string comes from the current magic tag (see
2077db96d56Sopenharmony_ci   :func:`get_tag`; if :attr:`sys.implementation.cache_tag` is not defined then
2087db96d56Sopenharmony_ci   :exc:`NotImplementedError` will be raised). By passing in ``True`` or
2097db96d56Sopenharmony_ci   ``False`` for *debug_override* you can override the system's value for
2107db96d56Sopenharmony_ci   ``__debug__``, leading to optimized bytecode.
2117db96d56Sopenharmony_ci
2127db96d56Sopenharmony_ci   *path* need not exist.
2137db96d56Sopenharmony_ci
2147db96d56Sopenharmony_ci   .. versionchanged:: 3.3
2157db96d56Sopenharmony_ci      If :attr:`sys.implementation.cache_tag` is ``None``, then
2167db96d56Sopenharmony_ci      :exc:`NotImplementedError` is raised.
2177db96d56Sopenharmony_ci
2187db96d56Sopenharmony_ci   .. deprecated:: 3.4
2197db96d56Sopenharmony_ci      Use :func:`importlib.util.cache_from_source` instead.
2207db96d56Sopenharmony_ci
2217db96d56Sopenharmony_ci   .. versionchanged:: 3.5
2227db96d56Sopenharmony_ci      The *debug_override* parameter no longer creates a ``.pyo`` file.
2237db96d56Sopenharmony_ci
2247db96d56Sopenharmony_ci
2257db96d56Sopenharmony_ci.. function:: source_from_cache(path)
2267db96d56Sopenharmony_ci
2277db96d56Sopenharmony_ci   Given the *path* to a :pep:`3147` file name, return the associated source code
2287db96d56Sopenharmony_ci   file path.  For example, if *path* is
2297db96d56Sopenharmony_ci   ``/foo/bar/__pycache__/baz.cpython-32.pyc`` the returned path would be
2307db96d56Sopenharmony_ci   ``/foo/bar/baz.py``.  *path* need not exist, however if it does not conform
2317db96d56Sopenharmony_ci   to :pep:`3147` format, a :exc:`ValueError` is raised. If
2327db96d56Sopenharmony_ci   :attr:`sys.implementation.cache_tag` is not defined,
2337db96d56Sopenharmony_ci   :exc:`NotImplementedError` is raised.
2347db96d56Sopenharmony_ci
2357db96d56Sopenharmony_ci   .. versionchanged:: 3.3
2367db96d56Sopenharmony_ci      Raise :exc:`NotImplementedError` when
2377db96d56Sopenharmony_ci      :attr:`sys.implementation.cache_tag` is not defined.
2387db96d56Sopenharmony_ci
2397db96d56Sopenharmony_ci   .. deprecated:: 3.4
2407db96d56Sopenharmony_ci      Use :func:`importlib.util.source_from_cache` instead.
2417db96d56Sopenharmony_ci
2427db96d56Sopenharmony_ci
2437db96d56Sopenharmony_ci.. function:: get_tag()
2447db96d56Sopenharmony_ci
2457db96d56Sopenharmony_ci   Return the :pep:`3147` magic tag string matching this version of Python's
2467db96d56Sopenharmony_ci   magic number, as returned by :func:`get_magic`.
2477db96d56Sopenharmony_ci
2487db96d56Sopenharmony_ci   .. deprecated:: 3.4
2497db96d56Sopenharmony_ci      Use :attr:`sys.implementation.cache_tag` directly starting
2507db96d56Sopenharmony_ci      in Python 3.3.
2517db96d56Sopenharmony_ci
2527db96d56Sopenharmony_ci
2537db96d56Sopenharmony_ciThe following functions help interact with the import system's internal
2547db96d56Sopenharmony_cilocking mechanism.  Locking semantics of imports are an implementation
2557db96d56Sopenharmony_cidetail which may vary from release to release.  However, Python ensures
2567db96d56Sopenharmony_cithat circular imports work without any deadlocks.
2577db96d56Sopenharmony_ci
2587db96d56Sopenharmony_ci
2597db96d56Sopenharmony_ci.. function:: lock_held()
2607db96d56Sopenharmony_ci
2617db96d56Sopenharmony_ci   Return ``True`` if the global import lock is currently held, else
2627db96d56Sopenharmony_ci   ``False``. On platforms without threads, always return ``False``.
2637db96d56Sopenharmony_ci
2647db96d56Sopenharmony_ci   On platforms with threads, a thread executing an import first holds a
2657db96d56Sopenharmony_ci   global import lock, then sets up a per-module lock for the rest of the
2667db96d56Sopenharmony_ci   import.  This blocks other threads from importing the same module until
2677db96d56Sopenharmony_ci   the original import completes, preventing other threads from seeing
2687db96d56Sopenharmony_ci   incomplete module objects constructed by the original thread.  An
2697db96d56Sopenharmony_ci   exception is made for circular imports, which by construction have to
2707db96d56Sopenharmony_ci   expose an incomplete module object at some point.
2717db96d56Sopenharmony_ci
2727db96d56Sopenharmony_ci   .. versionchanged:: 3.3
2737db96d56Sopenharmony_ci      The locking scheme has changed to per-module locks for
2747db96d56Sopenharmony_ci      the most part.  A global import lock is kept for some critical tasks,
2757db96d56Sopenharmony_ci      such as initializing the per-module locks.
2767db96d56Sopenharmony_ci
2777db96d56Sopenharmony_ci   .. deprecated:: 3.4
2787db96d56Sopenharmony_ci
2797db96d56Sopenharmony_ci
2807db96d56Sopenharmony_ci.. function:: acquire_lock()
2817db96d56Sopenharmony_ci
2827db96d56Sopenharmony_ci   Acquire the interpreter's global import lock for the current thread.
2837db96d56Sopenharmony_ci   This lock should be used by import hooks to ensure thread-safety when
2847db96d56Sopenharmony_ci   importing modules.
2857db96d56Sopenharmony_ci
2867db96d56Sopenharmony_ci   Once a thread has acquired the import lock, the same thread may acquire it
2877db96d56Sopenharmony_ci   again without blocking; the thread must release it once for each time it has
2887db96d56Sopenharmony_ci   acquired it.
2897db96d56Sopenharmony_ci
2907db96d56Sopenharmony_ci   On platforms without threads, this function does nothing.
2917db96d56Sopenharmony_ci
2927db96d56Sopenharmony_ci   .. versionchanged:: 3.3
2937db96d56Sopenharmony_ci      The locking scheme has changed to per-module locks for
2947db96d56Sopenharmony_ci      the most part.  A global import lock is kept for some critical tasks,
2957db96d56Sopenharmony_ci      such as initializing the per-module locks.
2967db96d56Sopenharmony_ci
2977db96d56Sopenharmony_ci   .. deprecated:: 3.4
2987db96d56Sopenharmony_ci
2997db96d56Sopenharmony_ci
3007db96d56Sopenharmony_ci.. function:: release_lock()
3017db96d56Sopenharmony_ci
3027db96d56Sopenharmony_ci   Release the interpreter's global import lock. On platforms without
3037db96d56Sopenharmony_ci   threads, this function does nothing.
3047db96d56Sopenharmony_ci
3057db96d56Sopenharmony_ci   .. versionchanged:: 3.3
3067db96d56Sopenharmony_ci      The locking scheme has changed to per-module locks for
3077db96d56Sopenharmony_ci      the most part.  A global import lock is kept for some critical tasks,
3087db96d56Sopenharmony_ci      such as initializing the per-module locks.
3097db96d56Sopenharmony_ci
3107db96d56Sopenharmony_ci   .. deprecated:: 3.4
3117db96d56Sopenharmony_ci
3127db96d56Sopenharmony_ci
3137db96d56Sopenharmony_ciThe following constants with integer values, defined in this module, are used
3147db96d56Sopenharmony_cito indicate the search result of :func:`find_module`.
3157db96d56Sopenharmony_ci
3167db96d56Sopenharmony_ci
3177db96d56Sopenharmony_ci.. data:: PY_SOURCE
3187db96d56Sopenharmony_ci
3197db96d56Sopenharmony_ci   The module was found as a source file.
3207db96d56Sopenharmony_ci
3217db96d56Sopenharmony_ci   .. deprecated:: 3.3
3227db96d56Sopenharmony_ci
3237db96d56Sopenharmony_ci
3247db96d56Sopenharmony_ci.. data:: PY_COMPILED
3257db96d56Sopenharmony_ci
3267db96d56Sopenharmony_ci   The module was found as a compiled code object file.
3277db96d56Sopenharmony_ci
3287db96d56Sopenharmony_ci   .. deprecated:: 3.3
3297db96d56Sopenharmony_ci
3307db96d56Sopenharmony_ci
3317db96d56Sopenharmony_ci.. data:: C_EXTENSION
3327db96d56Sopenharmony_ci
3337db96d56Sopenharmony_ci   The module was found as dynamically loadable shared library.
3347db96d56Sopenharmony_ci
3357db96d56Sopenharmony_ci   .. deprecated:: 3.3
3367db96d56Sopenharmony_ci
3377db96d56Sopenharmony_ci
3387db96d56Sopenharmony_ci.. data:: PKG_DIRECTORY
3397db96d56Sopenharmony_ci
3407db96d56Sopenharmony_ci   The module was found as a package directory.
3417db96d56Sopenharmony_ci
3427db96d56Sopenharmony_ci   .. deprecated:: 3.3
3437db96d56Sopenharmony_ci
3447db96d56Sopenharmony_ci
3457db96d56Sopenharmony_ci.. data:: C_BUILTIN
3467db96d56Sopenharmony_ci
3477db96d56Sopenharmony_ci   The module was found as a built-in module.
3487db96d56Sopenharmony_ci
3497db96d56Sopenharmony_ci   .. deprecated:: 3.3
3507db96d56Sopenharmony_ci
3517db96d56Sopenharmony_ci
3527db96d56Sopenharmony_ci.. data:: PY_FROZEN
3537db96d56Sopenharmony_ci
3547db96d56Sopenharmony_ci   The module was found as a frozen module.
3557db96d56Sopenharmony_ci
3567db96d56Sopenharmony_ci   .. deprecated:: 3.3
3577db96d56Sopenharmony_ci
3587db96d56Sopenharmony_ci
3597db96d56Sopenharmony_ci.. class:: NullImporter(path_string)
3607db96d56Sopenharmony_ci
3617db96d56Sopenharmony_ci   The :class:`NullImporter` type is a :pep:`302` import hook that handles
3627db96d56Sopenharmony_ci   non-directory path strings by failing to find any modules.  Calling this type
3637db96d56Sopenharmony_ci   with an existing directory or empty string raises :exc:`ImportError`.
3647db96d56Sopenharmony_ci   Otherwise, a :class:`NullImporter` instance is returned.
3657db96d56Sopenharmony_ci
3667db96d56Sopenharmony_ci   Instances have only one method:
3677db96d56Sopenharmony_ci
3687db96d56Sopenharmony_ci   .. method:: NullImporter.find_module(fullname [, path])
3697db96d56Sopenharmony_ci
3707db96d56Sopenharmony_ci      This method always returns ``None``, indicating that the requested module could
3717db96d56Sopenharmony_ci      not be found.
3727db96d56Sopenharmony_ci
3737db96d56Sopenharmony_ci   .. versionchanged:: 3.3
3747db96d56Sopenharmony_ci      ``None`` is inserted into ``sys.path_importer_cache`` instead of an
3757db96d56Sopenharmony_ci      instance of :class:`NullImporter`.
3767db96d56Sopenharmony_ci
3777db96d56Sopenharmony_ci   .. deprecated:: 3.4
3787db96d56Sopenharmony_ci      Insert ``None`` into ``sys.path_importer_cache`` instead.
3797db96d56Sopenharmony_ci
3807db96d56Sopenharmony_ci
3817db96d56Sopenharmony_ci.. _examples-imp:
3827db96d56Sopenharmony_ci
3837db96d56Sopenharmony_ciExamples
3847db96d56Sopenharmony_ci--------
3857db96d56Sopenharmony_ci
3867db96d56Sopenharmony_ciThe following function emulates what was the standard import statement up to
3877db96d56Sopenharmony_ciPython 1.4 (no hierarchical module names).  (This *implementation* wouldn't work
3887db96d56Sopenharmony_ciin that version, since :func:`find_module` has been extended and
3897db96d56Sopenharmony_ci:func:`load_module` has been added in 1.4.) ::
3907db96d56Sopenharmony_ci
3917db96d56Sopenharmony_ci   import imp
3927db96d56Sopenharmony_ci   import sys
3937db96d56Sopenharmony_ci
3947db96d56Sopenharmony_ci   def __import__(name, globals=None, locals=None, fromlist=None):
3957db96d56Sopenharmony_ci       # Fast path: see if the module has already been imported.
3967db96d56Sopenharmony_ci       try:
3977db96d56Sopenharmony_ci           return sys.modules[name]
3987db96d56Sopenharmony_ci       except KeyError:
3997db96d56Sopenharmony_ci           pass
4007db96d56Sopenharmony_ci
4017db96d56Sopenharmony_ci       # If any of the following calls raises an exception,
4027db96d56Sopenharmony_ci       # there's a problem we can't handle -- let the caller handle it.
4037db96d56Sopenharmony_ci
4047db96d56Sopenharmony_ci       fp, pathname, description = imp.find_module(name)
4057db96d56Sopenharmony_ci
4067db96d56Sopenharmony_ci       try:
4077db96d56Sopenharmony_ci           return imp.load_module(name, fp, pathname, description)
4087db96d56Sopenharmony_ci       finally:
4097db96d56Sopenharmony_ci           # Since we may exit via an exception, close fp explicitly.
4107db96d56Sopenharmony_ci           if fp:
4117db96d56Sopenharmony_ci               fp.close()
412