17db96d56Sopenharmony_ci.. highlight:: c
27db96d56Sopenharmony_ci
37db96d56Sopenharmony_ci.. _importing:
47db96d56Sopenharmony_ci
57db96d56Sopenharmony_ciImporting Modules
67db96d56Sopenharmony_ci=================
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci
97db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_ImportModule(const char *name)
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ci   .. index::
127db96d56Sopenharmony_ci      single: package variable; __all__
137db96d56Sopenharmony_ci      single: __all__ (package variable)
147db96d56Sopenharmony_ci      single: modules (in module sys)
157db96d56Sopenharmony_ci
167db96d56Sopenharmony_ci   This is a simplified interface to :c:func:`PyImport_ImportModuleEx` below,
177db96d56Sopenharmony_ci   leaving the *globals* and *locals* arguments set to ``NULL`` and *level* set
187db96d56Sopenharmony_ci   to 0.  When the *name*
197db96d56Sopenharmony_ci   argument contains a dot (when it specifies a submodule of a package), the
207db96d56Sopenharmony_ci   *fromlist* argument is set to the list ``['*']`` so that the return value is the
217db96d56Sopenharmony_ci   named module rather than the top-level package containing it as would otherwise
227db96d56Sopenharmony_ci   be the case.  (Unfortunately, this has an additional side effect when *name* in
237db96d56Sopenharmony_ci   fact specifies a subpackage instead of a submodule: the submodules specified in
247db96d56Sopenharmony_ci   the package's ``__all__`` variable are  loaded.)  Return a new reference to the
257db96d56Sopenharmony_ci   imported module, or ``NULL`` with an exception set on failure.  A failing
267db96d56Sopenharmony_ci   import of a module doesn't leave the module in :data:`sys.modules`.
277db96d56Sopenharmony_ci
287db96d56Sopenharmony_ci   This function always uses absolute imports.
297db96d56Sopenharmony_ci
307db96d56Sopenharmony_ci
317db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_ImportModuleNoBlock(const char *name)
327db96d56Sopenharmony_ci
337db96d56Sopenharmony_ci   This function is a deprecated alias of :c:func:`PyImport_ImportModule`.
347db96d56Sopenharmony_ci
357db96d56Sopenharmony_ci   .. versionchanged:: 3.3
367db96d56Sopenharmony_ci      This function used to fail immediately when the import lock was held
377db96d56Sopenharmony_ci      by another thread.  In Python 3.3 though, the locking scheme switched
387db96d56Sopenharmony_ci      to per-module locks for most purposes, so this function's special
397db96d56Sopenharmony_ci      behaviour isn't needed anymore.
407db96d56Sopenharmony_ci
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_ImportModuleEx(const char *name, PyObject *globals, PyObject *locals, PyObject *fromlist)
437db96d56Sopenharmony_ci
447db96d56Sopenharmony_ci   .. index:: pair: built-in function; __import__
457db96d56Sopenharmony_ci
467db96d56Sopenharmony_ci   Import a module.  This is best described by referring to the built-in Python
477db96d56Sopenharmony_ci   function :func:`__import__`.
487db96d56Sopenharmony_ci
497db96d56Sopenharmony_ci   The return value is a new reference to the imported module or top-level
507db96d56Sopenharmony_ci   package, or ``NULL`` with an exception set on failure.  Like for
517db96d56Sopenharmony_ci   :func:`__import__`, the return value when a submodule of a package was
527db96d56Sopenharmony_ci   requested is normally the top-level package, unless a non-empty *fromlist*
537db96d56Sopenharmony_ci   was given.
547db96d56Sopenharmony_ci
557db96d56Sopenharmony_ci   Failing imports remove incomplete module objects, like with
567db96d56Sopenharmony_ci   :c:func:`PyImport_ImportModule`.
577db96d56Sopenharmony_ci
587db96d56Sopenharmony_ci
597db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
607db96d56Sopenharmony_ci
617db96d56Sopenharmony_ci   Import a module.  This is best described by referring to the built-in Python
627db96d56Sopenharmony_ci   function :func:`__import__`, as the standard :func:`__import__` function calls
637db96d56Sopenharmony_ci   this function directly.
647db96d56Sopenharmony_ci
657db96d56Sopenharmony_ci   The return value is a new reference to the imported module or top-level package,
667db96d56Sopenharmony_ci   or ``NULL`` with an exception set on failure.  Like for :func:`__import__`,
677db96d56Sopenharmony_ci   the return value when a submodule of a package was requested is normally the
687db96d56Sopenharmony_ci   top-level package, unless a non-empty *fromlist* was given.
697db96d56Sopenharmony_ci
707db96d56Sopenharmony_ci   .. versionadded:: 3.3
717db96d56Sopenharmony_ci
727db96d56Sopenharmony_ci
737db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
747db96d56Sopenharmony_ci
757db96d56Sopenharmony_ci   Similar to :c:func:`PyImport_ImportModuleLevelObject`, but the name is a
767db96d56Sopenharmony_ci   UTF-8 encoded string instead of a Unicode object.
777db96d56Sopenharmony_ci
787db96d56Sopenharmony_ci   .. versionchanged:: 3.3
797db96d56Sopenharmony_ci         Negative values for *level* are no longer accepted.
807db96d56Sopenharmony_ci
817db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_Import(PyObject *name)
827db96d56Sopenharmony_ci
837db96d56Sopenharmony_ci   This is a higher-level interface that calls the current "import hook
847db96d56Sopenharmony_ci   function" (with an explicit *level* of 0, meaning absolute import).  It
857db96d56Sopenharmony_ci   invokes the :func:`__import__` function from the ``__builtins__`` of the
867db96d56Sopenharmony_ci   current globals.  This means that the import is done using whatever import
877db96d56Sopenharmony_ci   hooks are installed in the current environment.
887db96d56Sopenharmony_ci
897db96d56Sopenharmony_ci   This function always uses absolute imports.
907db96d56Sopenharmony_ci
917db96d56Sopenharmony_ci
927db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_ReloadModule(PyObject *m)
937db96d56Sopenharmony_ci
947db96d56Sopenharmony_ci   Reload a module.  Return a new reference to the reloaded module, or ``NULL`` with
957db96d56Sopenharmony_ci   an exception set on failure (the module still exists in this case).
967db96d56Sopenharmony_ci
977db96d56Sopenharmony_ci
987db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_AddModuleObject(PyObject *name)
997db96d56Sopenharmony_ci
1007db96d56Sopenharmony_ci   Return the module object corresponding to a module name.  The *name* argument
1017db96d56Sopenharmony_ci   may be of the form ``package.module``. First check the modules dictionary if
1027db96d56Sopenharmony_ci   there's one there, and if not, create a new one and insert it in the modules
1037db96d56Sopenharmony_ci   dictionary. Return ``NULL`` with an exception set on failure.
1047db96d56Sopenharmony_ci
1057db96d56Sopenharmony_ci   .. note::
1067db96d56Sopenharmony_ci
1077db96d56Sopenharmony_ci      This function does not load or import the module; if the module wasn't already
1087db96d56Sopenharmony_ci      loaded, you will get an empty module object. Use :c:func:`PyImport_ImportModule`
1097db96d56Sopenharmony_ci      or one of its variants to import a module.  Package structures implied by a
1107db96d56Sopenharmony_ci      dotted name for *name* are not created if not already present.
1117db96d56Sopenharmony_ci
1127db96d56Sopenharmony_ci   .. versionadded:: 3.3
1137db96d56Sopenharmony_ci
1147db96d56Sopenharmony_ci
1157db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_AddModule(const char *name)
1167db96d56Sopenharmony_ci
1177db96d56Sopenharmony_ci   Similar to :c:func:`PyImport_AddModuleObject`, but the name is a UTF-8
1187db96d56Sopenharmony_ci   encoded string instead of a Unicode object.
1197db96d56Sopenharmony_ci
1207db96d56Sopenharmony_ci
1217db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_ExecCodeModule(const char *name, PyObject *co)
1227db96d56Sopenharmony_ci
1237db96d56Sopenharmony_ci   .. index:: pair: built-in function; compile
1247db96d56Sopenharmony_ci
1257db96d56Sopenharmony_ci   Given a module name (possibly of the form ``package.module``) and a code object
1267db96d56Sopenharmony_ci   read from a Python bytecode file or obtained from the built-in function
1277db96d56Sopenharmony_ci   :func:`compile`, load the module.  Return a new reference to the module object,
1287db96d56Sopenharmony_ci   or ``NULL`` with an exception set if an error occurred.  *name*
1297db96d56Sopenharmony_ci   is removed from :attr:`sys.modules` in error cases, even if *name* was already
1307db96d56Sopenharmony_ci   in :attr:`sys.modules` on entry to :c:func:`PyImport_ExecCodeModule`.  Leaving
1317db96d56Sopenharmony_ci   incompletely initialized modules in :attr:`sys.modules` is dangerous, as imports of
1327db96d56Sopenharmony_ci   such modules have no way to know that the module object is an unknown (and
1337db96d56Sopenharmony_ci   probably damaged with respect to the module author's intents) state.
1347db96d56Sopenharmony_ci
1357db96d56Sopenharmony_ci   The module's :attr:`__spec__` and :attr:`__loader__` will be set, if
1367db96d56Sopenharmony_ci   not set already, with the appropriate values.  The spec's loader will
1377db96d56Sopenharmony_ci   be set to the module's ``__loader__`` (if set) and to an instance of
1387db96d56Sopenharmony_ci   :class:`SourceFileLoader` otherwise.
1397db96d56Sopenharmony_ci
1407db96d56Sopenharmony_ci   The module's :attr:`__file__` attribute will be set to the code object's
1417db96d56Sopenharmony_ci   :c:member:`co_filename`.  If applicable, :attr:`__cached__` will also
1427db96d56Sopenharmony_ci   be set.
1437db96d56Sopenharmony_ci
1447db96d56Sopenharmony_ci   This function will reload the module if it was already imported.  See
1457db96d56Sopenharmony_ci   :c:func:`PyImport_ReloadModule` for the intended way to reload a module.
1467db96d56Sopenharmony_ci
1477db96d56Sopenharmony_ci   If *name* points to a dotted name of the form ``package.module``, any package
1487db96d56Sopenharmony_ci   structures not already created will still not be created.
1497db96d56Sopenharmony_ci
1507db96d56Sopenharmony_ci   See also :c:func:`PyImport_ExecCodeModuleEx` and
1517db96d56Sopenharmony_ci   :c:func:`PyImport_ExecCodeModuleWithPathnames`.
1527db96d56Sopenharmony_ci
1537db96d56Sopenharmony_ci
1547db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
1557db96d56Sopenharmony_ci
1567db96d56Sopenharmony_ci   Like :c:func:`PyImport_ExecCodeModule`, but the :attr:`__file__` attribute of
1577db96d56Sopenharmony_ci   the module object is set to *pathname* if it is non-``NULL``.
1587db96d56Sopenharmony_ci
1597db96d56Sopenharmony_ci   See also :c:func:`PyImport_ExecCodeModuleWithPathnames`.
1607db96d56Sopenharmony_ci
1617db96d56Sopenharmony_ci
1627db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, PyObject *cpathname)
1637db96d56Sopenharmony_ci
1647db96d56Sopenharmony_ci   Like :c:func:`PyImport_ExecCodeModuleEx`, but the :attr:`__cached__`
1657db96d56Sopenharmony_ci   attribute of the module object is set to *cpathname* if it is
1667db96d56Sopenharmony_ci   non-``NULL``.  Of the three functions, this is the preferred one to use.
1677db96d56Sopenharmony_ci
1687db96d56Sopenharmony_ci   .. versionadded:: 3.3
1697db96d56Sopenharmony_ci
1707db96d56Sopenharmony_ci
1717db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, const char *pathname, const char *cpathname)
1727db96d56Sopenharmony_ci
1737db96d56Sopenharmony_ci   Like :c:func:`PyImport_ExecCodeModuleObject`, but *name*, *pathname* and
1747db96d56Sopenharmony_ci   *cpathname* are UTF-8 encoded strings. Attempts are also made to figure out
1757db96d56Sopenharmony_ci   what the value for *pathname* should be from *cpathname* if the former is
1767db96d56Sopenharmony_ci   set to ``NULL``.
1777db96d56Sopenharmony_ci
1787db96d56Sopenharmony_ci   .. versionadded:: 3.2
1797db96d56Sopenharmony_ci   .. versionchanged:: 3.3
1807db96d56Sopenharmony_ci      Uses :func:`imp.source_from_cache()` in calculating the source path if
1817db96d56Sopenharmony_ci      only the bytecode path is provided.
1827db96d56Sopenharmony_ci
1837db96d56Sopenharmony_ci
1847db96d56Sopenharmony_ci.. c:function:: long PyImport_GetMagicNumber()
1857db96d56Sopenharmony_ci
1867db96d56Sopenharmony_ci   Return the magic number for Python bytecode files (a.k.a. :file:`.pyc` file).
1877db96d56Sopenharmony_ci   The magic number should be present in the first four bytes of the bytecode
1887db96d56Sopenharmony_ci   file, in little-endian byte order. Returns ``-1`` on error.
1897db96d56Sopenharmony_ci
1907db96d56Sopenharmony_ci   .. versionchanged:: 3.3
1917db96d56Sopenharmony_ci      Return value of ``-1`` upon failure.
1927db96d56Sopenharmony_ci
1937db96d56Sopenharmony_ci
1947db96d56Sopenharmony_ci.. c:function:: const char * PyImport_GetMagicTag()
1957db96d56Sopenharmony_ci
1967db96d56Sopenharmony_ci   Return the magic tag string for :pep:`3147` format Python bytecode file
1977db96d56Sopenharmony_ci   names.  Keep in mind that the value at ``sys.implementation.cache_tag`` is
1987db96d56Sopenharmony_ci   authoritative and should be used instead of this function.
1997db96d56Sopenharmony_ci
2007db96d56Sopenharmony_ci   .. versionadded:: 3.2
2017db96d56Sopenharmony_ci
2027db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_GetModuleDict()
2037db96d56Sopenharmony_ci
2047db96d56Sopenharmony_ci   Return the dictionary used for the module administration (a.k.a.
2057db96d56Sopenharmony_ci   ``sys.modules``).  Note that this is a per-interpreter variable.
2067db96d56Sopenharmony_ci
2077db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_GetModule(PyObject *name)
2087db96d56Sopenharmony_ci
2097db96d56Sopenharmony_ci   Return the already imported module with the given name.  If the
2107db96d56Sopenharmony_ci   module has not been imported yet then returns ``NULL`` but does not set
2117db96d56Sopenharmony_ci   an error.  Returns ``NULL`` and sets an error if the lookup failed.
2127db96d56Sopenharmony_ci
2137db96d56Sopenharmony_ci   .. versionadded:: 3.7
2147db96d56Sopenharmony_ci
2157db96d56Sopenharmony_ci.. c:function:: PyObject* PyImport_GetImporter(PyObject *path)
2167db96d56Sopenharmony_ci
2177db96d56Sopenharmony_ci   Return a finder object for a :data:`sys.path`/:attr:`pkg.__path__` item
2187db96d56Sopenharmony_ci   *path*, possibly by fetching it from the :data:`sys.path_importer_cache`
2197db96d56Sopenharmony_ci   dict.  If it wasn't yet cached, traverse :data:`sys.path_hooks` until a hook
2207db96d56Sopenharmony_ci   is found that can handle the path item.  Return ``None`` if no hook could;
2217db96d56Sopenharmony_ci   this tells our caller that the :term:`path based finder` could not find a
2227db96d56Sopenharmony_ci   finder for this path item. Cache the result in :data:`sys.path_importer_cache`.
2237db96d56Sopenharmony_ci   Return a new reference to the finder object.
2247db96d56Sopenharmony_ci
2257db96d56Sopenharmony_ci
2267db96d56Sopenharmony_ci.. c:function:: int PyImport_ImportFrozenModuleObject(PyObject *name)
2277db96d56Sopenharmony_ci
2287db96d56Sopenharmony_ci   Load a frozen module named *name*.  Return ``1`` for success, ``0`` if the
2297db96d56Sopenharmony_ci   module is not found, and ``-1`` with an exception set if the initialization
2307db96d56Sopenharmony_ci   failed.  To access the imported module on a successful load, use
2317db96d56Sopenharmony_ci   :c:func:`PyImport_ImportModule`.  (Note the misnomer --- this function would
2327db96d56Sopenharmony_ci   reload the module if it was already imported.)
2337db96d56Sopenharmony_ci
2347db96d56Sopenharmony_ci   .. versionadded:: 3.3
2357db96d56Sopenharmony_ci
2367db96d56Sopenharmony_ci   .. versionchanged:: 3.4
2377db96d56Sopenharmony_ci      The ``__file__`` attribute is no longer set on the module.
2387db96d56Sopenharmony_ci
2397db96d56Sopenharmony_ci
2407db96d56Sopenharmony_ci.. c:function:: int PyImport_ImportFrozenModule(const char *name)
2417db96d56Sopenharmony_ci
2427db96d56Sopenharmony_ci   Similar to :c:func:`PyImport_ImportFrozenModuleObject`, but the name is a
2437db96d56Sopenharmony_ci   UTF-8 encoded string instead of a Unicode object.
2447db96d56Sopenharmony_ci
2457db96d56Sopenharmony_ci
2467db96d56Sopenharmony_ci.. c:struct:: _frozen
2477db96d56Sopenharmony_ci
2487db96d56Sopenharmony_ci   .. index:: single: freeze utility
2497db96d56Sopenharmony_ci
2507db96d56Sopenharmony_ci   This is the structure type definition for frozen module descriptors, as
2517db96d56Sopenharmony_ci   generated by the :program:`freeze` utility (see :file:`Tools/freeze/` in the
2527db96d56Sopenharmony_ci   Python source distribution).  Its definition, found in :file:`Include/import.h`,
2537db96d56Sopenharmony_ci   is::
2547db96d56Sopenharmony_ci
2557db96d56Sopenharmony_ci      struct _frozen {
2567db96d56Sopenharmony_ci          const char *name;
2577db96d56Sopenharmony_ci          const unsigned char *code;
2587db96d56Sopenharmony_ci          int size;
2597db96d56Sopenharmony_ci          bool is_package;
2607db96d56Sopenharmony_ci      };
2617db96d56Sopenharmony_ci
2627db96d56Sopenharmony_ci   .. versionchanged:: 3.11
2637db96d56Sopenharmony_ci      The new ``is_package`` field indicates whether the module is a package or not.
2647db96d56Sopenharmony_ci      This replaces setting the ``size`` field to a negative value.
2657db96d56Sopenharmony_ci
2667db96d56Sopenharmony_ci.. c:var:: const struct _frozen* PyImport_FrozenModules
2677db96d56Sopenharmony_ci
2687db96d56Sopenharmony_ci   This pointer is initialized to point to an array of :c:struct:`_frozen`
2697db96d56Sopenharmony_ci   records, terminated by one whose members are all ``NULL`` or zero.  When a frozen
2707db96d56Sopenharmony_ci   module is imported, it is searched in this table.  Third-party code could play
2717db96d56Sopenharmony_ci   tricks with this to provide a dynamically created collection of frozen modules.
2727db96d56Sopenharmony_ci
2737db96d56Sopenharmony_ci
2747db96d56Sopenharmony_ci.. c:function:: int PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
2757db96d56Sopenharmony_ci
2767db96d56Sopenharmony_ci   Add a single module to the existing table of built-in modules.  This is a
2777db96d56Sopenharmony_ci   convenience wrapper around :c:func:`PyImport_ExtendInittab`, returning ``-1`` if
2787db96d56Sopenharmony_ci   the table could not be extended.  The new module can be imported by the name
2797db96d56Sopenharmony_ci   *name*, and uses the function *initfunc* as the initialization function called
2807db96d56Sopenharmony_ci   on the first attempted import.  This should be called before
2817db96d56Sopenharmony_ci   :c:func:`Py_Initialize`.
2827db96d56Sopenharmony_ci
2837db96d56Sopenharmony_ci
2847db96d56Sopenharmony_ci.. c:struct:: _inittab
2857db96d56Sopenharmony_ci
2867db96d56Sopenharmony_ci   Structure describing a single entry in the list of built-in modules.  Each of
2877db96d56Sopenharmony_ci   these structures gives the name and initialization function for a module built
2887db96d56Sopenharmony_ci   into the interpreter.  The name is an ASCII encoded string.  Programs which
2897db96d56Sopenharmony_ci   embed Python may use an array of these structures in conjunction with
2907db96d56Sopenharmony_ci   :c:func:`PyImport_ExtendInittab` to provide additional built-in modules.
2917db96d56Sopenharmony_ci   The structure is defined in :file:`Include/import.h` as::
2927db96d56Sopenharmony_ci
2937db96d56Sopenharmony_ci      struct _inittab {
2947db96d56Sopenharmony_ci          const char *name;           /* ASCII encoded string */
2957db96d56Sopenharmony_ci          PyObject* (*initfunc)(void);
2967db96d56Sopenharmony_ci      };
2977db96d56Sopenharmony_ci
2987db96d56Sopenharmony_ci
2997db96d56Sopenharmony_ci.. c:function:: int PyImport_ExtendInittab(struct _inittab *newtab)
3007db96d56Sopenharmony_ci
3017db96d56Sopenharmony_ci   Add a collection of modules to the table of built-in modules.  The *newtab*
3027db96d56Sopenharmony_ci   array must end with a sentinel entry which contains ``NULL`` for the :attr:`name`
3037db96d56Sopenharmony_ci   field; failure to provide the sentinel value can result in a memory fault.
3047db96d56Sopenharmony_ci   Returns ``0`` on success or ``-1`` if insufficient memory could be allocated to
3057db96d56Sopenharmony_ci   extend the internal table.  In the event of failure, no modules are added to the
3067db96d56Sopenharmony_ci   internal table.  This must be called before :c:func:`Py_Initialize`.
3077db96d56Sopenharmony_ci
3087db96d56Sopenharmony_ci   If Python is initialized multiple times, :c:func:`PyImport_AppendInittab` or
3097db96d56Sopenharmony_ci   :c:func:`PyImport_ExtendInittab` must be called before each Python
3107db96d56Sopenharmony_ci   initialization.
311