blob: 2d0d1b58668833c808c71ac4f308319c58b17bec [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _importing:
4
5Importing Modules
6=================
7
8
Georg Brandl60203b42010-10-06 10:11:56 +00009.. c:function:: PyObject* PyImport_ImportModule(const char *name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000010
11 .. index::
12 single: package variable; __all__
13 single: __all__ (package variable)
14 single: modules (in module sys)
15
Georg Brandl60203b42010-10-06 10:11:56 +000016 This is a simplified interface to :c:func:`PyImport_ImportModuleEx` below,
Georg Brandl54a3faa2008-01-20 09:30:57 +000017 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 Brandle6bcc912008-05-12 18:05:20 +000025 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 Brandl54a3faa2008-01-20 09:30:57 +000027
Alexandre Vassalotti711ed4a2009-07-17 10:42:05 +000028 This function always uses absolute imports.
29
Georg Brandl54a3faa2008-01-20 09:30:57 +000030
Georg Brandl60203b42010-10-06 10:11:56 +000031.. c:function:: PyObject* PyImport_ImportModuleNoBlock(const char *name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000032
Antoine Pitrouea3eb882012-05-17 18:55:59 +020033 This function is a deprecated alias of :c:func:`PyImport_ImportModule`.
34
35 .. versionchanged:: 3.3
36 This function used to fail immediately when the import lock was held
37 by another thread. In Python 3.3 though, the locking scheme switched
38 to per-module locks for most purposes, so this function's special
39 behaviour isn't needed anymore.
Georg Brandl54a3faa2008-01-20 09:30:57 +000040
41
Georg Brandl60203b42010-10-06 10:11:56 +000042.. c:function:: PyObject* PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist)
Georg Brandl54a3faa2008-01-20 09:30:57 +000043
44 .. index:: builtin: __import__
45
46 Import a module. This is best described by referring to the built-in Python
47 function :func:`__import__`, as the standard :func:`__import__` function calls
48 this function directly.
49
Georg Brandle6bcc912008-05-12 18:05:20 +000050 The return value is a new reference to the imported module or top-level
51 package, or *NULL* with an exception set on failure. Like for
52 :func:`__import__`, the return value when a submodule of a package was
53 requested is normally the top-level package, unless a non-empty *fromlist*
54 was given.
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
56 Failing imports remove incomplete module objects, like with
Georg Brandl60203b42010-10-06 10:11:56 +000057 :c:func:`PyImport_ImportModule`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
59
Victor Stinnerfe93faf2011-03-14 15:54:52 -040060.. c:function:: PyObject* PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
Georg Brandl54a3faa2008-01-20 09:30:57 +000061
62 Import a module. This is best described by referring to the built-in Python
63 function :func:`__import__`, as the standard :func:`__import__` function calls
64 this function directly.
65
66 The return value is a new reference to the imported module or top-level package,
67 or *NULL* with an exception set on failure. Like for :func:`__import__`,
68 the return value when a submodule of a package was requested is normally the
69 top-level package, unless a non-empty *fromlist* was given.
70
Victor Stinnerfe93faf2011-03-14 15:54:52 -040071 .. versionadded:: 3.3
72
73
74.. c:function:: PyObject* PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
75
76 Similar to :c:func:`PyImport_ImportModuleLevelObject`, but the name is an
77 UTF-8 encoded string instead of a Unicode object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
Georg Brandl60203b42010-10-06 10:11:56 +000079.. c:function:: PyObject* PyImport_Import(PyObject *name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000080
81 This is a higher-level interface that calls the current "import hook
82 function" (with an explicit *level* of 0, meaning absolute import). It
83 invokes the :func:`__import__` function from the ``__builtins__`` of the
84 current globals. This means that the import is done using whatever import
85 hooks are installed in the current environment.
86
Alexandre Vassalotti711ed4a2009-07-17 10:42:05 +000087 This function always uses absolute imports.
88
Georg Brandl54a3faa2008-01-20 09:30:57 +000089
Georg Brandl60203b42010-10-06 10:11:56 +000090.. c:function:: PyObject* PyImport_ReloadModule(PyObject *m)
Georg Brandl54a3faa2008-01-20 09:30:57 +000091
92 Reload a module. Return a new reference to the reloaded module, or *NULL* with
93 an exception set on failure (the module still exists in this case).
94
95
Victor Stinner27ee0892011-03-04 12:57:09 +000096.. c:function:: PyObject* PyImport_AddModuleObject(PyObject *name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000097
98 Return the module object corresponding to a module name. The *name* argument
99 may be of the form ``package.module``. First check the modules dictionary if
100 there's one there, and if not, create a new one and insert it in the modules
101 dictionary. Return *NULL* with an exception set on failure.
102
103 .. note::
104
105 This function does not load or import the module; if the module wasn't already
Georg Brandl60203b42010-10-06 10:11:56 +0000106 loaded, you will get an empty module object. Use :c:func:`PyImport_ImportModule`
Georg Brandl54a3faa2008-01-20 09:30:57 +0000107 or one of its variants to import a module. Package structures implied by a
108 dotted name for *name* are not created if not already present.
109
Victor Stinner27ee0892011-03-04 12:57:09 +0000110 .. versionadded:: 3.3
111
112
113.. c:function:: PyObject* PyImport_AddModule(const char *name)
114
Éric Araujo16e6f4c2011-03-20 18:08:19 +0100115 Similar to :c:func:`PyImport_AddModuleObject`, but the name is a UTF-8
Victor Stinner27ee0892011-03-04 12:57:09 +0000116 encoded string instead of a Unicode object.
117
Georg Brandl54a3faa2008-01-20 09:30:57 +0000118
Georg Brandl60203b42010-10-06 10:11:56 +0000119.. c:function:: PyObject* PyImport_ExecCodeModule(char *name, PyObject *co)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000120
121 .. index:: builtin: compile
122
123 Given a module name (possibly of the form ``package.module``) and a code object
124 read from a Python bytecode file or obtained from the built-in function
125 :func:`compile`, load the module. Return a new reference to the module object,
Georg Brandle6bcc912008-05-12 18:05:20 +0000126 or *NULL* with an exception set if an error occurred. *name*
127 is removed from :attr:`sys.modules` in error cases, even if *name* was already
Georg Brandl60203b42010-10-06 10:11:56 +0000128 in :attr:`sys.modules` on entry to :c:func:`PyImport_ExecCodeModule`. Leaving
Georg Brandl54a3faa2008-01-20 09:30:57 +0000129 incompletely initialized modules in :attr:`sys.modules` is dangerous, as imports of
130 such modules have no way to know that the module object is an unknown (and
131 probably damaged with respect to the module author's intents) state.
132
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000133 The module's :attr:`__file__` attribute will be set to the code object's
Georg Brandl60203b42010-10-06 10:11:56 +0000134 :c:member:`co_filename`.
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000135
Georg Brandl54a3faa2008-01-20 09:30:57 +0000136 This function will reload the module if it was already imported. See
Georg Brandl60203b42010-10-06 10:11:56 +0000137 :c:func:`PyImport_ReloadModule` for the intended way to reload a module.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000138
139 If *name* points to a dotted name of the form ``package.module``, any package
140 structures not already created will still not be created.
141
Georg Brandl60203b42010-10-06 10:11:56 +0000142 See also :c:func:`PyImport_ExecCodeModuleEx` and
143 :c:func:`PyImport_ExecCodeModuleWithPathnames`.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000144
Georg Brandl54a3faa2008-01-20 09:30:57 +0000145
Georg Brandl60203b42010-10-06 10:11:56 +0000146.. c:function:: PyObject* PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000147
Georg Brandl60203b42010-10-06 10:11:56 +0000148 Like :c:func:`PyImport_ExecCodeModule`, but the :attr:`__file__` attribute of
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000149 the module object is set to *pathname* if it is non-``NULL``.
150
Georg Brandl60203b42010-10-06 10:11:56 +0000151 See also :c:func:`PyImport_ExecCodeModuleWithPathnames`.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000152
153
Victor Stinner27ee0892011-03-04 12:57:09 +0000154.. c:function:: PyObject* PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, PyObject *cpathname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000155
Georg Brandl60203b42010-10-06 10:11:56 +0000156 Like :c:func:`PyImport_ExecCodeModuleEx`, but the :attr:`__cached__`
Barry Warsaw28a691b2010-04-17 00:19:56 +0000157 attribute of the module object is set to *cpathname* if it is
158 non-``NULL``. Of the three functions, this is the preferred one to use.
159
Victor Stinner27ee0892011-03-04 12:57:09 +0000160 .. versionadded:: 3.3
161
162
163.. c:function:: PyObject* PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname, char *cpathname)
164
165 Like :c:func:`PyImport_ExecCodeModuleObject`, but *name*, *pathname* and
166 *cpathname* are UTF-8 encoded strings.
167
Éric Araujo930df312010-12-16 06:28:48 +0000168 .. versionadded:: 3.2
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000169
Victor Stinner27ee0892011-03-04 12:57:09 +0000170
Georg Brandl60203b42010-10-06 10:11:56 +0000171.. c:function:: long PyImport_GetMagicNumber()
Georg Brandl54a3faa2008-01-20 09:30:57 +0000172
173 Return the magic number for Python bytecode files (a.k.a. :file:`.pyc` and
174 :file:`.pyo` files). The magic number should be present in the first four bytes
Brett Cannon77b2abd2012-07-09 16:09:00 -0400175 of the bytecode file, in little-endian byte order. Returns -1 on error.
176
177 .. versionchanged:: 3.3
178 Return value of -1 upon failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000179
180
Georg Brandl60203b42010-10-06 10:11:56 +0000181.. c:function:: const char * PyImport_GetMagicTag()
Barry Warsaw28a691b2010-04-17 00:19:56 +0000182
183 Return the magic tag string for :pep:`3147` format Python bytecode file
Brett Cannon3adc7b72012-07-09 14:22:12 -0400184 names. Keep in mind that the value at ``sys.implementation.cache_tag`` is
185 authoritative and should be used instead of this function.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000186
Éric Araujo930df312010-12-16 06:28:48 +0000187 .. versionadded:: 3.2
188
Georg Brandl60203b42010-10-06 10:11:56 +0000189.. c:function:: PyObject* PyImport_GetModuleDict()
Georg Brandl54a3faa2008-01-20 09:30:57 +0000190
191 Return the dictionary used for the module administration (a.k.a.
192 ``sys.modules``). Note that this is a per-interpreter variable.
193
194
Georg Brandl60203b42010-10-06 10:11:56 +0000195.. c:function:: PyObject* PyImport_GetImporter(PyObject *path)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000196
197 Return an importer object for a :data:`sys.path`/:attr:`pkg.__path__` item
198 *path*, possibly by fetching it from the :data:`sys.path_importer_cache`
199 dict. If it wasn't yet cached, traverse :data:`sys.path_hooks` until a hook
200 is found that can handle the path item. Return ``None`` if no hook could;
Georg Brandl22b34312009-07-26 14:54:51 +0000201 this tells our caller it should fall back to the built-in import mechanism.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000202 Cache the result in :data:`sys.path_importer_cache`. Return a new reference
203 to the importer object.
204
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000205
Georg Brandl60203b42010-10-06 10:11:56 +0000206.. c:function:: void _PyImport_Init()
Georg Brandl54a3faa2008-01-20 09:30:57 +0000207
208 Initialize the import mechanism. For internal use only.
209
210
Georg Brandl60203b42010-10-06 10:11:56 +0000211.. c:function:: void PyImport_Cleanup()
Georg Brandl54a3faa2008-01-20 09:30:57 +0000212
213 Empty the module table. For internal use only.
214
215
Georg Brandl60203b42010-10-06 10:11:56 +0000216.. c:function:: void _PyImport_Fini()
Georg Brandl54a3faa2008-01-20 09:30:57 +0000217
218 Finalize the import mechanism. For internal use only.
219
220
Georg Brandl60203b42010-10-06 10:11:56 +0000221.. c:function:: PyObject* _PyImport_FindExtension(char *, char *)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000222
223 For internal use only.
224
225
Georg Brandl60203b42010-10-06 10:11:56 +0000226.. c:function:: PyObject* _PyImport_FixupExtension(char *, char *)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000227
228 For internal use only.
229
230
Victor Stinner53dc7352011-03-20 01:50:21 +0100231.. c:function:: int PyImport_ImportFrozenModuleObject(PyObject *name)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000232
233 Load a frozen module named *name*. Return ``1`` for success, ``0`` if the
234 module is not found, and ``-1`` with an exception set if the initialization
235 failed. To access the imported module on a successful load, use
Georg Brandl60203b42010-10-06 10:11:56 +0000236 :c:func:`PyImport_ImportModule`. (Note the misnomer --- this function would
Georg Brandl54a3faa2008-01-20 09:30:57 +0000237 reload the module if it was already imported.)
238
Victor Stinner53dc7352011-03-20 01:50:21 +0100239 .. versionadded:: 3.3
240
241
242.. c:function:: int PyImport_ImportFrozenModule(char *name)
243
Éric Araujo16e6f4c2011-03-20 18:08:19 +0100244 Similar to :c:func:`PyImport_ImportFrozenModuleObject`, but the name is a
Victor Stinner53dc7352011-03-20 01:50:21 +0100245 UTF-8 encoded string instead of a Unicode object.
246
Georg Brandl54a3faa2008-01-20 09:30:57 +0000247
Georg Brandl60203b42010-10-06 10:11:56 +0000248.. c:type:: struct _frozen
Georg Brandl54a3faa2008-01-20 09:30:57 +0000249
250 .. index:: single: freeze utility
251
252 This is the structure type definition for frozen module descriptors, as
253 generated by the :program:`freeze` utility (see :file:`Tools/freeze/` in the
254 Python source distribution). Its definition, found in :file:`Include/import.h`,
255 is::
256
257 struct _frozen {
258 char *name;
259 unsigned char *code;
260 int size;
261 };
262
263
Georg Brandl60203b42010-10-06 10:11:56 +0000264.. c:var:: struct _frozen* PyImport_FrozenModules
Georg Brandl54a3faa2008-01-20 09:30:57 +0000265
Georg Brandl60203b42010-10-06 10:11:56 +0000266 This pointer is initialized to point to an array of :c:type:`struct _frozen`
Georg Brandl54a3faa2008-01-20 09:30:57 +0000267 records, terminated by one whose members are all *NULL* or zero. When a frozen
268 module is imported, it is searched in this table. Third-party code could play
269 tricks with this to provide a dynamically created collection of frozen modules.
270
271
Georg Brandl60203b42010-10-06 10:11:56 +0000272.. c:function:: int PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Georg Brandl54a3faa2008-01-20 09:30:57 +0000273
274 Add a single module to the existing table of built-in modules. This is a
Georg Brandl60203b42010-10-06 10:11:56 +0000275 convenience wrapper around :c:func:`PyImport_ExtendInittab`, returning ``-1`` if
Georg Brandl54a3faa2008-01-20 09:30:57 +0000276 the table could not be extended. The new module can be imported by the name
277 *name*, and uses the function *initfunc* as the initialization function called
278 on the first attempted import. This should be called before
Georg Brandl60203b42010-10-06 10:11:56 +0000279 :c:func:`Py_Initialize`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000280
281
Georg Brandl60203b42010-10-06 10:11:56 +0000282.. c:type:: struct _inittab
Georg Brandl54a3faa2008-01-20 09:30:57 +0000283
284 Structure describing a single entry in the list of built-in modules. Each of
285 these structures gives the name and initialization function for a module built
Victor Stinnerdb536af2011-03-07 18:34:59 +0100286 into the interpreter. The name is an ASCII encoded string. Programs which
287 embed Python may use an array of these structures in conjunction with
288 :c:func:`PyImport_ExtendInittab` to provide additional built-in modules.
289 The structure is defined in :file:`Include/import.h` as::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000290
291 struct _inittab {
Victor Stinnerdb536af2011-03-07 18:34:59 +0100292 char *name; /* ASCII encoded string */
Martin v. Löwis1a214512008-06-11 05:26:20 +0000293 PyObject* (*initfunc)(void);
Georg Brandl54a3faa2008-01-20 09:30:57 +0000294 };
295
296
Georg Brandl60203b42010-10-06 10:11:56 +0000297.. c:function:: int PyImport_ExtendInittab(struct _inittab *newtab)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000298
299 Add a collection of modules to the table of built-in modules. The *newtab*
300 array must end with a sentinel entry which contains *NULL* for the :attr:`name`
301 field; failure to provide the sentinel value can result in a memory fault.
302 Returns ``0`` on success or ``-1`` if insufficient memory could be allocated to
303 extend the internal table. In the event of failure, no modules are added to the
Georg Brandl60203b42010-10-06 10:11:56 +0000304 internal table. This should be called before :c:func:`Py_Initialize`.