blob: eb9fd00706d44234dfa4de89642529bcd6432db7 [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
100 ..method:: find_module(fullname, path=None)
101
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
115 ..method:: load_module(fullname)
116
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
163 ..method:: get_data(path)
164
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
178 ..method:: is_package(fullname)
179
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
184 ..method:: get_source(fullname)
185
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
191 ..method:: get_code(fullname)
192
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
209 ..method:: source_path(fullname)
210
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
215 ..method:: load_module(fullname)
216
217 A concrete implementation of :meth:`importlib.abc.Loader.load_module`
218 that loads Python source code.
219
220 ..method:: get_code(fullname)
221
222 A concrete implementation of
223 :meth:`importlib.abc.InspectLoader.get_code` that creates code objects
224 from Python source code.
225
226
227.. class:: PyPycLoader
228
229 An abstract base class inheriting from :class:`importlib.abc.PyLoader`.
230 This ABC is meant to help in creating loaders that support both Python
231 source and bytecode.
232
233 ..method:: source_mtime(fullname)
234
235 An abstract method which returns the modification time for the source
236 code of the specified module. The modification time should be an
237 integer. If there is no source code, return :keyword:`None. If the
238 module cannot be found then :exc:`ImportError` is raised.
239
240 ..method:: bytecode_path(fullname)
241
242 An abstract method which returns the path to the bytecode for the
243 specified module. :keyword:`None` is returned if there is no bytecode.
244 :exc:`ImportError` is raised if the module is not found.
245
246 ..method:: write_bytecode(fullname, bytecode)
247
248 An abstract method which has the loader write *bytecode* for future
249 use. If the bytecode is written, return :keyword:`True`. Return
250 :keyword:`False` if the bytecode could not be written. This method
251 should not be called if :data:`sys.dont_write_bytecode` is true.
252
253
Brett Cannon78246b62009-01-25 04:56:30 +0000254:mod:`importlib.machinery` -- Importers and path hooks
255------------------------------------------------------
256
257.. module:: importlib.machinery
258 :synopsis: Importers and path hooks
259
260This module contains the various objects that help :keyword:`import`
261find and load modules.
262
263.. class:: BuiltinImporter
264
Brett Cannon2a922ed2009-03-09 03:35:50 +0000265 An :term:`importer` for built-in modules. All known built-in modules are
266 listed in :data:`sys.builtin_module_names`. This class implements the
267 :class:`importlib.abc.Finder` and :class:`importlib.abc.Loader` ABCs.
Brett Cannon78246b62009-01-25 04:56:30 +0000268
269 Only class methods are defined by this class to alleviate the need for
270 instantiation.
271
Brett Cannon78246b62009-01-25 04:56:30 +0000272
273.. class:: FrozenImporter
274
Brett Cannon2a922ed2009-03-09 03:35:50 +0000275 An :term:`importer` for frozen modules. This class implements the
276 :class:`importlib.abc.Finder` and :class:`importlib.abc.Loader` ABCs.
Brett Cannon78246b62009-01-25 04:56:30 +0000277
278 Only class methods are defined by this class to alleviate the need for
279 instantiation.
280
Brett Cannondebb98d2009-02-16 04:18:01 +0000281
282.. class:: PathFinder
283
Brett Cannon2a922ed2009-03-09 03:35:50 +0000284 :term:`Finder` for :data:`sys.path`. This class implements the
285 :class:`importlib.abc.Finder` ABC.
Brett Cannondebb98d2009-02-16 04:18:01 +0000286
287 This class does not perfectly mirror the semantics of :keyword:`import` in
288 terms of :data:`sys.path`. No implicit path hooks are assumed for
289 simplification of the class and its semantics.
290
291 Only class method are defined by this class to alleviate the need for
292 instantiation.
293
294 .. classmethod:: find_module(fullname, path=None)
295
296 Class method that attempts to find a :term:`loader` for the module
Brett Cannon2a922ed2009-03-09 03:35:50 +0000297 specified by *fullname* on :data:`sys.path` or, if defined, on
Brett Cannondebb98d2009-02-16 04:18:01 +0000298 *path*. For each path entry that is searched,
299 :data:`sys.path_importer_cache` is checked. If an non-false object is
Brett Cannon2a922ed2009-03-09 03:35:50 +0000300 found then it is used as the :term:`finder` to look for the module
301 being searched for. If no entry is found in
Brett Cannondebb98d2009-02-16 04:18:01 +0000302 :data:`sys.path_importer_cache`, then :data:`sys.path_hooks` is
303 searched for a finder for the path entry and, if found, is stored in
304 :data:`sys.path_importer_cache` along with being queried about the
Brett Cannon2a922ed2009-03-09 03:35:50 +0000305 module. If no finder is ever found then :keyword:`None` is returned.
Brett Cannond2e7b332009-02-17 02:45:03 +0000306
307
308:mod:`importlib.util` -- Utility code for importers
309---------------------------------------------------
310
311.. module:: importlib.util
312 :synopsis: Importers and path hooks
313
314This module contains the various objects that help in the construction of
315an :term:`importer`.
316
317.. function:: module_for_loader(method)
318
319 A :term:`decorator` for a :term:`loader` which handles selecting the proper
320 module object to load with. The decorated method is expected to have a call
Brett Cannon2a922ed2009-03-09 03:35:50 +0000321 signature taking two positional arguments
322 (e.g. ``load_module(self, module)``) for which the second argument
323 will be the module object to be used by the loader. Note that the decorator
Brett Cannon57b46f52009-03-02 14:38:26 +0000324 will not work on static methods because of the assumption of two
Brett Cannon2a922ed2009-03-09 03:35:50 +0000325 arguments.
Brett Cannond2e7b332009-02-17 02:45:03 +0000326
327 The decorated method will take in the name of the module to be loaded as
Brett Cannon57b46f52009-03-02 14:38:26 +0000328 expected for a :term:`loader`. If the module is not found in
329 :data:`sys.modules` then a new one is constructed with its
330 :attr:`__name__` attribute set. Otherwise the module found in
331 :data:`sys.modules` will be passed into the method. If an
Brett Cannond2e7b332009-02-17 02:45:03 +0000332 exception is raised by the decorated method and a module was added to
333 :data:`sys.modules` it will be removed to prevent a partially initialized
Brett Cannon57b46f52009-03-02 14:38:26 +0000334 module from being in left in :data:`sys.modules`. If the module was already
335 in :data:`sys.modules` then it is left alone.
Brett Cannond2e7b332009-02-17 02:45:03 +0000336
Brett Cannon57b46f52009-03-02 14:38:26 +0000337 Use of this decorator handles all the details of what module object a
338 loader should initialize as specified by :pep:`302`.
339
340
Brett Cannon435aad82009-03-04 16:07:00 +0000341.. function:: set_package(method)
Brett Cannon57b46f52009-03-02 14:38:26 +0000342
343 A :term:`decorator` for a :term:`loader` to set the :attr:`__package__`
344 attribute on the module returned by the loader. If :attr:`__package__` is
345 set and has a value other than :keyword:`None` it will not be changed.
346 Note that the module returned by the loader is what has the attribute
347 set on and not the module found in :data:`sys.modules`.