Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _importing: |
| 4 | |
| 5 | Importing Modules |
| 6 | ================= |
| 7 | |
| 8 | |
| 9 | .. cfunction:: PyObject* PyImport_ImportModule(const char *name) |
| 10 | |
| 11 | .. index:: |
| 12 | single: package variable; __all__ |
| 13 | single: __all__ (package variable) |
| 14 | single: modules (in module sys) |
| 15 | |
| 16 | This is a simplified interface to :cfunc:`PyImport_ImportModuleEx` below, |
| 17 | leaving the *globals* and *locals* arguments set to *NULL* and *level* set |
| 18 | to 0. When the *name* |
| 19 | argument contains a dot (when it specifies a submodule of a package), the |
| 20 | *fromlist* argument is set to the list ``['*']`` so that the return value is the |
| 21 | named module rather than the top-level package containing it as would otherwise |
| 22 | be the case. (Unfortunately, this has an additional side effect when *name* in |
| 23 | fact specifies a subpackage instead of a submodule: the submodules specified in |
| 24 | the package's ``__all__`` variable are loaded.) Return a new reference to the |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 25 | imported module, or *NULL* with an exception set on failure. A failing |
| 26 | import of a module doesn't leave the module in :data:`sys.modules`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | .. cfunction:: PyObject* PyImport_ImportModuleNoBlock(const char *name) |
| 30 | |
| 31 | This version of :cfunc:`PyImport_ImportModule` does not block. It's intended |
| 32 | to be used in C functions that import other modules to execute a function. |
| 33 | The import may block if another thread holds the import lock. The function |
| 34 | :cfunc:`PyImport_ImportModuleNoBlock` never blocks. It first tries to fetch |
| 35 | the module from sys.modules and falls back to :cfunc:`PyImport_ImportModule` |
| 36 | unless the lock is held, in which case the function will raise an |
| 37 | :exc:`ImportError`. |
| 38 | |
| 39 | |
| 40 | .. cfunction:: PyObject* PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist) |
| 41 | |
| 42 | .. index:: builtin: __import__ |
| 43 | |
| 44 | Import a module. This is best described by referring to the built-in Python |
| 45 | function :func:`__import__`, as the standard :func:`__import__` function calls |
| 46 | this function directly. |
| 47 | |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 48 | The return value is a new reference to the imported module or top-level |
| 49 | package, or *NULL* with an exception set on failure. Like for |
| 50 | :func:`__import__`, the return value when a submodule of a package was |
| 51 | requested is normally the top-level package, unless a non-empty *fromlist* |
| 52 | was given. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 53 | |
| 54 | Failing imports remove incomplete module objects, like with |
| 55 | :cfunc:`PyImport_ImportModule`. |
| 56 | |
| 57 | |
| 58 | .. cfunction:: PyObject* PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level) |
| 59 | |
| 60 | Import a module. This is best described by referring to the built-in Python |
| 61 | function :func:`__import__`, as the standard :func:`__import__` function calls |
| 62 | this function directly. |
| 63 | |
| 64 | The return value is a new reference to the imported module or top-level package, |
| 65 | or *NULL* with an exception set on failure. Like for :func:`__import__`, |
| 66 | the return value when a submodule of a package was requested is normally the |
| 67 | top-level package, unless a non-empty *fromlist* was given. |
| 68 | |
| 69 | |
| 70 | .. cfunction:: PyObject* PyImport_Import(PyObject *name) |
| 71 | |
| 72 | This is a higher-level interface that calls the current "import hook |
| 73 | function" (with an explicit *level* of 0, meaning absolute import). It |
| 74 | invokes the :func:`__import__` function from the ``__builtins__`` of the |
| 75 | current globals. This means that the import is done using whatever import |
| 76 | hooks are installed in the current environment. |
| 77 | |
| 78 | |
| 79 | .. cfunction:: PyObject* PyImport_ReloadModule(PyObject *m) |
| 80 | |
| 81 | Reload a module. Return a new reference to the reloaded module, or *NULL* with |
| 82 | an exception set on failure (the module still exists in this case). |
| 83 | |
| 84 | |
| 85 | .. cfunction:: PyObject* PyImport_AddModule(const char *name) |
| 86 | |
| 87 | Return the module object corresponding to a module name. The *name* argument |
| 88 | may be of the form ``package.module``. First check the modules dictionary if |
| 89 | there's one there, and if not, create a new one and insert it in the modules |
| 90 | dictionary. Return *NULL* with an exception set on failure. |
| 91 | |
| 92 | .. note:: |
| 93 | |
| 94 | This function does not load or import the module; if the module wasn't already |
| 95 | loaded, you will get an empty module object. Use :cfunc:`PyImport_ImportModule` |
| 96 | or one of its variants to import a module. Package structures implied by a |
| 97 | dotted name for *name* are not created if not already present. |
| 98 | |
| 99 | |
| 100 | .. cfunction:: PyObject* PyImport_ExecCodeModule(char *name, PyObject *co) |
| 101 | |
| 102 | .. index:: builtin: compile |
| 103 | |
| 104 | Given a module name (possibly of the form ``package.module``) and a code object |
| 105 | read from a Python bytecode file or obtained from the built-in function |
| 106 | :func:`compile`, load the module. Return a new reference to the module object, |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 107 | or *NULL* with an exception set if an error occurred. *name* |
| 108 | is removed from :attr:`sys.modules` in error cases, even if *name* was already |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 109 | in :attr:`sys.modules` on entry to :cfunc:`PyImport_ExecCodeModule`. Leaving |
| 110 | incompletely initialized modules in :attr:`sys.modules` is dangerous, as imports of |
| 111 | such modules have no way to know that the module object is an unknown (and |
| 112 | probably damaged with respect to the module author's intents) state. |
| 113 | |
| 114 | This function will reload the module if it was already imported. See |
| 115 | :cfunc:`PyImport_ReloadModule` for the intended way to reload a module. |
| 116 | |
| 117 | If *name* points to a dotted name of the form ``package.module``, any package |
| 118 | structures not already created will still not be created. |
| 119 | |
| 120 | |
| 121 | .. cfunction:: long PyImport_GetMagicNumber() |
| 122 | |
| 123 | Return the magic number for Python bytecode files (a.k.a. :file:`.pyc` and |
| 124 | :file:`.pyo` files). The magic number should be present in the first four bytes |
| 125 | of the bytecode file, in little-endian byte order. |
| 126 | |
| 127 | |
| 128 | .. cfunction:: PyObject* PyImport_GetModuleDict() |
| 129 | |
| 130 | Return the dictionary used for the module administration (a.k.a. |
| 131 | ``sys.modules``). Note that this is a per-interpreter variable. |
| 132 | |
| 133 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 134 | .. cfunction:: PyObject* PyImport_GetImporter(PyObject *path) |
| 135 | |
| 136 | Return an importer object for a :data:`sys.path`/:attr:`pkg.__path__` item |
| 137 | *path*, possibly by fetching it from the :data:`sys.path_importer_cache` |
| 138 | dict. If it wasn't yet cached, traverse :data:`sys.path_hooks` until a hook |
| 139 | is found that can handle the path item. Return ``None`` if no hook could; |
| 140 | this tells our caller it should fall back to the builtin import mechanism. |
| 141 | Cache the result in :data:`sys.path_importer_cache`. Return a new reference |
| 142 | to the importer object. |
| 143 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 144 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 145 | .. cfunction:: void _PyImport_Init() |
| 146 | |
| 147 | Initialize the import mechanism. For internal use only. |
| 148 | |
| 149 | |
| 150 | .. cfunction:: void PyImport_Cleanup() |
| 151 | |
| 152 | Empty the module table. For internal use only. |
| 153 | |
| 154 | |
| 155 | .. cfunction:: void _PyImport_Fini() |
| 156 | |
| 157 | Finalize the import mechanism. For internal use only. |
| 158 | |
| 159 | |
| 160 | .. cfunction:: PyObject* _PyImport_FindExtension(char *, char *) |
| 161 | |
| 162 | For internal use only. |
| 163 | |
| 164 | |
| 165 | .. cfunction:: PyObject* _PyImport_FixupExtension(char *, char *) |
| 166 | |
| 167 | For internal use only. |
| 168 | |
| 169 | |
| 170 | .. cfunction:: int PyImport_ImportFrozenModule(char *name) |
| 171 | |
| 172 | Load a frozen module named *name*. Return ``1`` for success, ``0`` if the |
| 173 | module is not found, and ``-1`` with an exception set if the initialization |
| 174 | failed. To access the imported module on a successful load, use |
| 175 | :cfunc:`PyImport_ImportModule`. (Note the misnomer --- this function would |
| 176 | reload the module if it was already imported.) |
| 177 | |
| 178 | |
| 179 | .. ctype:: struct _frozen |
| 180 | |
| 181 | .. index:: single: freeze utility |
| 182 | |
| 183 | This is the structure type definition for frozen module descriptors, as |
| 184 | generated by the :program:`freeze` utility (see :file:`Tools/freeze/` in the |
| 185 | Python source distribution). Its definition, found in :file:`Include/import.h`, |
| 186 | is:: |
| 187 | |
| 188 | struct _frozen { |
| 189 | char *name; |
| 190 | unsigned char *code; |
| 191 | int size; |
| 192 | }; |
| 193 | |
| 194 | |
| 195 | .. cvar:: struct _frozen* PyImport_FrozenModules |
| 196 | |
| 197 | This pointer is initialized to point to an array of :ctype:`struct _frozen` |
| 198 | records, terminated by one whose members are all *NULL* or zero. When a frozen |
| 199 | module is imported, it is searched in this table. Third-party code could play |
| 200 | tricks with this to provide a dynamically created collection of frozen modules. |
| 201 | |
| 202 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 203 | .. cfunction:: int PyImport_AppendInittab(char *name, PyObject* (*initfunc)(void)) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 204 | |
| 205 | Add a single module to the existing table of built-in modules. This is a |
| 206 | convenience wrapper around :cfunc:`PyImport_ExtendInittab`, returning ``-1`` if |
| 207 | the table could not be extended. The new module can be imported by the name |
| 208 | *name*, and uses the function *initfunc* as the initialization function called |
| 209 | on the first attempted import. This should be called before |
| 210 | :cfunc:`Py_Initialize`. |
| 211 | |
| 212 | |
| 213 | .. ctype:: struct _inittab |
| 214 | |
| 215 | Structure describing a single entry in the list of built-in modules. Each of |
| 216 | these structures gives the name and initialization function for a module built |
| 217 | into the interpreter. Programs which embed Python may use an array of these |
| 218 | structures in conjunction with :cfunc:`PyImport_ExtendInittab` to provide |
| 219 | additional built-in modules. The structure is defined in |
| 220 | :file:`Include/import.h` as:: |
| 221 | |
| 222 | struct _inittab { |
| 223 | char *name; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 224 | PyObject* (*initfunc)(void); |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | |
| 228 | .. cfunction:: int PyImport_ExtendInittab(struct _inittab *newtab) |
| 229 | |
| 230 | Add a collection of modules to the table of built-in modules. The *newtab* |
| 231 | array must end with a sentinel entry which contains *NULL* for the :attr:`name` |
| 232 | field; failure to provide the sentinel value can result in a memory fault. |
| 233 | Returns ``0`` on success or ``-1`` if insufficient memory could be allocated to |
| 234 | extend the internal table. In the event of failure, no modules are added to the |
| 235 | internal table. This should be called before :cfunc:`Py_Initialize`. |