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