blob: 2ef5b901b930bf55b980840c4fda4d2355bfc4ed [file] [log] [blame]
Barry Warsawd7d21942012-07-29 16:36:17 -04001
Barry Warsawdadebab2012-07-31 16:03:09 -04002.. _importsystem:
Barry Warsawd7d21942012-07-29 16:36:17 -04003
Barry Warsawdadebab2012-07-31 16:03:09 -04004*****************
5The import system
6*****************
Barry Warsawd7d21942012-07-29 16:36:17 -04007
8.. index:: single: import machinery
9
10Python code in one :term:`module` gains access to the code in another module
Barry Warsawc1e721b2012-07-30 16:24:12 -040011by the process of :term:`importing` it. The :keyword:`import` statement is
12the most common way of invoking the import machinery, but it is not the only
13way. Functions such as :func:`importlib.import_module` and built-in
14:func:`__import__` can also be used to invoke the import machinery.
Barry Warsawd7d21942012-07-29 16:36:17 -040015
16The :keyword:`import` statement combines two operations; it searches for the
17named module, then it binds the results of that search to a name in the local
Serhiy Storchaka2b57c432018-12-19 08:09:46 +020018scope. The search operation of the :keyword:`!import` statement is defined as
Nick Coghlan49417742012-08-02 23:03:58 +100019a call to the :func:`__import__` function, with the appropriate arguments.
20The return value of :func:`__import__` is used to perform the name
Serhiy Storchaka2b57c432018-12-19 08:09:46 +020021binding operation of the :keyword:`!import` statement. See the
22:keyword:`!import` statement for the exact details of that name binding
Barry Warsawc1e721b2012-07-30 16:24:12 -040023operation.
Barry Warsawd7d21942012-07-29 16:36:17 -040024
Barry Warsawc1e721b2012-07-30 16:24:12 -040025A direct call to :func:`__import__` performs only the module search and, if
26found, the module creation operation. While certain side-effects may occur,
27such as the importing of parent packages, and the updating of various caches
28(including :data:`sys.modules`), only the :keyword:`import` statement performs
29a name binding operation.
Barry Warsawd7d21942012-07-29 16:36:17 -040030
Barry Warsaw93952f82017-11-03 13:45:46 -070031When an :keyword:`import` statement is executed, the standard builtin
32:func:`__import__` function is called. Other mechanisms for invoking the
33import system (such as :func:`importlib.import_module`) may choose to bypass
34:func:`__import__` and use their own solutions to implement import semantics.
Nick Coghlan49417742012-08-02 23:03:58 +100035
Barry Warsawd7d21942012-07-29 16:36:17 -040036When a module is first imported, Python searches for the module and if found,
Barry Warsawc1e721b2012-07-30 16:24:12 -040037it creates a module object [#fnmo]_, initializing it. If the named module
kms708479c972b52017-05-30 12:12:33 -040038cannot be found, a :exc:`ModuleNotFoundError` is raised. Python implements various
Barry Warsawc1e721b2012-07-30 16:24:12 -040039strategies to search for the named module when the import machinery is
40invoked. These strategies can be modified and extended by using various hooks
Nick Coghlan49417742012-08-02 23:03:58 +100041described in the sections below.
Barry Warsawc1e721b2012-07-30 16:24:12 -040042
Nick Coghlan1685db02012-08-20 13:49:08 +100043.. versionchanged:: 3.3
44 The import system has been updated to fully implement the second phase
Andrew Svetlove2cf03e2012-11-15 16:28:21 +020045 of :pep:`302`. There is no longer any implicit import machinery - the full
Nick Coghlan1685db02012-08-20 13:49:08 +100046 import system is exposed through :data:`sys.meta_path`. In addition,
Andrew Svetlove2cf03e2012-11-15 16:28:21 +020047 native namespace package support has been implemented (see :pep:`420`).
Nick Coghlan1685db02012-08-20 13:49:08 +100048
Barry Warsawc1e721b2012-07-30 16:24:12 -040049
50:mod:`importlib`
51================
52
53The :mod:`importlib` module provides a rich API for interacting with the
54import system. For example :func:`importlib.import_module` provides a
55recommended, simpler API than built-in :func:`__import__` for invoking the
56import machinery. Refer to the :mod:`importlib` library documentation for
57additional detail.
58
Barry Warsawd7d21942012-07-29 16:36:17 -040059
60
61Packages
62========
63
64.. index::
65 single: package
66
67Python has only one type of module object, and all modules are of this type,
68regardless of whether the module is implemented in Python, C, or something
69else. To help organize modules and provide a naming hierarchy, Python has a
Barry Warsawc1e721b2012-07-30 16:24:12 -040070concept of :term:`packages <package>`.
Barry Warsawd7d21942012-07-29 16:36:17 -040071
Barry Warsawc1e721b2012-07-30 16:24:12 -040072You can think of packages as the directories on a file system and modules as
73files within directories, but don't take this analogy too literally since
74packages and modules need not originate from the file system. For the
75purposes of this documentation, we'll use this convenient analogy of
76directories and files. Like file system directories, packages are organized
77hierarchically, and packages may themselves contain subpackages, as well as
78regular modules.
Barry Warsawd7d21942012-07-29 16:36:17 -040079
Barry Warsawc1e721b2012-07-30 16:24:12 -040080It's important to keep in mind that all packages are modules, but not all
81modules are packages. Or put another way, packages are just a special kind of
Nick Coghlan49417742012-08-02 23:03:58 +100082module. Specifically, any module that contains a ``__path__`` attribute is
Barry Warsawc1e721b2012-07-30 16:24:12 -040083considered a package.
84
85All modules have a name. Subpackage names are separated from their parent
Shufc6b1bf2019-11-08 15:26:35 -050086package name by a dot, akin to Python's standard attribute access syntax. Thus
Barry Warsawc1e721b2012-07-30 16:24:12 -040087you might have a module called :mod:`sys` and a package called :mod:`email`,
88which in turn has a subpackage called :mod:`email.mime` and a module within
89that subpackage called :mod:`email.mime.text`.
Barry Warsawd7d21942012-07-29 16:36:17 -040090
91
92Regular packages
93----------------
94
95.. index::
96 pair: package; regular
97
98Python defines two types of packages, :term:`regular packages <regular
99package>` and :term:`namespace packages <namespace package>`. Regular
100packages are traditional packages as they existed in Python 3.2 and earlier.
101A regular package is typically implemented as a directory containing an
102``__init__.py`` file. When a regular package is imported, this
Nick Coghlan49417742012-08-02 23:03:58 +1000103``__init__.py`` file is implicitly executed, and the objects it defines are
Barry Warsawd7d21942012-07-29 16:36:17 -0400104bound to names in the package's namespace. The ``__init__.py`` file can
105contain the same Python code that any other module can contain, and Python
106will add some additional attributes to the module when it is imported.
107
Barry Warsawd7d21942012-07-29 16:36:17 -0400108For example, the following file system layout defines a top level ``parent``
109package with three subpackages::
110
111 parent/
112 __init__.py
113 one/
114 __init__.py
115 two/
116 __init__.py
117 three/
118 __init__.py
119
Nick Coghlan49417742012-08-02 23:03:58 +1000120Importing ``parent.one`` will implicitly execute ``parent/__init__.py`` and
Barry Warsawd7d21942012-07-29 16:36:17 -0400121``parent/one/__init__.py``. Subsequent imports of ``parent.two`` or
Nick Coghlan49417742012-08-02 23:03:58 +1000122``parent.three`` will execute ``parent/two/__init__.py`` and
Barry Warsawd7d21942012-07-29 16:36:17 -0400123``parent/three/__init__.py`` respectively.
124
Barry Warsawc1e721b2012-07-30 16:24:12 -0400125
126Namespace packages
127------------------
128
129.. index::
Serhiy Storchakaddb961d2018-10-26 09:00:49 +0300130 pair: package; namespace
131 pair: package; portion
Barry Warsawc1e721b2012-07-30 16:24:12 -0400132
133A namespace package is a composite of various :term:`portions <portion>`,
134where each portion contributes a subpackage to the parent package. Portions
135may reside in different locations on the file system. Portions may also be
136found in zip files, on the network, or anywhere else that Python searches
137during import. Namespace packages may or may not correspond directly to
138objects on the file system; they may be virtual modules that have no concrete
139representation.
140
Nick Coghlan49417742012-08-02 23:03:58 +1000141Namespace packages do not use an ordinary list for their ``__path__``
142attribute. They instead use a custom iterable type which will automatically
143perform a new search for package portions on the next import attempt within
144that package if the path of their parent package (or :data:`sys.path` for a
145top level package) changes.
146
Barry Warsawd7d21942012-07-29 16:36:17 -0400147With namespace packages, there is no ``parent/__init__.py`` file. In fact,
148there may be multiple ``parent`` directories found during import search, where
Barry Warsawc1e721b2012-07-30 16:24:12 -0400149each one is provided by a different portion. Thus ``parent/one`` may not be
Barry Warsawd7d21942012-07-29 16:36:17 -0400150physically located next to ``parent/two``. In this case, Python will create a
151namespace package for the top-level ``parent`` package whenever it or one of
152its subpackages is imported.
153
Barry Warsawc1e721b2012-07-30 16:24:12 -0400154See also :pep:`420` for the namespace package specification.
155
Barry Warsawd7d21942012-07-29 16:36:17 -0400156
157Searching
158=========
159
160To begin the search, Python needs the :term:`fully qualified <qualified name>`
161name of the module (or package, but for the purposes of this discussion, the
162difference is immaterial) being imported. This name may come from various
163arguments to the :keyword:`import` statement, or from the parameters to the
Barry Warsawc1e721b2012-07-30 16:24:12 -0400164:func:`importlib.import_module` or :func:`__import__` functions.
Barry Warsawd7d21942012-07-29 16:36:17 -0400165
166This name will be used in various phases of the import search, and it may be
167the dotted path to a submodule, e.g. ``foo.bar.baz``. In this case, Python
168first tries to import ``foo``, then ``foo.bar``, and finally ``foo.bar.baz``.
kms708479c972b52017-05-30 12:12:33 -0400169If any of the intermediate imports fail, a :exc:`ModuleNotFoundError` is raised.
Barry Warsawd7d21942012-07-29 16:36:17 -0400170
171
172The module cache
173----------------
174
175.. index::
176 single: sys.modules
177
178The first place checked during import search is :data:`sys.modules`. This
179mapping serves as a cache of all modules that have been previously imported,
180including the intermediate paths. So if ``foo.bar.baz`` was previously
181imported, :data:`sys.modules` will contain entries for ``foo``, ``foo.bar``,
182and ``foo.bar.baz``. Each key will have as its value the corresponding module
183object.
184
185During import, the module name is looked up in :data:`sys.modules` and if
186present, the associated value is the module satisfying the import, and the
kms708479c972b52017-05-30 12:12:33 -0400187process completes. However, if the value is ``None``, then a
Eric Snow46f97b82016-09-07 16:56:15 -0700188:exc:`ModuleNotFoundError` is raised. If the module name is missing, Python will
Barry Warsawd7d21942012-07-29 16:36:17 -0400189continue searching for the module.
190
Nick Coghlan49417742012-08-02 23:03:58 +1000191:data:`sys.modules` is writable. Deleting a key may not destroy the
192associated module (as other modules may hold references to it),
193but it will invalidate the cache entry for the named module, causing
194Python to search anew for the named module upon its next
195import. The key can also be assigned to ``None``, forcing the next import
kms708479c972b52017-05-30 12:12:33 -0400196of the module to result in a :exc:`ModuleNotFoundError`.
Nick Coghlan49417742012-08-02 23:03:58 +1000197
198Beware though, as if you keep a reference to the module object,
Barry Warsawd7d21942012-07-29 16:36:17 -0400199invalidate its cache entry in :data:`sys.modules`, and then re-import the
Nick Coghlan49417742012-08-02 23:03:58 +1000200named module, the two module objects will *not* be the same. By contrast,
Berker Peksag7e732a72015-07-25 13:02:37 +0300201:func:`importlib.reload` will reuse the *same* module object, and simply
Nick Coghlan49417742012-08-02 23:03:58 +1000202reinitialise the module contents by rerunning the module's code.
Barry Warsawd7d21942012-07-29 16:36:17 -0400203
204
Oleg Höflingcbd04082019-12-29 18:26:35 +0100205.. _finders-and-loaders:
206
Barry Warsawd7d21942012-07-29 16:36:17 -0400207Finders and loaders
208-------------------
209
210.. index::
211 single: finder
212 single: loader
Eric Snowb523f842013-11-22 09:05:39 -0700213 single: module spec
Barry Warsawd7d21942012-07-29 16:36:17 -0400214
Barry Warsawdadebab2012-07-31 16:03:09 -0400215If the named module is not found in :data:`sys.modules`, then Python's import
216protocol is invoked to find and load the module. This protocol consists of
217two conceptual objects, :term:`finders <finder>` and :term:`loaders <loader>`.
218A finder's job is to determine whether it can find the named module using
Nick Coghlan49417742012-08-02 23:03:58 +1000219whatever strategy it knows about. Objects that implement both of these
220interfaces are referred to as :term:`importers <importer>` - they return
221themselves when they find that they can load the requested module.
Barry Warsawdadebab2012-07-31 16:03:09 -0400222
Andrew Svetlove2cf03e2012-11-15 16:28:21 +0200223Python includes a number of default finders and importers. The first one
224knows how to locate built-in modules, and the second knows how to locate
225frozen modules. A third default finder searches an :term:`import path`
Nick Coghlan49417742012-08-02 23:03:58 +1000226for modules. The :term:`import path` is a list of locations that may
227name file system paths or zip files. It can also be extended to search
228for any locatable resource, such as those identified by URLs.
Barry Warsawdadebab2012-07-31 16:03:09 -0400229
230The import machinery is extensible, so new finders can be added to extend the
231range and scope of module searching.
Barry Warsawd7d21942012-07-29 16:36:17 -0400232
233Finders do not actually load modules. If they can find the named module, they
Georg Brandl472a65a2013-11-24 12:39:56 +0100234return a :dfn:`module spec`, an encapsulation of the module's import-related
Eric Snowb523f842013-11-22 09:05:39 -0700235information, which the import machinery then uses when loading the module.
Barry Warsawd7d21942012-07-29 16:36:17 -0400236
237The following sections describe the protocol for finders and loaders in more
238detail, including how you can create and register new ones to extend the
239import machinery.
240
Eric Snowb523f842013-11-22 09:05:39 -0700241.. versionchanged:: 3.4
242 In previous versions of Python, finders returned :term:`loaders <loader>`
243 directly, whereas now they return module specs which *contain* loaders.
244 Loaders are still used during import but have fewer responsibilities.
Barry Warsawd7d21942012-07-29 16:36:17 -0400245
246Import hooks
247------------
248
249.. index::
250 single: import hooks
251 single: meta hooks
252 single: path hooks
253 pair: hooks; import
254 pair: hooks; meta
255 pair: hooks; path
256
257The import machinery is designed to be extensible; the primary mechanism for
258this are the *import hooks*. There are two types of import hooks: *meta
Barry Warsawdadebab2012-07-31 16:03:09 -0400259hooks* and *import path hooks*.
Barry Warsawd7d21942012-07-29 16:36:17 -0400260
261Meta hooks are called at the start of import processing, before any other
Barry Warsawdadebab2012-07-31 16:03:09 -0400262import processing has occurred, other than :data:`sys.modules` cache look up.
263This allows meta hooks to override :data:`sys.path` processing, frozen
264modules, or even built-in modules. Meta hooks are registered by adding new
265finder objects to :data:`sys.meta_path`, as described below.
Barry Warsawd7d21942012-07-29 16:36:17 -0400266
Barry Warsawdadebab2012-07-31 16:03:09 -0400267Import path hooks are called as part of :data:`sys.path` (or
268``package.__path__``) processing, at the point where their associated path
269item is encountered. Import path hooks are registered by adding new callables
270to :data:`sys.path_hooks` as described below.
Barry Warsawd7d21942012-07-29 16:36:17 -0400271
272
273The meta path
274-------------
275
276.. index::
277 single: sys.meta_path
Eric Snowb523f842013-11-22 09:05:39 -0700278 pair: finder; find_spec
Barry Warsawd7d21942012-07-29 16:36:17 -0400279
280When the named module is not found in :data:`sys.modules`, Python next
281searches :data:`sys.meta_path`, which contains a list of meta path finder
282objects. These finders are queried in order to see if they know how to handle
283the named module. Meta path finders must implement a method called
Eric Snow7cff4cd2013-12-16 23:10:50 -0700284:meth:`~importlib.abc.MetaPathFinder.find_spec()` which takes three arguments:
285a name, an import path, and (optionally) a target module. The meta path
286finder can use any strategy it wants to determine whether it can handle
287the named module or not.
Barry Warsawd7d21942012-07-29 16:36:17 -0400288
289If the meta path finder knows how to handle the named module, it returns a
Eric Snowb523f842013-11-22 09:05:39 -0700290spec object. If it cannot handle the named module, it returns ``None``. If
Barry Warsawd7d21942012-07-29 16:36:17 -0400291:data:`sys.meta_path` processing reaches the end of its list without returning
Eric Snow46f97b82016-09-07 16:56:15 -0700292a spec, then a :exc:`ModuleNotFoundError` is raised. Any other exceptions
293raised are simply propagated up, aborting the import process.
Barry Warsawd7d21942012-07-29 16:36:17 -0400294
Eric Snow7cff4cd2013-12-16 23:10:50 -0700295The :meth:`~importlib.abc.MetaPathFinder.find_spec()` method of meta path
296finders is called with two or three arguments. The first is the fully
297qualified name of the module being imported, for example ``foo.bar.baz``.
298The second argument is the path entries to use for the module search. For
299top-level modules, the second argument is ``None``, but for submodules or
300subpackages, the second argument is the value of the parent package's
301``__path__`` attribute. If the appropriate ``__path__`` attribute cannot
kms708479c972b52017-05-30 12:12:33 -0400302be accessed, a :exc:`ModuleNotFoundError` is raised. The third argument
Eric Snow46f97b82016-09-07 16:56:15 -0700303is an existing module object that will be the target of loading later.
304The import system passes in a target module only during reload.
Nick Coghlan49417742012-08-02 23:03:58 +1000305
306The meta path may be traversed multiple times for a single import request.
307For example, assuming none of the modules involved has already been cached,
308importing ``foo.bar.baz`` will first perform a top level import, calling
Eric Snow7cff4cd2013-12-16 23:10:50 -0700309``mpf.find_spec("foo", None, None)`` on each meta path finder (``mpf``). After
Nick Coghlan49417742012-08-02 23:03:58 +1000310``foo`` has been imported, ``foo.bar`` will be imported by traversing the
311meta path a second time, calling
Eric Snow7cff4cd2013-12-16 23:10:50 -0700312``mpf.find_spec("foo.bar", foo.__path__, None)``. Once ``foo.bar`` has been
Nick Coghlan49417742012-08-02 23:03:58 +1000313imported, the final traversal will call
Eric Snow7cff4cd2013-12-16 23:10:50 -0700314``mpf.find_spec("foo.bar.baz", foo.bar.__path__, None)``.
Nick Coghlan49417742012-08-02 23:03:58 +1000315
316Some meta path finders only support top level imports. These importers will
317always return ``None`` when anything other than ``None`` is passed as the
318second argument.
Barry Warsawd7d21942012-07-29 16:36:17 -0400319
320Python's default :data:`sys.meta_path` has three meta path finders, one that
321knows how to import built-in modules, one that knows how to import frozen
Barry Warsawdadebab2012-07-31 16:03:09 -0400322modules, and one that knows how to import modules from an :term:`import path`
Nick Coghlan1685db02012-08-20 13:49:08 +1000323(i.e. the :term:`path based finder`).
Barry Warsawd7d21942012-07-29 16:36:17 -0400324
Eric Snowb523f842013-11-22 09:05:39 -0700325.. versionchanged:: 3.4
Eric Snow7cff4cd2013-12-16 23:10:50 -0700326 The :meth:`~importlib.abc.MetaPathFinder.find_spec` method of meta path
327 finders replaced :meth:`~importlib.abc.MetaPathFinder.find_module`, which
328 is now deprecated. While it will continue to work without change, the
329 import machinery will try it only if the finder does not implement
330 ``find_spec()``.
Barry Warsawd7d21942012-07-29 16:36:17 -0400331
Brett Cannona7ff6df2021-03-30 08:43:03 -0700332.. versionchanged:: 3.10
333 Use of :meth:`~importlib.abc.MetaPathFinder.find_module` by the import system
334 now raises :exc:`ImportWarning`.
335
Eric Snowb523f842013-11-22 09:05:39 -0700336
337Loading
Barry Warsawdadebab2012-07-31 16:03:09 -0400338=======
Barry Warsawd7d21942012-07-29 16:36:17 -0400339
Eric Snowb523f842013-11-22 09:05:39 -0700340If and when a module spec is found, the import machinery will use it (and
341the loader it contains) when loading the module. Here is an approximation
342of what happens during the loading portion of import::
Barry Warsawd7d21942012-07-29 16:36:17 -0400343
Eric Snowb523f842013-11-22 09:05:39 -0700344 module = None
345 if spec.loader is not None and hasattr(spec.loader, 'create_module'):
Brett Cannon02d84542015-01-09 11:39:21 -0500346 # It is assumed 'exec_module' will also be defined on the loader.
Eric Snowb523f842013-11-22 09:05:39 -0700347 module = spec.loader.create_module(spec)
348 if module is None:
349 module = ModuleType(spec.name)
350 # The import-related module attributes get set here:
351 _init_module_attrs(spec, module)
Barry Warsawd7d21942012-07-29 16:36:17 -0400352
Eric Snowb523f842013-11-22 09:05:39 -0700353 if spec.loader is None:
Géry Ogamee88af32019-05-01 22:08:17 +0200354 # unsupported
355 raise ImportError
356 if spec.origin is None and spec.submodule_search_locations is not None:
357 # namespace package
358 sys.modules[spec.name] = module
Eric Snowb523f842013-11-22 09:05:39 -0700359 elif not hasattr(spec.loader, 'exec_module'):
360 module = spec.loader.load_module(spec.name)
Eric Snow7cff4cd2013-12-16 23:10:50 -0700361 # Set __loader__ and __package__ if missing.
Eric Snowb523f842013-11-22 09:05:39 -0700362 else:
363 sys.modules[spec.name] = module
364 try:
365 spec.loader.exec_module(module)
366 except BaseException:
367 try:
368 del sys.modules[spec.name]
369 except KeyError:
370 pass
371 raise
Eric Snow7cff4cd2013-12-16 23:10:50 -0700372 return sys.modules[spec.name]
Eric Snowb523f842013-11-22 09:05:39 -0700373
374Note the following details:
Barry Warsawd7d21942012-07-29 16:36:17 -0400375
376 * If there is an existing module object with the given name in
Eric Snowb523f842013-11-22 09:05:39 -0700377 :data:`sys.modules`, import will have already returned it.
Barry Warsawd7d21942012-07-29 16:36:17 -0400378
Eric Snowb523f842013-11-22 09:05:39 -0700379 * The module will exist in :data:`sys.modules` before the loader
Barry Warsawd7d21942012-07-29 16:36:17 -0400380 executes the module code. This is crucial because the module code may
381 (directly or indirectly) import itself; adding it to :data:`sys.modules`
382 beforehand prevents unbounded recursion in the worst case and multiple
383 loading in the best.
384
Eric Snowb523f842013-11-22 09:05:39 -0700385 * If loading fails, the failing module -- and only the failing module --
386 gets removed from :data:`sys.modules`. Any module already in the
387 :data:`sys.modules` cache, and any module that was successfully loaded
388 as a side-effect, must remain in the cache. This contrasts with
389 reloading where even the failing module is left in :data:`sys.modules`.
Barry Warsawd7d21942012-07-29 16:36:17 -0400390
Eric Snowb523f842013-11-22 09:05:39 -0700391 * After the module is created but before execution, the import machinery
Eric Snow7cff4cd2013-12-16 23:10:50 -0700392 sets the import-related module attributes ("_init_module_attrs" in
393 the pseudo-code example above), as summarized in a
394 :ref:`later section <import-mod-attrs>`.
Barry Warsawd7d21942012-07-29 16:36:17 -0400395
Eric Snowb523f842013-11-22 09:05:39 -0700396 * Module execution is the key moment of loading in which the module's
397 namespace gets populated. Execution is entirely delegated to the
398 loader, which gets to decide what gets populated and how.
Barry Warsawd7d21942012-07-29 16:36:17 -0400399
Eric Snowb523f842013-11-22 09:05:39 -0700400 * The module created during loading and passed to exec_module() may
401 not be the one returned at the end of import [#fnlo]_.
Barry Warsawd7d21942012-07-29 16:36:17 -0400402
Eric Snowb523f842013-11-22 09:05:39 -0700403.. versionchanged:: 3.4
404 The import system has taken over the boilerplate responsibilities of
Eric Snow7cff4cd2013-12-16 23:10:50 -0700405 loaders. These were previously performed by the
406 :meth:`importlib.abc.Loader.load_module` method.
Barry Warsawd7d21942012-07-29 16:36:17 -0400407
Eric Snowb523f842013-11-22 09:05:39 -0700408Loaders
409-------
Barry Warsawd7d21942012-07-29 16:36:17 -0400410
Eric Snowb523f842013-11-22 09:05:39 -0700411Module loaders provide the critical function of loading: module execution.
Eric Snow7cff4cd2013-12-16 23:10:50 -0700412The import machinery calls the :meth:`importlib.abc.Loader.exec_module`
Eric Snowb523f842013-11-22 09:05:39 -0700413method with a single argument, the module object to execute. Any value
Eric Snow7cff4cd2013-12-16 23:10:50 -0700414returned from :meth:`~importlib.abc.Loader.exec_module` is ignored.
Eric Snowb523f842013-11-22 09:05:39 -0700415
416Loaders must satisfy the following requirements:
Barry Warsawd7d21942012-07-29 16:36:17 -0400417
418 * If the module is a Python module (as opposed to a built-in module or a
Barry Warsawc1e721b2012-07-30 16:24:12 -0400419 dynamically loaded extension), the loader should execute the module's code
420 in the module's global name space (``module.__dict__``).
Barry Warsawd7d21942012-07-29 16:36:17 -0400421
Eric Snow7cff4cd2013-12-16 23:10:50 -0700422 * If the loader cannot execute the module, it should raise an
Eric Snowb523f842013-11-22 09:05:39 -0700423 :exc:`ImportError`, although any other exception raised during
Eric Snow7cff4cd2013-12-16 23:10:50 -0700424 :meth:`~importlib.abc.Loader.exec_module` will be propagated.
Barry Warsawd7d21942012-07-29 16:36:17 -0400425
Eric Snowb523f842013-11-22 09:05:39 -0700426In many cases, the finder and loader can be the same object; in such cases the
Eric Snow7cff4cd2013-12-16 23:10:50 -0700427:meth:`~importlib.abc.MetaPathFinder.find_spec` method would just return a
428spec with the loader set to ``self``.
Barry Warsawd7d21942012-07-29 16:36:17 -0400429
Eric Snowb523f842013-11-22 09:05:39 -0700430Module loaders may opt in to creating the module object during loading
Eric Snow7cff4cd2013-12-16 23:10:50 -0700431by implementing a :meth:`~importlib.abc.Loader.create_module` method.
432It takes one argument, the module spec, and returns the new module object
433to use during loading. ``create_module()`` does not need to set any attributes
Brett Cannon02d84542015-01-09 11:39:21 -0500434on the module object. If the method returns ``None``, the
Eric Snow7cff4cd2013-12-16 23:10:50 -0700435import machinery will create the new module itself.
Barry Warsawd7d21942012-07-29 16:36:17 -0400436
Eric Snowb523f842013-11-22 09:05:39 -0700437.. versionadded:: 3.4
Marco Buttu46ce7592017-02-26 16:14:45 +0100438 The :meth:`~importlib.abc.Loader.create_module` method of loaders.
Barry Warsawd7d21942012-07-29 16:36:17 -0400439
Eric Snowb523f842013-11-22 09:05:39 -0700440.. versionchanged:: 3.4
Eric Snow7cff4cd2013-12-16 23:10:50 -0700441 The :meth:`~importlib.abc.Loader.load_module` method was replaced by
442 :meth:`~importlib.abc.Loader.exec_module` and the import
Eric Snowb523f842013-11-22 09:05:39 -0700443 machinery assumed all the boilerplate responsibilities of loading.
Barry Warsawd7d21942012-07-29 16:36:17 -0400444
Eric Snowb523f842013-11-22 09:05:39 -0700445 For compatibility with existing loaders, the import machinery will use
Eric Snow7cff4cd2013-12-16 23:10:50 -0700446 the ``load_module()`` method of loaders if it exists and the loader does
Larry Hastingsbfd715e2014-01-05 04:35:56 -0800447 not also implement ``exec_module()``. However, ``load_module()`` has been
Eric Snow7cff4cd2013-12-16 23:10:50 -0700448 deprecated and loaders should implement ``exec_module()`` instead.
Barry Warsawd7d21942012-07-29 16:36:17 -0400449
Eric Snow7cff4cd2013-12-16 23:10:50 -0700450 The ``load_module()`` method must implement all the boilerplate loading
Eric Snowb523f842013-11-22 09:05:39 -0700451 functionality described above in addition to executing the module. All
452 the same constraints apply, with some additional clarification:
Barry Warsawd7d21942012-07-29 16:36:17 -0400453
Eric Snowb523f842013-11-22 09:05:39 -0700454 * If there is an existing module object with the given name in
455 :data:`sys.modules`, the loader must use that existing module.
Eric Snow7cff4cd2013-12-16 23:10:50 -0700456 (Otherwise, :func:`importlib.reload` will not work correctly.) If the
Eric Snowb523f842013-11-22 09:05:39 -0700457 named module does not exist in :data:`sys.modules`, the loader
458 must create a new module object and add it to :data:`sys.modules`.
Barry Warsawd7d21942012-07-29 16:36:17 -0400459
Eric Snowb523f842013-11-22 09:05:39 -0700460 * The module *must* exist in :data:`sys.modules` before the loader
461 executes the module code, to prevent unbounded recursion or multiple
462 loading.
Barry Warsawd7d21942012-07-29 16:36:17 -0400463
Eric Snowb523f842013-11-22 09:05:39 -0700464 * If loading fails, the loader must remove any modules it has inserted
465 into :data:`sys.modules`, but it must remove **only** the failing
Brett Cannond0c4ef12014-11-07 11:29:33 -0500466 module(s), and only if the loader itself has loaded the module(s)
467 explicitly.
Barry Warsawd7d21942012-07-29 16:36:17 -0400468
Brett Cannon02d84542015-01-09 11:39:21 -0500469.. versionchanged:: 3.5
470 A :exc:`DeprecationWarning` is raised when ``exec_module()`` is defined but
Mariatta6b4a5f42017-02-26 07:36:57 -0800471 ``create_module()`` is not.
472
473.. versionchanged:: 3.6
474 An :exc:`ImportError` is raised when ``exec_module()`` is defined but
Mariatta1f5639c2017-02-26 13:23:38 -0800475 ``create_module()`` is not.
Brett Cannon02d84542015-01-09 11:39:21 -0500476
Brett Cannona7ff6df2021-03-30 08:43:03 -0700477.. versionchanged:: 3.10
478 Use of ``load_module()`` will raise :exc:`ImportWarning`.
479
Barry Warsaw2097f532015-04-22 18:29:16 -0400480Submodules
481----------
482
483When a submodule is loaded using any mechanism (e.g. ``importlib`` APIs, the
484``import`` or ``import-from`` statements, or built-in ``__import__()``) a
485binding is placed in the parent module's namespace to the submodule object.
486For example, if package ``spam`` has a submodule ``foo``, after importing
487``spam.foo``, ``spam`` will have an attribute ``foo`` which is bound to the
488submodule. Let's say you have the following directory structure::
489
490 spam/
491 __init__.py
492 foo.py
493 bar.py
494
495and ``spam/__init__.py`` has the following lines in it::
496
497 from .foo import Foo
498 from .bar import Bar
499
500then executing the following puts a name binding to ``foo`` and ``bar`` in the
501``spam`` module::
502
503 >>> import spam
504 >>> spam.foo
505 <module 'spam.foo' from '/tmp/imports/spam/foo.py'>
506 >>> spam.bar
507 <module 'spam.bar' from '/tmp/imports/spam/bar.py'>
508
509Given Python's familiar name binding rules this might seem surprising, but
510it's actually a fundamental feature of the import system. The invariant
511holding is that if you have ``sys.modules['spam']`` and
512``sys.modules['spam.foo']`` (as you would after the above import), the latter
513must appear as the ``foo`` attribute of the former.
514
Eric Snowb523f842013-11-22 09:05:39 -0700515Module spec
516-----------
Barry Warsawd7d21942012-07-29 16:36:17 -0400517
Eric Snowb523f842013-11-22 09:05:39 -0700518The import machinery uses a variety of information about each module
519during import, especially before loading. Most of the information is
520common to all modules. The purpose of a module's spec is to encapsulate
521this import-related information on a per-module basis.
Barry Warsawd7d21942012-07-29 16:36:17 -0400522
Eric Snowb523f842013-11-22 09:05:39 -0700523Using a spec during import allows state to be transferred between import
524system components, e.g. between the finder that creates the module spec
525and the loader that executes it. Most importantly, it allows the
526import machinery to perform the boilerplate operations of loading,
527whereas without a module spec the loader had that responsibility.
Barry Warsawd7d21942012-07-29 16:36:17 -0400528
Barry Warsaw191e3132017-10-17 15:52:38 -0400529The module's spec is exposed as the ``__spec__`` attribute on a module object.
530See :class:`~importlib.machinery.ModuleSpec` for details on the contents of
531the module spec.
Eric Snowb523f842013-11-22 09:05:39 -0700532
533.. versionadded:: 3.4
534
Georg Brandl472a65a2013-11-24 12:39:56 +0100535.. _import-mod-attrs:
536
Eric Snowb523f842013-11-22 09:05:39 -0700537Import-related module attributes
538--------------------------------
539
540The import machinery fills in these attributes on each module object
541during loading, based on the module's spec, before the loader executes
542the module.
543
544.. attribute:: __name__
545
546 The ``__name__`` attribute must be set to the fully-qualified name of
547 the module. This name is used to uniquely identify the module in
548 the import system.
549
550.. attribute:: __loader__
551
552 The ``__loader__`` attribute must be set to the loader object that
553 the import machinery used when loading the module. This is mostly
554 for introspection, but can be used for additional loader-specific
555 functionality, for example getting data associated with a loader.
556
557.. attribute:: __package__
558
559 The module's ``__package__`` attribute must be set. Its value must
560 be a string, but it can be the same value as its ``__name__``. When
561 the module is a package, its ``__package__`` value should be set to
562 its ``__name__``. When the module is not a package, ``__package__``
563 should be set to the empty string for top-level modules, or for
564 submodules, to the parent package's name. See :pep:`366` for further
565 details.
566
567 This attribute is used instead of ``__name__`` to calculate explicit
Brett Cannon849113a2016-01-22 15:25:50 -0800568 relative imports for main modules, as defined in :pep:`366`. It is
569 expected to have the same value as ``__spec__.parent``.
570
571 .. versionchanged:: 3.6
572 The value of ``__package__`` is expected to be the same as
573 ``__spec__.parent``.
Eric Snowb523f842013-11-22 09:05:39 -0700574
575.. attribute:: __spec__
576
577 The ``__spec__`` attribute must be set to the module spec that was
Brett Cannon849113a2016-01-22 15:25:50 -0800578 used when importing the module. Setting ``__spec__``
Eric Snowe50f9aa2014-03-28 18:10:33 -0600579 appropriately applies equally to :ref:`modules initialized during
580 interpreter startup <programs>`. The one exception is ``__main__``,
581 where ``__spec__`` is :ref:`set to None in some cases <main_spec>`.
Eric Snowb523f842013-11-22 09:05:39 -0700582
Brett Cannon849113a2016-01-22 15:25:50 -0800583 When ``__package__`` is not defined, ``__spec__.parent`` is used as
584 a fallback.
585
Eric Snow7cff4cd2013-12-16 23:10:50 -0700586 .. versionadded:: 3.4
587
Brett Cannon849113a2016-01-22 15:25:50 -0800588 .. versionchanged:: 3.6
589 ``__spec__.parent`` is used as a fallback when ``__package__`` is
590 not defined.
591
Eric Snowb523f842013-11-22 09:05:39 -0700592.. attribute:: __path__
593
594 If the module is a package (either regular or namespace), the module
595 object's ``__path__`` attribute must be set. The value must be
596 iterable, but may be empty if ``__path__`` has no further significance.
597 If ``__path__`` is not empty, it must produce strings when iterated
598 over. More details on the semantics of ``__path__`` are given
599 :ref:`below <package-path-rules>`.
600
601 Non-package modules should not have a ``__path__`` attribute.
602
603.. attribute:: __file__
604.. attribute:: __cached__
605
606 ``__file__`` is optional. If set, this attribute's value must be a
607 string. The import system may opt to leave ``__file__`` unset if it
608 has no semantic meaning (e.g. a module loaded from a database).
609
610 If ``__file__`` is set, it may also be appropriate to set the
611 ``__cached__`` attribute which is the path to any compiled version of
612 the code (e.g. byte-compiled file). The file does not need to exist
613 to set this attribute; the path can simply point to where the
614 compiled file would exist (see :pep:`3147`).
615
616 It is also appropriate to set ``__cached__`` when ``__file__`` is not
617 set. However, that scenario is quite atypical. Ultimately, the
618 loader is what makes use of ``__file__`` and/or ``__cached__``. So
619 if a loader can load from a cached module but otherwise does not load
620 from a file, that atypical scenario may be appropriate.
Barry Warsawd7d21942012-07-29 16:36:17 -0400621
Nick Coghlan49417742012-08-02 23:03:58 +1000622.. _package-path-rules:
623
Barry Warsawd7d21942012-07-29 16:36:17 -0400624module.__path__
625---------------
626
Brett Cannon441d9452018-04-20 16:32:46 -0700627By definition, if a module has a ``__path__`` attribute, it is a package.
Barry Warsawd7d21942012-07-29 16:36:17 -0400628
629A package's ``__path__`` attribute is used during imports of its subpackages.
630Within the import machinery, it functions much the same as :data:`sys.path`,
631i.e. providing a list of locations to search for modules during import.
632However, ``__path__`` is typically much more constrained than
633:data:`sys.path`.
634
Nick Coghlan49417742012-08-02 23:03:58 +1000635``__path__`` must be an iterable of strings, but it may be empty.
636The same rules used for :data:`sys.path` also apply to a package's
637``__path__``, and :data:`sys.path_hooks` (described below) are
638consulted when traversing a package's ``__path__``.
Barry Warsawd7d21942012-07-29 16:36:17 -0400639
640A package's ``__init__.py`` file may set or alter the package's ``__path__``
641attribute, and this was typically the way namespace packages were implemented
642prior to :pep:`420`. With the adoption of :pep:`420`, namespace packages no
643longer need to supply ``__init__.py`` files containing only ``__path__``
Eric Snowb523f842013-11-22 09:05:39 -0700644manipulation code; the import machinery automatically sets ``__path__``
Barry Warsawd7d21942012-07-29 16:36:17 -0400645correctly for the namespace package.
646
Eric Snowb523f842013-11-22 09:05:39 -0700647Module reprs
648------------
649
650By default, all modules have a usable repr, however depending on the
651attributes set above, and in the module's spec, you can more explicitly
652control the repr of module objects.
653
654If the module has a spec (``__spec__``), the import machinery will try
655to generate a repr from it. If that fails or there is no spec, the import
656system will craft a default repr using whatever information is available
657on the module. It will try to use the ``module.__name__``,
658``module.__file__``, and ``module.__loader__`` as input into the repr,
659with defaults for whatever information is missing.
660
661Here are the exact rules used:
662
663 * If the module has a ``__spec__`` attribute, the information in the spec
664 is used to generate the repr. The "name", "loader", "origin", and
665 "has_location" attributes are consulted.
666
667 * If the module has a ``__file__`` attribute, this is used as part of the
668 module's repr.
669
670 * If the module has no ``__file__`` but does have a ``__loader__`` that is not
671 ``None``, then the loader's repr is used as part of the module's repr.
672
673 * Otherwise, just use the module's ``__name__`` in the repr.
674
675.. versionchanged:: 3.4
Eric Snow7cff4cd2013-12-16 23:10:50 -0700676 Use of :meth:`loader.module_repr() <importlib.abc.Loader.module_repr>`
677 has been deprecated and the module spec is now used by the import
678 machinery to generate a module repr.
Eric Snowb523f842013-11-22 09:05:39 -0700679
680 For backward compatibility with Python 3.3, the module repr will be
Eric Snow7cff4cd2013-12-16 23:10:50 -0700681 generated by calling the loader's
682 :meth:`~importlib.abc.Loader.module_repr` method, if defined, before
683 trying either approach described above. However, the method is deprecated.
Eric Snowb523f842013-11-22 09:05:39 -0700684
Brett Cannon18990872021-03-26 11:55:07 -0700685.. versionchanged:: 3.10
686
687 Calling :meth:`~importlib.abc.Loader.module_repr` now occurs after trying to
688 use a module's ``__spec__`` attribute but before falling back on
689 ``__file__``. Use of :meth:`~importlib.abc.Loader.module_repr` is slated to
690 stop in Python 3.12.
691
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800692.. _pyc-invalidation:
693
694Cached bytecode invalidation
695----------------------------
696
Zackery Spytzcb115e32020-10-21 02:36:03 -0600697Before Python loads cached bytecode from a ``.pyc`` file, it checks whether the
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800698cache is up-to-date with the source ``.py`` file. By default, Python does this
699by storing the source's last-modified timestamp and size in the cache file when
700writing it. At runtime, the import system then validates the cache file by
Shu2d56af72019-11-12 22:12:11 -0500701checking the stored metadata in the cache file against the source's
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800702metadata.
703
704Python also supports "hash-based" cache files, which store a hash of the source
705file's contents rather than its metadata. There are two variants of hash-based
706``.pyc`` files: checked and unchecked. For checked hash-based ``.pyc`` files,
707Python validates the cache file by hashing the source file and comparing the
708resulting hash with the hash in the cache file. If a checked hash-based cache
709file is found to be invalid, Python regenerates it and writes a new checked
710hash-based cache file. For unchecked hash-based ``.pyc`` files, Python simply
711assumes the cache file is valid if it exists. Hash-based ``.pyc`` files
712validation behavior may be overridden with the :option:`--check-hash-based-pycs`
713flag.
714
715.. versionchanged:: 3.7
716 Added hash-based ``.pyc`` files. Previously, Python only supported
717 timestamp-based invalidation of bytecode caches.
718
Barry Warsawd7d21942012-07-29 16:36:17 -0400719
Nick Coghlan1685db02012-08-20 13:49:08 +1000720The Path Based Finder
721=====================
Barry Warsawd7d21942012-07-29 16:36:17 -0400722
723.. index::
Nick Coghlan1685db02012-08-20 13:49:08 +1000724 single: path based finder
Barry Warsawd7d21942012-07-29 16:36:17 -0400725
726As mentioned previously, Python comes with several default meta path finders.
Eric Snow7cff4cd2013-12-16 23:10:50 -0700727One of these, called the :term:`path based finder`
Serhiy Storchaka2a614522013-12-23 18:21:57 +0200728(:class:`~importlib.machinery.PathFinder`), searches an :term:`import path`,
Eric Snow7cff4cd2013-12-16 23:10:50 -0700729which contains a list of :term:`path entries <path entry>`. Each path
Barry Warsawdadebab2012-07-31 16:03:09 -0400730entry names a location to search for modules.
Barry Warsawd7d21942012-07-29 16:36:17 -0400731
Nick Coghlan1685db02012-08-20 13:49:08 +1000732The path based finder itself doesn't know how to import anything. Instead, it
Nick Coghlan49417742012-08-02 23:03:58 +1000733traverses the individual path entries, associating each of them with a
734path entry finder that knows how to handle that particular kind of path.
735
736The default set of path entry finders implement all the semantics for finding
737modules on the file system, handling special file types such as Python source
Brett Cannonf299abd2015-04-13 14:21:02 -0400738code (``.py`` files), Python byte code (``.pyc`` files) and
Nick Coghlan49417742012-08-02 23:03:58 +1000739shared libraries (e.g. ``.so`` files). When supported by the :mod:`zipimport`
740module in the standard library, the default path entry finders also handle
741loading all of these file types (other than shared libraries) from zipfiles.
Barry Warsawdadebab2012-07-31 16:03:09 -0400742
743Path entries need not be limited to file system locations. They can refer to
Nick Coghlan1685db02012-08-20 13:49:08 +1000744URLs, database queries, or any other location that can be specified as a
Nick Coghlan49417742012-08-02 23:03:58 +1000745string.
Barry Warsawdadebab2012-07-31 16:03:09 -0400746
Nick Coghlan1685db02012-08-20 13:49:08 +1000747The path based finder provides additional hooks and protocols so that you
Barry Warsawdadebab2012-07-31 16:03:09 -0400748can extend and customize the types of searchable path entries. For example,
749if you wanted to support path entries as network URLs, you could write a hook
750that implements HTTP semantics to find modules on the web. This hook (a
751callable) would return a :term:`path entry finder` supporting the protocol
752described below, which was then used to get a loader for the module from the
753web.
Barry Warsawd7d21942012-07-29 16:36:17 -0400754
755A word of warning: this section and the previous both use the term *finder*,
756distinguishing between them by using the terms :term:`meta path finder` and
Barry Warsawdadebab2012-07-31 16:03:09 -0400757:term:`path entry finder`. These two types of finders are very similar,
758support similar protocols, and function in similar ways during the import
759process, but it's important to keep in mind that they are subtly different.
760In particular, meta path finders operate at the beginning of the import
761process, as keyed off the :data:`sys.meta_path` traversal.
Barry Warsawd7d21942012-07-29 16:36:17 -0400762
Nick Coghlan1685db02012-08-20 13:49:08 +1000763By contrast, path entry finders are in a sense an implementation detail
764of the path based finder, and in fact, if the path based finder were to be
Barry Warsawdadebab2012-07-31 16:03:09 -0400765removed from :data:`sys.meta_path`, none of the path entry finder semantics
766would be invoked.
Barry Warsawd7d21942012-07-29 16:36:17 -0400767
768
Barry Warsawdadebab2012-07-31 16:03:09 -0400769Path entry finders
770------------------
Barry Warsawd7d21942012-07-29 16:36:17 -0400771
772.. index::
773 single: sys.path
774 single: sys.path_hooks
775 single: sys.path_importer_cache
776 single: PYTHONPATH
777
Eric Snow7cff4cd2013-12-16 23:10:50 -0700778The :term:`path based finder` is responsible for finding and loading
779Python modules and packages whose location is specified with a string
780:term:`path entry`. Most path entries name locations in the file system,
781but they need not be limited to this.
Barry Warsawdadebab2012-07-31 16:03:09 -0400782
Nick Coghlan1685db02012-08-20 13:49:08 +1000783As a meta path finder, the :term:`path based finder` implements the
Eric Snow7cff4cd2013-12-16 23:10:50 -0700784:meth:`~importlib.abc.MetaPathFinder.find_spec` protocol previously
785described, however it exposes additional hooks that can be used to
786customize how modules are found and loaded from the :term:`import path`.
Barry Warsawd7d21942012-07-29 16:36:17 -0400787
Nick Coghlan1685db02012-08-20 13:49:08 +1000788Three variables are used by the :term:`path based finder`, :data:`sys.path`,
Barry Warsawdadebab2012-07-31 16:03:09 -0400789:data:`sys.path_hooks` and :data:`sys.path_importer_cache`. The ``__path__``
Nick Coghlan49417742012-08-02 23:03:58 +1000790attributes on package objects are also used. These provide additional ways
791that the import machinery can be customized.
Barry Warsawd7d21942012-07-29 16:36:17 -0400792
793:data:`sys.path` contains a list of strings providing search locations for
794modules and packages. It is initialized from the :data:`PYTHONPATH`
795environment variable and various other installation- and
796implementation-specific defaults. Entries in :data:`sys.path` can name
797directories on the file system, zip files, and potentially other "locations"
Barry Warsawdadebab2012-07-31 16:03:09 -0400798(see the :mod:`site` module) that should be searched for modules, such as
Barry Warsaw82c1c782012-11-20 15:22:51 -0500799URLs, or database queries. Only strings and bytes should be present on
800:data:`sys.path`; all other data types are ignored. The encoding of bytes
801entries is determined by the individual :term:`path entry finders <path entry
802finder>`.
Barry Warsawd7d21942012-07-29 16:36:17 -0400803
Nick Coghlan1685db02012-08-20 13:49:08 +1000804The :term:`path based finder` is a :term:`meta path finder`, so the import
Nick Coghlan49417742012-08-02 23:03:58 +1000805machinery begins the :term:`import path` search by calling the path
Eric Snow7cff4cd2013-12-16 23:10:50 -0700806based finder's :meth:`~importlib.machinery.PathFinder.find_spec` method as
807described previously. When the ``path`` argument to
808:meth:`~importlib.machinery.PathFinder.find_spec` is given, it will be a
Nick Coghlan49417742012-08-02 23:03:58 +1000809list of string paths to traverse - typically a package's ``__path__``
Eric Snow7cff4cd2013-12-16 23:10:50 -0700810attribute for an import within that package. If the ``path`` argument is
811``None``, this indicates a top level import and :data:`sys.path` is used.
Barry Warsawd7d21942012-07-29 16:36:17 -0400812
Nick Coghlan1685db02012-08-20 13:49:08 +1000813The path based finder iterates over every entry in the search path, and
Eric Snow7cff4cd2013-12-16 23:10:50 -0700814for each of these, looks for an appropriate :term:`path entry finder`
815(:class:`~importlib.abc.PathEntryFinder`) for the
Barry Warsawdadebab2012-07-31 16:03:09 -0400816path entry. Because this can be an expensive operation (e.g. there may be
Nick Coghlan1685db02012-08-20 13:49:08 +1000817`stat()` call overheads for this search), the path based finder maintains
Barry Warsawdadebab2012-07-31 16:03:09 -0400818a cache mapping path entries to path entry finders. This cache is maintained
Nick Coghlan1685db02012-08-20 13:49:08 +1000819in :data:`sys.path_importer_cache` (despite the name, this cache actually
820stores finder objects rather than being limited to :term:`importer` objects).
821In this way, the expensive search for a particular :term:`path entry`
822location's :term:`path entry finder` need only be done once. User code is
823free to remove cache entries from :data:`sys.path_importer_cache` forcing
824the path based finder to perform the path entry search again [#fnpic]_.
Barry Warsawd7d21942012-07-29 16:36:17 -0400825
Nick Coghlan1685db02012-08-20 13:49:08 +1000826If the path entry is not present in the cache, the path based finder iterates
Barry Warsaw82c1c782012-11-20 15:22:51 -0500827over every callable in :data:`sys.path_hooks`. Each of the :term:`path entry
828hooks <path entry hook>` in this list is called with a single argument, the
829path entry to be searched. This callable may either return a :term:`path
830entry finder` that can handle the path entry, or it may raise
831:exc:`ImportError`. An :exc:`ImportError` is used by the path based finder to
Brett Cannon6336fb22016-08-12 10:56:48 -0700832signal that the hook cannot find a :term:`path entry finder`
Eric Snow7cff4cd2013-12-16 23:10:50 -0700833for that :term:`path entry`. The
834exception is ignored and :term:`import path` iteration continues. The hook
835should expect either a string or bytes object; the encoding of bytes objects
836is up to the hook (e.g. it may be a file system encoding, UTF-8, or something
837else), and if the hook cannot decode the argument, it should raise
838:exc:`ImportError`.
Barry Warsawd7d21942012-07-29 16:36:17 -0400839
Barry Warsawdadebab2012-07-31 16:03:09 -0400840If :data:`sys.path_hooks` iteration ends with no :term:`path entry finder`
Eric Snow7cff4cd2013-12-16 23:10:50 -0700841being returned, then the path based finder's
842:meth:`~importlib.machinery.PathFinder.find_spec` method will store ``None``
843in :data:`sys.path_importer_cache` (to indicate that there is no finder for
844this path entry) and return ``None``, indicating that this
845:term:`meta path finder` could not find the module.
Barry Warsawd7d21942012-07-29 16:36:17 -0400846
Barry Warsawdadebab2012-07-31 16:03:09 -0400847If a :term:`path entry finder` *is* returned by one of the :term:`path entry
848hook` callables on :data:`sys.path_hooks`, then the following protocol is used
Eric Snowb523f842013-11-22 09:05:39 -0700849to ask the finder for a module spec, which is then used when loading the
850module.
Barry Warsawd7d21942012-07-29 16:36:17 -0400851
Brett Cannonb6e25562014-11-21 12:19:28 -0500852The current working directory -- denoted by an empty string -- is handled
853slightly differently from other entries on :data:`sys.path`. First, if the
854current working directory is found to not exist, no value is stored in
855:data:`sys.path_importer_cache`. Second, the value for the current working
856directory is looked up fresh for each module lookup. Third, the path used for
857:data:`sys.path_importer_cache` and returned by
858:meth:`importlib.machinery.PathFinder.find_spec` will be the actual current
859working directory and not the empty string.
860
Barry Warsawdadebab2012-07-31 16:03:09 -0400861Path entry finder protocol
862--------------------------
Barry Warsawd7d21942012-07-29 16:36:17 -0400863
Nick Coghlan49417742012-08-02 23:03:58 +1000864In order to support imports of modules and initialized packages and also to
865contribute portions to namespace packages, path entry finders must implement
Eric Snow7cff4cd2013-12-16 23:10:50 -0700866the :meth:`~importlib.abc.PathEntryFinder.find_spec` method.
Barry Warsawd7d21942012-07-29 16:36:17 -0400867
Shu2d56af72019-11-12 22:12:11 -0500868:meth:`~importlib.abc.PathEntryFinder.find_spec` takes two arguments: the
Eric Snow7cff4cd2013-12-16 23:10:50 -0700869fully qualified name of the module being imported, and the (optional) target
870module. ``find_spec()`` returns a fully populated spec for the module.
871This spec will always have "loader" set (with one exception).
Barry Warsawd7d21942012-07-29 16:36:17 -0400872
Eric Snowb523f842013-11-22 09:05:39 -0700873To indicate to the import machinery that the spec represents a namespace
Géry Ogam27f1bd82020-10-21 23:17:35 +0200874:term:`portion`, the path entry finder sets "submodule_search_locations" to
875a list containing the portion.
Nick Coghlan49417742012-08-02 23:03:58 +1000876
Eric Snowb523f842013-11-22 09:05:39 -0700877.. versionchanged:: 3.4
Eric Snow7cff4cd2013-12-16 23:10:50 -0700878 :meth:`~importlib.abc.PathEntryFinder.find_spec` replaced
879 :meth:`~importlib.abc.PathEntryFinder.find_loader` and
880 :meth:`~importlib.abc.PathEntryFinder.find_module`, both of which
881 are now deprecated, but will be used if ``find_spec()`` is not defined.
Nick Coghlan49417742012-08-02 23:03:58 +1000882
Eric Snowb523f842013-11-22 09:05:39 -0700883 Older path entry finders may implement one of these two deprecated methods
Eric Snow7cff4cd2013-12-16 23:10:50 -0700884 instead of ``find_spec()``. The methods are still respected for the
Brett Cannon6336fb22016-08-12 10:56:48 -0700885 sake of backward compatibility. However, if ``find_spec()`` is
Eric Snow7cff4cd2013-12-16 23:10:50 -0700886 implemented on the path entry finder, the legacy methods are ignored.
Eric Snowb523f842013-11-22 09:05:39 -0700887
Eric Snow7cff4cd2013-12-16 23:10:50 -0700888 :meth:`~importlib.abc.PathEntryFinder.find_loader` takes one argument, the
889 fully qualified name of the module being imported. ``find_loader()``
890 returns a 2-tuple where the first item is the loader and the second item
Géry Ogam27f1bd82020-10-21 23:17:35 +0200891 is a namespace :term:`portion`.
Eric Snowb523f842013-11-22 09:05:39 -0700892
893 For backwards compatibility with other implementations of the import
894 protocol, many path entry finders also support the same,
Eric Snow7cff4cd2013-12-16 23:10:50 -0700895 traditional ``find_module()`` method that meta path finders support.
896 However path entry finder ``find_module()`` methods are never called
Eric Snowb523f842013-11-22 09:05:39 -0700897 with a ``path`` argument (they are expected to record the appropriate
898 path information from the initial call to the path hook).
899
Eric Snow7cff4cd2013-12-16 23:10:50 -0700900 The ``find_module()`` method on path entry finders is deprecated,
Eric Snowb523f842013-11-22 09:05:39 -0700901 as it does not allow the path entry finder to contribute portions to
Eric Snow7cff4cd2013-12-16 23:10:50 -0700902 namespace packages. If both ``find_loader()`` and ``find_module()``
Eric Snowb523f842013-11-22 09:05:39 -0700903 exist on a path entry finder, the import system will always call
Eric Snow7cff4cd2013-12-16 23:10:50 -0700904 ``find_loader()`` in preference to ``find_module()``.
Nick Coghlan49417742012-08-02 23:03:58 +1000905
Brett Cannona7ff6df2021-03-30 08:43:03 -0700906.. versionchanged:: 3.10
Brett Cannonf97dc802021-04-02 12:35:32 -0700907 Calls to :meth:`~importlib.abc.PathEntryFinder.find_module` and
908 :meth:`~importlib.abc.PathEntryFinder.find_loader` by the import
Brett Cannona7ff6df2021-03-30 08:43:03 -0700909 system will raise :exc:`ImportWarning`.
910
Nick Coghlan49417742012-08-02 23:03:58 +1000911
912Replacing the standard import system
913====================================
914
915The most reliable mechanism for replacing the entire import system is to
916delete the default contents of :data:`sys.meta_path`, replacing them
917entirely with a custom meta path hook.
918
919If it is acceptable to only alter the behaviour of import statements
920without affecting other APIs that access the import system, then replacing
921the builtin :func:`__import__` function may be sufficient. This technique
922may also be employed at the module level to only alter the behaviour of
923import statements within that module.
924
Shu2d56af72019-11-12 22:12:11 -0500925To selectively prevent the import of some modules from a hook early on the
Nick Coghlan49417742012-08-02 23:03:58 +1000926meta path (rather than disabling the standard import system entirely),
Dominik Miedzińskiaf34e0a2017-05-16 18:40:17 +0200927it is sufficient to raise :exc:`ModuleNotFoundError` directly from
Eric Snow7cff4cd2013-12-16 23:10:50 -0700928:meth:`~importlib.abc.MetaPathFinder.find_spec` instead of returning
929``None``. The latter indicates that the meta path search should continue,
930while raising an exception terminates it immediately.
Barry Warsawd7d21942012-07-29 16:36:17 -0400931
Joannah Nanjekye70bf7132019-04-24 11:14:44 -0400932.. _relativeimports:
933
934Package Relative Imports
935========================
936
937Relative imports use leading dots. A single leading dot indicates a relative
938import, starting with the current package. Two or more leading dots indicate a
939relative import to the parent(s) of the current package, one level per dot
940after the first. For example, given the following package layout::
941
942 package/
943 __init__.py
944 subpackage1/
945 __init__.py
946 moduleX.py
947 moduleY.py
948 subpackage2/
949 __init__.py
950 moduleZ.py
951 moduleA.py
952
953In either ``subpackage1/moduleX.py`` or ``subpackage1/__init__.py``,
954the following are valid relative imports::
955
956 from .moduleY import spam
957 from .moduleY import spam as ham
958 from . import moduleY
959 from ..subpackage1 import moduleY
960 from ..subpackage2.moduleZ import eggs
961 from ..moduleA import foo
962
963Absolute imports may use either the ``import <>`` or ``from <> import <>``
964syntax, but relative imports may only use the second form; the reason
965for this is that::
966
967 import XXX.YYY.ZZZ
968
969should expose ``XXX.YYY.ZZZ`` as a usable expression, but .moduleY is
970not a valid expression.
971
Barry Warsawd7d21942012-07-29 16:36:17 -0400972
Eric Snowe50f9aa2014-03-28 18:10:33 -0600973Special considerations for __main__
974===================================
975
976The :mod:`__main__` module is a special case relative to Python's import
977system. As noted :ref:`elsewhere <programs>`, the ``__main__`` module
978is directly initialized at interpreter startup, much like :mod:`sys` and
979:mod:`builtins`. However, unlike those two, it doesn't strictly
980qualify as a built-in module. This is because the manner in which
981``__main__`` is initialized depends on the flags and other options with
982which the interpreter is invoked.
983
984.. _main_spec:
985
986__main__.__spec__
987-----------------
988
989Depending on how :mod:`__main__` is initialized, ``__main__.__spec__``
990gets set appropriately or to ``None``.
991
992When Python is started with the :option:`-m` option, ``__spec__`` is set
Nick Coghlan9aa00d12014-03-29 15:39:42 +1000993to the module spec of the corresponding module or package. ``__spec__`` is
994also populated when the ``__main__`` module is loaded as part of executing a
995directory, zipfile or other :data:`sys.path` entry.
Eric Snowe50f9aa2014-03-28 18:10:33 -0600996
997In :ref:`the remaining cases <using-on-interface-options>`
Nick Coghlan9aa00d12014-03-29 15:39:42 +1000998``__main__.__spec__`` is set to ``None``, as the code used to populate the
999:mod:`__main__` does not correspond directly with an importable module:
Eric Snowe50f9aa2014-03-28 18:10:33 -06001000
1001- interactive prompt
Andrés Delfinoea6a28c2018-11-07 14:06:45 -03001002- :option:`-c` option
Eric Snowe50f9aa2014-03-28 18:10:33 -06001003- running from stdin
1004- running directly from a source or bytecode file
1005
Nick Coghlan9aa00d12014-03-29 15:39:42 +10001006Note that ``__main__.__spec__`` is always ``None`` in the last case,
1007*even if* the file could technically be imported directly as a module
1008instead. Use the :option:`-m` switch if valid module metadata is desired
1009in :mod:`__main__`.
1010
1011Note also that even when ``__main__`` corresponds with an importable module
1012and ``__main__.__spec__`` is set accordingly, they're still considered
1013*distinct* modules. This is due to the fact that blocks guarded by
1014``if __name__ == "__main__":`` checks only execute when the module is used
1015to populate the ``__main__`` namespace, and not during normal import.
1016
Eric Snowe50f9aa2014-03-28 18:10:33 -06001017
Barry Warsawd7d21942012-07-29 16:36:17 -04001018Open issues
1019===========
1020
Barry Warsawd7d21942012-07-29 16:36:17 -04001021XXX It would be really nice to have a diagram.
1022
Barry Warsawc1e721b2012-07-30 16:24:12 -04001023XXX * (import_machinery.rst) how about a section devoted just to the
1024attributes of modules and packages, perhaps expanding upon or supplanting the
1025related entries in the data model reference page?
1026
Barry Warsawdadebab2012-07-31 16:03:09 -04001027XXX runpy, pkgutil, et al in the library manual should all get "See Also"
1028links at the top pointing to the new import system section.
1029
Eric Snowe50f9aa2014-03-28 18:10:33 -06001030XXX Add more explanation regarding the different ways in which
1031``__main__`` is initialized?
1032
1033XXX Add more info on ``__main__`` quirks/pitfalls (i.e. copy from
1034:pep:`395`).
1035
Barry Warsawd7d21942012-07-29 16:36:17 -04001036
1037References
1038==========
1039
1040The import machinery has evolved considerably since Python's early days. The
1041original `specification for packages
Benjamin Peterson60dbed12017-09-05 16:24:39 -07001042<https://www.python.org/doc/essays/packages/>`_ is still available to read,
Barry Warsawd7d21942012-07-29 16:36:17 -04001043although some details have changed since the writing of that document.
1044
1045The original specification for :data:`sys.meta_path` was :pep:`302`, with
Barry Warsawdadebab2012-07-31 16:03:09 -04001046subsequent extension in :pep:`420`.
1047
1048:pep:`420` introduced :term:`namespace packages <namespace package>` for
1049Python 3.3. :pep:`420` also introduced the :meth:`find_loader` protocol as an
1050alternative to :meth:`find_module`.
Barry Warsawd7d21942012-07-29 16:36:17 -04001051
1052:pep:`366` describes the addition of the ``__package__`` attribute for
1053explicit relative imports in main modules.
Barry Warsawc1e721b2012-07-30 16:24:12 -04001054
Nick Coghlan1685db02012-08-20 13:49:08 +10001055:pep:`328` introduced absolute and explicit relative imports and initially
1056proposed ``__name__`` for semantics :pep:`366` would eventually specify for
Barry Warsawdadebab2012-07-31 16:03:09 -04001057``__package__``.
1058
1059:pep:`338` defines executing modules as scripts.
1060
Eric Snowb523f842013-11-22 09:05:39 -07001061:pep:`451` adds the encapsulation of per-module import state in spec
1062objects. It also off-loads most of the boilerplate responsibilities of
1063loaders back onto the import machinery. These changes allow the
1064deprecation of several APIs in the import system and also addition of new
1065methods to finders and loaders.
Barry Warsawc1e721b2012-07-30 16:24:12 -04001066
Georg Brandl44ea77b2013-03-28 13:28:44 +01001067.. rubric:: Footnotes
Barry Warsawc1e721b2012-07-30 16:24:12 -04001068
1069.. [#fnmo] See :class:`types.ModuleType`.
1070
Nick Coghlan1685db02012-08-20 13:49:08 +10001071.. [#fnlo] The importlib implementation avoids using the return value
Barry Warsawc1e721b2012-07-30 16:24:12 -04001072 directly. Instead, it gets the module object by looking the module name up
Nick Coghlan1685db02012-08-20 13:49:08 +10001073 in :data:`sys.modules`. The indirect effect of this is that an imported
Barry Warsawc1e721b2012-07-30 16:24:12 -04001074 module may replace itself in :data:`sys.modules`. This is
1075 implementation-specific behavior that is not guaranteed to work in other
1076 Python implementations.
1077
Barry Warsawc1e721b2012-07-30 16:24:12 -04001078.. [#fnpic] In legacy code, it is possible to find instances of
1079 :class:`imp.NullImporter` in the :data:`sys.path_importer_cache`. It
Nick Coghlan1685db02012-08-20 13:49:08 +10001080 is recommended that code be changed to use ``None`` instead. See
Barry Warsawc1e721b2012-07-30 16:24:12 -04001081 :ref:`portingpythoncode` for more details.