blob: 9784954e15b7535f97e7eb74d82b11f3bf20b187 [file] [log] [blame]
Brett Cannonafccd632009-01-20 02:21:27 +00001:mod:`importlib` -- An implementation of :keyword:`import`
2==========================================================
3
4.. module:: importlib
5 :synopsis: An implementation of the import machinery.
6
7.. moduleauthor:: Brett Cannon <brett@python.org>
8.. sectionauthor:: Brett Cannon <brett@python.org>
9
10.. versionadded:: 3.1
11
12
13Introduction
14------------
15
16The purpose of the :mod:`importlib` package is two-fold. One is to provide an
17implementation of the :keyword:`import` statement (and thus, by extension, the
18:func:`__import__` function) in Python source code. This provides an
19implementaiton of :keyword:`import` which is portable to any Python
20interpreter. This also provides a reference implementation which is easier to
Brett Cannondebb98d2009-02-16 04:18:01 +000021comprehend than one in a programming language other than Python.
Brett Cannonafccd632009-01-20 02:21:27 +000022
23Two, the components to implement :keyword:`import` can be exposed in this
24package, making it easier for users to create their own custom objects (known
Brett Cannondebb98d2009-02-16 04:18:01 +000025generically as an :term:`importer`) to participate in the import process.
26Details on providing custom importers can be found in :pep:`302`.
Brett Cannonafccd632009-01-20 02:21:27 +000027
28.. seealso::
29
30 :ref:`import`
31 The language reference for the :keyword:`import` statement.
32
33 `Packages specification <http://www.python.org/doc/essays/packages.html>`__
34 Original specification of packages. Some semantics have changed since
35 the writing of this document (e.g. redirecting based on :keyword:`None`
36 in :data:`sys.modules`).
37
38 The :func:`.__import__` function
39 The built-in function for which the :keyword:`import` statement is
40 syntactic sugar for.
41
42 :pep:`235`
43 Import on Case-Insensitive Platforms
44
45 :pep:`263`
46 Defining Python Source Code Encodings
47
48 :pep:`302`
49 New Import Hooks.
50
51 :pep:`328`
52 Imports: Multi-Line and Absolute/Relative
53
54 :pep:`366`
55 Main module explicit relative imports
56
57 :pep:`3128`
58 Using UTF-8 as the Default Source Encoding
59
60
61Functions
62---------
63
Brett Cannon78246b62009-01-25 04:56:30 +000064.. function:: __import__(name, globals={}, locals={}, fromlist=list(), level=0)
Brett Cannonafccd632009-01-20 02:21:27 +000065
66 An implementation of the built-in :func:`__import__` function. See the
67 built-in function's documentation for usage instructions.
68
69.. function:: import_module(name, package=None)
70
Brett Cannon33418c82009-01-22 18:37:20 +000071 Import a module. The *name* argument specifies what module to
Brett Cannonafccd632009-01-20 02:21:27 +000072 import in absolute or relative terms
73 (e.g. either ``pkg.mod`` or ``..mod``). If the name is
Brett Cannon33418c82009-01-22 18:37:20 +000074 specified in relative terms, then the *package* argument must be
Brett Cannon2c318a12009-02-07 01:15:27 +000075 set to the package which is to act as the anchor for resolving the
Brett Cannonafccd632009-01-20 02:21:27 +000076 package name (e.g. ``import_module('..mod', 'pkg.subpkg')`` will import
Brett Cannon2c318a12009-02-07 01:15:27 +000077 ``pkg.mod``).
Brett Cannon78246b62009-01-25 04:56:30 +000078
Brett Cannon2c318a12009-02-07 01:15:27 +000079 The :func:`import_module` function acts as a simplifying wrapper around
80 :func:`__import__`. This means all semantics of the function are derived
81 from :func:`__import__`, including requiring the package where an import is
82 occuring from to already be imported (i.e., *package* must already be
83 imported).
Brett Cannon78246b62009-01-25 04:56:30 +000084
Brett Cannon2a922ed2009-03-09 03:35:50 +000085:mod:`importlib.abc` -- Abstract base classes related to import
86---------------------------------------------------------------
87
88.. module:: importlib.abc
89 :synopsis: Abstract base classes related to import
90
91The :mod:`importlib.abc` module contains all of the core abstract base classes
92used by :keyword:`import`. Some subclasses of the core abstract base classes
93are also provided to help in implementing the core ABCs.
94
95
96.. class:: Finder
97
98 An abstract base class representing a :term:`finder`.
99
Brett Cannon9c751b72009-03-09 16:28:16 +0000100 .. method:: find_module(fullname, path=None)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000101
102 An abstract method for finding a :term:`loader` for the specified
103 module. If the :term:`finder` is found on :data:`sys.meta_path` and the
104 module to be searched for is a subpackage or module then *path* is set
105 to the value of :attr:`__path__` from the parent package. If a loader
106 cannot be found, :keyword:`None` is returned.
107
108 The exact definition of a :term:`finder` can be found in :pep:`302`.
109
110
111.. class:: Loader
112
113 An abstract base class for a :term:`loader`.
114
Brett Cannon9c751b72009-03-09 16:28:16 +0000115 .. method:: load_module(fullname)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000116
117 An abstract method for loading a module. If the module cannot be
118 loaded, :exc:`ImportError` is raised, otherwise the loaded module is
119 returned.
120
121 If the requested module is already exists in :data:`sys.modules`, that
122 module should be used and reloaded.
123 Otherwise a new module is to be created by the loader and inserted into
124 :data:`sys.modules`before any loading begins to prevent recursion from
125 the import. If the loader inserted into a module and the load fails it
126 must be removed by the loader from :data:`sys.modules`; modules already
127 in :data:`sys.modules` before the loader began execution should be left
128 alone. The :func:`importlib.util.module_for_loader` decorator handles
129 all of these details.
130
131 The loader is expected to set several attributes on the module when
132 adding a new module to :data:`sys.modules`.
133
134 - :attr:`__name__`
135 The name of the module.
136
137 - :attr:`__file__`
138 The path to where the module data is stored (not set for built-in
139 modules).
140
141 - :attr:`__path__`
142 Set to a list of strings specifying the search path within a
143 package. This attribute is not set on modules.
144
145 - :attr:`__package__`
146 The parent package for the module/package. If the module is
147 top-level then it has a value of the empty string. The
148 :func:`importlib.util.set_package` decorator can handle the details
149 for :attr:`__package__`.
150
151 - :attr:`__loader__`
152 Set to the loader used to load the module.
153
154 See :pep:`302` for the exact definition for a loader.
155
156
157.. class:: ResourceLoader
158
159 An abstract base class for a :term:`loader` which implements the optional
160 :pep:`302` protocol for loading arbitrary resources from the storage
161 back-end.
162
Brett Cannon9c751b72009-03-09 16:28:16 +0000163 .. method:: get_data(path)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000164
165 An abstract method to return the bytes for the data located at *path*.
166 Loaders that have a file-like storage back-end can implement this
167 abstract method to give direct access
168 to the data stored. :exc:`IOError` is to be raised if the *path* cannot
169 be found. The *path* is expected to be constructed using a module's
170 :attr:`__path__` attribute or an item from :attr:`__path__`.
171
172
173.. class:: InspectLoader
174
175 An abstract base class for a :term:`loader` which implements the optional
176 :pep:`302` protocol for loaders which inspect modules.
177
Brett Cannon9c751b72009-03-09 16:28:16 +0000178 .. method:: is_package(fullname)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000179
180 An abstract method to return a true value if the module is a package, a
181 false value otherwise. :exc:`ImportError` is raised if the
182 :term:`loader` cannot find the module.
183
Brett Cannon9c751b72009-03-09 16:28:16 +0000184 .. method:: get_source(fullname)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000185
186 An abstract method to return the source of a module. It is returned as
187 a string with universal newline support. Returns :keyword:`None` if no
188 source is available (e.g. a built-in module). Raises :exc:`ImportError`
189 if the loader cannot find the module specified.
190
Brett Cannon9c751b72009-03-09 16:28:16 +0000191 .. method:: get_code(fullname)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000192
193 An abstract method to return the :class:`code` object for a module.
194 :keyword:`None` is returned if the module does not have a code object
195 (e.g. built-in module). :exc:`ImportError` is raised if loader cannot
196 find the requested module.
197
198
199.. class:: PyLoader
200
201 An abstract base class inheriting from :class:`importlib.abc.InspectLoader`
202 and :class:`importlib.abc.ResourceLoader` designed to ease the loading of
203 Python source modules (bytecode is not handled; see
204 :class:`importlib.abc.PyPycLoader` for a source/bytecode ABC). A subclass
205 implementing this ABC will only need to worry about exposing how the source
206 code is stored; all other details for loading Python source code will be
207 handled by the concrete implementations of key methods.
208
Brett Cannon9c751b72009-03-09 16:28:16 +0000209 .. method:: source_path(fullname)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000210
211 An abstract method that returns the path to the source code for a
212 module. Should return :keyword:`None` if there is no source code.
213 :exc:`ImportError` if the module cannot be found.
214
Brett Cannon9c751b72009-03-09 16:28:16 +0000215 .. method:: load_module(fullname)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000216
217 A concrete implementation of :meth:`importlib.abc.Loader.load_module`
Brett Cannonad876c72009-03-09 07:53:09 +0000218 that loads Python source code. All needed information comes from the
219 abstract methods required by this ABC. The only pertinent assumption
220 made by this method is that when loading a package
221 :attr:`__path__` is set to ``[os.path.dirname(__file__)]``.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000222
Brett Cannon9c751b72009-03-09 16:28:16 +0000223 .. method:: get_code(fullname)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000224
225 A concrete implementation of
226 :meth:`importlib.abc.InspectLoader.get_code` that creates code objects
227 from Python source code.
228
229
230.. class:: PyPycLoader
231
232 An abstract base class inheriting from :class:`importlib.abc.PyLoader`.
233 This ABC is meant to help in creating loaders that support both Python
234 source and bytecode.
235
Brett Cannon9c751b72009-03-09 16:28:16 +0000236 .. method:: source_mtime(fullname)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000237
238 An abstract method which returns the modification time for the source
239 code of the specified module. The modification time should be an
240 integer. If there is no source code, return :keyword:`None. If the
241 module cannot be found then :exc:`ImportError` is raised.
242
Brett Cannon9c751b72009-03-09 16:28:16 +0000243 .. method:: bytecode_path(fullname)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000244
245 An abstract method which returns the path to the bytecode for the
246 specified module. :keyword:`None` is returned if there is no bytecode.
247 :exc:`ImportError` is raised if the module is not found.
248
Brett Cannon9c751b72009-03-09 16:28:16 +0000249 .. method:: write_bytecode(fullname, bytecode)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000250
251 An abstract method which has the loader write *bytecode* for future
252 use. If the bytecode is written, return :keyword:`True`. Return
253 :keyword:`False` if the bytecode could not be written. This method
254 should not be called if :data:`sys.dont_write_bytecode` is true.
255
256
Brett Cannon78246b62009-01-25 04:56:30 +0000257:mod:`importlib.machinery` -- Importers and path hooks
258------------------------------------------------------
259
260.. module:: importlib.machinery
261 :synopsis: Importers and path hooks
262
263This module contains the various objects that help :keyword:`import`
264find and load modules.
265
266.. class:: BuiltinImporter
267
Brett Cannon2a922ed2009-03-09 03:35:50 +0000268 An :term:`importer` for built-in modules. All known built-in modules are
269 listed in :data:`sys.builtin_module_names`. This class implements the
270 :class:`importlib.abc.Finder` and :class:`importlib.abc.Loader` ABCs.
Brett Cannon78246b62009-01-25 04:56:30 +0000271
272 Only class methods are defined by this class to alleviate the need for
273 instantiation.
274
Brett Cannon78246b62009-01-25 04:56:30 +0000275
276.. class:: FrozenImporter
277
Brett Cannon2a922ed2009-03-09 03:35:50 +0000278 An :term:`importer` for frozen modules. This class implements the
279 :class:`importlib.abc.Finder` and :class:`importlib.abc.Loader` ABCs.
Brett Cannon78246b62009-01-25 04:56:30 +0000280
281 Only class methods are defined by this class to alleviate the need for
282 instantiation.
283
Brett Cannondebb98d2009-02-16 04:18:01 +0000284
285.. class:: PathFinder
286
Brett Cannon2a922ed2009-03-09 03:35:50 +0000287 :term:`Finder` for :data:`sys.path`. This class implements the
288 :class:`importlib.abc.Finder` ABC.
Brett Cannondebb98d2009-02-16 04:18:01 +0000289
290 This class does not perfectly mirror the semantics of :keyword:`import` in
291 terms of :data:`sys.path`. No implicit path hooks are assumed for
292 simplification of the class and its semantics.
293
294 Only class method are defined by this class to alleviate the need for
295 instantiation.
296
297 .. classmethod:: find_module(fullname, path=None)
298
299 Class method that attempts to find a :term:`loader` for the module
Brett Cannon2a922ed2009-03-09 03:35:50 +0000300 specified by *fullname* on :data:`sys.path` or, if defined, on
Brett Cannondebb98d2009-02-16 04:18:01 +0000301 *path*. For each path entry that is searched,
302 :data:`sys.path_importer_cache` is checked. If an non-false object is
Brett Cannon2a922ed2009-03-09 03:35:50 +0000303 found then it is used as the :term:`finder` to look for the module
304 being searched for. If no entry is found in
Brett Cannondebb98d2009-02-16 04:18:01 +0000305 :data:`sys.path_importer_cache`, then :data:`sys.path_hooks` is
306 searched for a finder for the path entry and, if found, is stored in
307 :data:`sys.path_importer_cache` along with being queried about the
Brett Cannon2a922ed2009-03-09 03:35:50 +0000308 module. If no finder is ever found then :keyword:`None` is returned.
Brett Cannond2e7b332009-02-17 02:45:03 +0000309
310
311:mod:`importlib.util` -- Utility code for importers
312---------------------------------------------------
313
314.. module:: importlib.util
315 :synopsis: Importers and path hooks
316
317This module contains the various objects that help in the construction of
318an :term:`importer`.
319
320.. function:: module_for_loader(method)
321
322 A :term:`decorator` for a :term:`loader` which handles selecting the proper
323 module object to load with. The decorated method is expected to have a call
Brett Cannon2a922ed2009-03-09 03:35:50 +0000324 signature taking two positional arguments
325 (e.g. ``load_module(self, module)``) for which the second argument
326 will be the module object to be used by the loader. Note that the decorator
Brett Cannon57b46f52009-03-02 14:38:26 +0000327 will not work on static methods because of the assumption of two
Brett Cannon2a922ed2009-03-09 03:35:50 +0000328 arguments.
Brett Cannond2e7b332009-02-17 02:45:03 +0000329
330 The decorated method will take in the name of the module to be loaded as
Brett Cannon57b46f52009-03-02 14:38:26 +0000331 expected for a :term:`loader`. If the module is not found in
332 :data:`sys.modules` then a new one is constructed with its
333 :attr:`__name__` attribute set. Otherwise the module found in
334 :data:`sys.modules` will be passed into the method. If an
Brett Cannond2e7b332009-02-17 02:45:03 +0000335 exception is raised by the decorated method and a module was added to
336 :data:`sys.modules` it will be removed to prevent a partially initialized
Brett Cannon57b46f52009-03-02 14:38:26 +0000337 module from being in left in :data:`sys.modules`. If the module was already
338 in :data:`sys.modules` then it is left alone.
Brett Cannond2e7b332009-02-17 02:45:03 +0000339
Brett Cannon57b46f52009-03-02 14:38:26 +0000340 Use of this decorator handles all the details of what module object a
341 loader should initialize as specified by :pep:`302`.
342
343
Brett Cannon435aad82009-03-04 16:07:00 +0000344.. function:: set_package(method)
Brett Cannon57b46f52009-03-02 14:38:26 +0000345
346 A :term:`decorator` for a :term:`loader` to set the :attr:`__package__`
347 attribute on the module returned by the loader. If :attr:`__package__` is
348 set and has a value other than :keyword:`None` it will not be changed.
349 Note that the module returned by the loader is what has the attribute
350 set on and not the module found in :data:`sys.modules`.