blob: 50c041cfe2f994cdf18590abd9d091a43016e817 [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
18scope. The search operation of the :keyword:`import` statement is defined as
Barry Warsawc1e721b2012-07-30 16:24:12 -040019a call to the built-in :func:`__import__` function, with the appropriate
20arguments. The return value of :func:`__import__` is used to perform the name
21binding operation of the :keyword:`import` statement. See the
22:keyword:`import` statement for the exact details of that name binding
23operation.
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
31When a module is first imported, Python searches for the module and if found,
Barry Warsawc1e721b2012-07-30 16:24:12 -040032it creates a module object [#fnmo]_, initializing it. If the named module
33cannot be found, an :exc:`ImportError` is raised. Python implements various
34strategies to search for the named module when the import machinery is
35invoked. These strategies can be modified and extended by using various hooks
Barry Warsawdadebab2012-07-31 16:03:09 -040036described in the sections below. More coarse-grained overriding of the import
37system can be accomplished by replacing built-in :func:`__import__`.
Barry Warsawc1e721b2012-07-30 16:24:12 -040038
39
40:mod:`importlib`
41================
42
43The :mod:`importlib` module provides a rich API for interacting with the
44import system. For example :func:`importlib.import_module` provides a
45recommended, simpler API than built-in :func:`__import__` for invoking the
46import machinery. Refer to the :mod:`importlib` library documentation for
47additional detail.
48
Barry Warsawd7d21942012-07-29 16:36:17 -040049
50
51Packages
52========
53
54.. index::
55 single: package
56
57Python has only one type of module object, and all modules are of this type,
58regardless of whether the module is implemented in Python, C, or something
59else. To help organize modules and provide a naming hierarchy, Python has a
Barry Warsawc1e721b2012-07-30 16:24:12 -040060concept of :term:`packages <package>`.
Barry Warsawd7d21942012-07-29 16:36:17 -040061
Barry Warsawc1e721b2012-07-30 16:24:12 -040062You can think of packages as the directories on a file system and modules as
63files within directories, but don't take this analogy too literally since
64packages and modules need not originate from the file system. For the
65purposes of this documentation, we'll use this convenient analogy of
66directories and files. Like file system directories, packages are organized
67hierarchically, and packages may themselves contain subpackages, as well as
68regular modules.
Barry Warsawd7d21942012-07-29 16:36:17 -040069
Barry Warsawc1e721b2012-07-30 16:24:12 -040070It's important to keep in mind that all packages are modules, but not all
71modules are packages. Or put another way, packages are just a special kind of
72module. Specifically, any module that contains an ``__path__`` attribute is
73considered a package.
74
75All modules have a name. Subpackage names are separated from their parent
76package name by dots, akin to Python's standard attribute access syntax. Thus
77you might have a module called :mod:`sys` and a package called :mod:`email`,
78which in turn has a subpackage called :mod:`email.mime` and a module within
79that subpackage called :mod:`email.mime.text`.
Barry Warsawd7d21942012-07-29 16:36:17 -040080
81
82Regular packages
83----------------
84
85.. index::
86 pair: package; regular
87
88Python defines two types of packages, :term:`regular packages <regular
89package>` and :term:`namespace packages <namespace package>`. Regular
90packages are traditional packages as they existed in Python 3.2 and earlier.
91A regular package is typically implemented as a directory containing an
92``__init__.py`` file. When a regular package is imported, this
93``__init__.py`` file is implicitly imported, and the objects it defines are
94bound to names in the package's namespace. The ``__init__.py`` file can
95contain the same Python code that any other module can contain, and Python
96will add some additional attributes to the module when it is imported.
97
Barry Warsawd7d21942012-07-29 16:36:17 -040098For example, the following file system layout defines a top level ``parent``
99package with three subpackages::
100
101 parent/
102 __init__.py
103 one/
104 __init__.py
105 two/
106 __init__.py
107 three/
108 __init__.py
109
110Importing ``parent.one`` will implicitly import ``parent/__init__.py`` and
111``parent/one/__init__.py``. Subsequent imports of ``parent.two`` or
112``parent.three`` will import ``parent/two/__init__.py`` and
113``parent/three/__init__.py`` respectively.
114
Barry Warsawc1e721b2012-07-30 16:24:12 -0400115
116Namespace packages
117------------------
118
119.. index::
120 pair:: package; namespace
121 pair:: package; portion
122
123A namespace package is a composite of various :term:`portions <portion>`,
124where each portion contributes a subpackage to the parent package. Portions
125may reside in different locations on the file system. Portions may also be
126found in zip files, on the network, or anywhere else that Python searches
127during import. Namespace packages may or may not correspond directly to
128objects on the file system; they may be virtual modules that have no concrete
129representation.
130
Barry Warsawd7d21942012-07-29 16:36:17 -0400131With namespace packages, there is no ``parent/__init__.py`` file. In fact,
132there may be multiple ``parent`` directories found during import search, where
Barry Warsawc1e721b2012-07-30 16:24:12 -0400133each one is provided by a different portion. Thus ``parent/one`` may not be
Barry Warsawd7d21942012-07-29 16:36:17 -0400134physically located next to ``parent/two``. In this case, Python will create a
135namespace package for the top-level ``parent`` package whenever it or one of
136its subpackages is imported.
137
Barry Warsawc1e721b2012-07-30 16:24:12 -0400138See also :pep:`420` for the namespace package specification.
139
Barry Warsawd7d21942012-07-29 16:36:17 -0400140
141Searching
142=========
143
144To begin the search, Python needs the :term:`fully qualified <qualified name>`
145name of the module (or package, but for the purposes of this discussion, the
146difference is immaterial) being imported. This name may come from various
147arguments to the :keyword:`import` statement, or from the parameters to the
Barry Warsawc1e721b2012-07-30 16:24:12 -0400148:func:`importlib.import_module` or :func:`__import__` functions.
Barry Warsawd7d21942012-07-29 16:36:17 -0400149
150This name will be used in various phases of the import search, and it may be
151the dotted path to a submodule, e.g. ``foo.bar.baz``. In this case, Python
152first tries to import ``foo``, then ``foo.bar``, and finally ``foo.bar.baz``.
153If any of the intermediate imports fail, an :exc:`ImportError` is raised.
154
155
156The module cache
157----------------
158
159.. index::
160 single: sys.modules
161
162The first place checked during import search is :data:`sys.modules`. This
163mapping serves as a cache of all modules that have been previously imported,
164including the intermediate paths. So if ``foo.bar.baz`` was previously
165imported, :data:`sys.modules` will contain entries for ``foo``, ``foo.bar``,
166and ``foo.bar.baz``. Each key will have as its value the corresponding module
167object.
168
169During import, the module name is looked up in :data:`sys.modules` and if
170present, the associated value is the module satisfying the import, and the
171process completes. However, if the value is ``None``, then an
172:exc:`ImportError` is raised. If the module name is missing, Python will
173continue searching for the module.
174
Barry Warsawc1e721b2012-07-30 16:24:12 -0400175:data:`sys.modules` is writable. Deleting a key will not destroy the
176associated module, but it will invalidate the cache entry for the named
Barry Warsawd7d21942012-07-29 16:36:17 -0400177module, causing Python to search anew for the named module upon its next
178import. Beware though, because if you keep a reference to the module object,
179invalidate its cache entry in :data:`sys.modules`, and then re-import the
180named module, the two module objects will *not* be the same. The key can also
181be assigned to ``None``, forcing the next import of the module to result in an
182:exc:`ImportError`.
183
184
185Finders and loaders
186-------------------
187
188.. index::
189 single: finder
190 single: loader
191
Barry Warsawdadebab2012-07-31 16:03:09 -0400192If the named module is not found in :data:`sys.modules`, then Python's import
193protocol is invoked to find and load the module. This protocol consists of
194two conceptual objects, :term:`finders <finder>` and :term:`loaders <loader>`.
195A finder's job is to determine whether it can find the named module using
196whatever strategy it knows about.
197
198By default, Python comes with several default finders. One knows how to
199locate frozen modules, and another knows how to locate built-in modules. A
200third default finder searches an :term:`import path` for modules. The
201:term:`import path` is a list of locations that may name file system paths or
202zip files. It can also be extended to search for any locatable resource, such
203as those identified by URLs.
204
205The import machinery is extensible, so new finders can be added to extend the
206range and scope of module searching.
Barry Warsawd7d21942012-07-29 16:36:17 -0400207
208Finders do not actually load modules. If they can find the named module, they
Barry Warsawdadebab2012-07-31 16:03:09 -0400209return a :term:`loader`, which the import machinery then invokes to load the
210module and create the corresponding module object.
Barry Warsawd7d21942012-07-29 16:36:17 -0400211
212The following sections describe the protocol for finders and loaders in more
213detail, including how you can create and register new ones to extend the
214import machinery.
215
216
217Import hooks
218------------
219
220.. index::
221 single: import hooks
222 single: meta hooks
223 single: path hooks
224 pair: hooks; import
225 pair: hooks; meta
226 pair: hooks; path
227
228The import machinery is designed to be extensible; the primary mechanism for
229this are the *import hooks*. There are two types of import hooks: *meta
Barry Warsawdadebab2012-07-31 16:03:09 -0400230hooks* and *import path hooks*.
Barry Warsawd7d21942012-07-29 16:36:17 -0400231
232Meta hooks are called at the start of import processing, before any other
Barry Warsawdadebab2012-07-31 16:03:09 -0400233import processing has occurred, other than :data:`sys.modules` cache look up.
234This allows meta hooks to override :data:`sys.path` processing, frozen
235modules, or even built-in modules. Meta hooks are registered by adding new
236finder objects to :data:`sys.meta_path`, as described below.
Barry Warsawd7d21942012-07-29 16:36:17 -0400237
Barry Warsawdadebab2012-07-31 16:03:09 -0400238Import path hooks are called as part of :data:`sys.path` (or
239``package.__path__``) processing, at the point where their associated path
240item is encountered. Import path hooks are registered by adding new callables
241to :data:`sys.path_hooks` as described below.
Barry Warsawd7d21942012-07-29 16:36:17 -0400242
243
244The meta path
245-------------
246
247.. index::
248 single: sys.meta_path
249 pair: finder; find_module
250 pair: finder; find_loader
251
252When the named module is not found in :data:`sys.modules`, Python next
253searches :data:`sys.meta_path`, which contains a list of meta path finder
254objects. These finders are queried in order to see if they know how to handle
255the named module. Meta path finders must implement a method called
Barry Warsawdadebab2012-07-31 16:03:09 -0400256:meth:`find_module()` which takes two arguments, a name and an import path.
257The meta path finder can use any strategy it wants to determine whether it can
258handle the named module or not.
Barry Warsawd7d21942012-07-29 16:36:17 -0400259
260If the meta path finder knows how to handle the named module, it returns a
261loader object. If it cannot handle the named module, it returns ``None``. If
262:data:`sys.meta_path` processing reaches the end of its list without returning
263a loader, then an :exc:`ImportError` is raised. Any other exceptions raised
264are simply propagated up, aborting the import process.
265
266The :meth:`find_module()` method of meta path finders is called with two
267arguments. The first is the fully qualified name of the module being
268imported, for example ``foo.bar.baz``. The second argument is the relative
Barry Warsawdadebab2012-07-31 16:03:09 -0400269import path for the module search. For top-level modules, this second
270argument will always be ``None``, but for submodules or subpackages, the
271second argument is the value of the parent package's ``__path__`` attribute,
272which must exist on the parent module or an :exc:`ImportError` is raised.
Barry Warsawd7d21942012-07-29 16:36:17 -0400273
274Python's default :data:`sys.meta_path` has three meta path finders, one that
275knows how to import built-in modules, one that knows how to import frozen
Barry Warsawdadebab2012-07-31 16:03:09 -0400276modules, and one that knows how to import modules from an :term:`import path`
Barry Warsawd7d21942012-07-29 16:36:17 -0400277(i.e. the :term:`path importer`).
278
279
Barry Warsawdadebab2012-07-31 16:03:09 -0400280Loaders
281=======
Barry Warsawd7d21942012-07-29 16:36:17 -0400282
Barry Warsawdadebab2012-07-31 16:03:09 -0400283If and when a module loader is found its
Barry Warsawc1e721b2012-07-30 16:24:12 -0400284:meth:`~importlib.abc.Loader.load_module` method is called, with a single
285argument, the fully qualified name of the module being imported. This method
286has several responsibilities, and should return the module object it has
287loaded [#fnlo]_. If it cannot load the module, it should raise an
288:exc:`ImportError`, although any other exception raised during
289:meth:`load_module()` will be propagated.
Barry Warsawd7d21942012-07-29 16:36:17 -0400290
Barry Warsawdadebab2012-07-31 16:03:09 -0400291In many cases, the finder and loader can be the same object; in such cases the
292:meth:`finder.find_module()` would just return ``self``.
Barry Warsawd7d21942012-07-29 16:36:17 -0400293
294Loaders must satisfy the following requirements:
295
296 * If there is an existing module object with the given name in
297 :data:`sys.modules`, the loader must use that existing module. (Otherwise,
Barry Warsawc1e721b2012-07-30 16:24:12 -0400298 the :func:`imp.reload` will not work correctly.) If the named module does
299 not exist in :data:`sys.modules`, the loader must create a new module
Barry Warsawd7d21942012-07-29 16:36:17 -0400300 object and add it to :data:`sys.modules`.
301
302 Note that the module *must* exist in :data:`sys.modules` before the loader
303 executes the module code. This is crucial because the module code may
304 (directly or indirectly) import itself; adding it to :data:`sys.modules`
305 beforehand prevents unbounded recursion in the worst case and multiple
306 loading in the best.
307
Barry Warsawdadebab2012-07-31 16:03:09 -0400308 If loading fails, the loader must remove any modules it has inserted into
309 :data:`sys.modules`, but it must remove **only** the failing module, and
310 only if the loader itself has loaded it explicitly. Any module already in
311 the :data:`sys.modules` cache, and any module that was successfully loaded
312 as a side-effect, must remain in the cache.
Barry Warsawd7d21942012-07-29 16:36:17 -0400313
314 * The loader may set the ``__file__`` attribute of the module. If set, this
315 attribute's value must be a string. The loader may opt to leave
316 ``__file__`` unset if it has no semantic meaning (e.g. a module loaded from
317 a database).
318
319 * The loader may set the ``__name__`` attribute of the module. While not
320 required, setting this attribute is highly recommended so that the
321 :meth:`repr()` of the module is more informative.
322
323 * If module is a package (either regular or namespace), the loader must set
324 the module object's ``__path__`` attribute. The value must be a list, but
325 may be empty if ``__path__`` has no further significance to the importer.
326 More details on the semantics of ``__path__`` are given below.
327
328 * The ``__loader__`` attribute must be set to the loader object that loaded
329 the module. This is mostly for introspection and reloading, but can be
330 used for additional importer-specific functionality, for example getting
331 data associated with an importer.
332
333 * The module's ``__package__`` attribute should be set. Its value must be a
Barry Warsawdadebab2012-07-31 16:03:09 -0400334 string, but it can be the same value as its ``__name__``. If the attribute
335 is set to ``None`` or is missing, the import system will fill it in with a
336 more appropriate value. When the module is a package, its ``__package__``
337 value should be set to its ``__name__``. When the module is not a package,
338 ``__package__`` should be set to the empty string for top-level modules, or
339 for submodules, to the parent package's name. See :pep:`366` for further
340 details.
Barry Warsawd7d21942012-07-29 16:36:17 -0400341
342 This attribute is used instead of ``__name__`` to calculate explicit
343 relative imports for main modules, as defined in :pep:`366`.
344
345 * If the module is a Python module (as opposed to a built-in module or a
Barry Warsawc1e721b2012-07-30 16:24:12 -0400346 dynamically loaded extension), the loader should execute the module's code
347 in the module's global name space (``module.__dict__``).
Barry Warsawd7d21942012-07-29 16:36:17 -0400348
349
350Module reprs
351------------
352
353By default, all modules have a usable repr, however depending on the
Barry Warsawc1e721b2012-07-30 16:24:12 -0400354attributes set above, and hooks in the loader, you can more explicitly control
Barry Warsawd7d21942012-07-29 16:36:17 -0400355the repr of module objects.
356
357Loaders may implement a :meth:`module_repr()` method which takes a single
358argument, the module object. When ``repr(module)`` is called for a module
359with a loader supporting this protocol, whatever is returned from
Barry Warsawc1e721b2012-07-30 16:24:12 -0400360``module.__loader__.module_repr(module)`` is returned as the module's repr
361without further processing. This return value must be a string.
Barry Warsawd7d21942012-07-29 16:36:17 -0400362
363If the module has no ``__loader__`` attribute, or the loader has no
364:meth:`module_repr()` method, then the module object implementation itself
365will craft a default repr using whatever information is available. It will
366try to use the ``module.__name__``, ``module.__file__``, and
367``module.__loader__`` as input into the repr, with defaults for whatever
368information is missing.
369
370Here are the exact rules used:
371
372 * If the module has an ``__loader__`` and that loader has a
373 :meth:`module_repr()` method, call it with a single argument, which is the
374 module object. The value returned is used as the module's repr.
375
376 * If an exception occurs in :meth:`module_repr()`, the exception is caught
377 and discarded, and the calculation of the module's repr continues as if
378 :meth:`module_repr()` did not exist.
379
380 * If the module has an ``__file__`` attribute, this is used as part of the
381 module's repr.
382
383 * If the module has no ``__file__`` but does have an ``__loader__``, then the
384 loader's repr is used as part of the module's repr.
385
386 * Otherwise, just use the module's ``__name__`` in the repr.
387
388This example, from :pep:`420` shows how a loader can craft its own module
389repr::
390
391 class NamespaceLoader:
392 @classmethod
393 def module_repr(cls, module):
394 return "<module '{}' (namespace)>".format(module.__name__)
395
396
397module.__path__
398---------------
399
400By definition, if a module has an ``__path__`` attribute, it is a package,
401regardless of its value.
402
403A package's ``__path__`` attribute is used during imports of its subpackages.
404Within the import machinery, it functions much the same as :data:`sys.path`,
405i.e. providing a list of locations to search for modules during import.
406However, ``__path__`` is typically much more constrained than
407:data:`sys.path`.
408
409``__path__`` must be a list, but it may be empty. The same rules used for
410:data:`sys.path` also apply to a package's ``__path__``, and
Barry Warsawc1e721b2012-07-30 16:24:12 -0400411:data:`sys.path_hooks` (described below) is consulted when traversing a
Barry Warsawd7d21942012-07-29 16:36:17 -0400412package's ``__path__``.
413
414A package's ``__init__.py`` file may set or alter the package's ``__path__``
415attribute, and this was typically the way namespace packages were implemented
416prior to :pep:`420`. With the adoption of :pep:`420`, namespace packages no
417longer need to supply ``__init__.py`` files containing only ``__path__``
418manipulation code; the namespace loader automatically sets ``__path__``
419correctly for the namespace package.
420
421
422The Path Importer
423=================
424
425.. index::
426 single: path importer
427
428As mentioned previously, Python comes with several default meta path finders.
Barry Warsawdadebab2012-07-31 16:03:09 -0400429One of these, called the :term:`path importer`, searches an :term:`import
430path`, which contains a list of :term:`path entries <path entry>`. Each path
431entry names a location to search for modules.
Barry Warsawd7d21942012-07-29 16:36:17 -0400432
Barry Warsawdadebab2012-07-31 16:03:09 -0400433Path entries may name file system locations, and by default the :term:`path
434importer` knows how to provide traditional file system imports. It implements
435all the semantics for finding modules on the file system, handling special
436file types such as Python source code (``.py`` files), Python byte code
437(``.pyc`` and ``.pyo`` files) and shared libraries (e.g. ``.so`` files).
438
439Path entries need not be limited to file system locations. They can refer to
440the contents of zip files, URLs, database queries, or any other location that
441can be specified as a string.
442
443The :term:`path importer` provides additional hooks and protocols so that you
444can extend and customize the types of searchable path entries. For example,
445if you wanted to support path entries as network URLs, you could write a hook
446that implements HTTP semantics to find modules on the web. This hook (a
447callable) would return a :term:`path entry finder` supporting the protocol
448described below, which was then used to get a loader for the module from the
449web.
Barry Warsawd7d21942012-07-29 16:36:17 -0400450
451A word of warning: this section and the previous both use the term *finder*,
452distinguishing between them by using the terms :term:`meta path finder` and
Barry Warsawdadebab2012-07-31 16:03:09 -0400453:term:`path entry finder`. These two types of finders are very similar,
454support similar protocols, and function in similar ways during the import
455process, but it's important to keep in mind that they are subtly different.
456In particular, meta path finders operate at the beginning of the import
457process, as keyed off the :data:`sys.meta_path` traversal.
Barry Warsawd7d21942012-07-29 16:36:17 -0400458
Barry Warsawdadebab2012-07-31 16:03:09 -0400459On the other hand, path entry finders are in a sense an implementation detail
460of the :term:`path importer`, and in fact, if the path importer were to be
461removed from :data:`sys.meta_path`, none of the path entry finder semantics
462would be invoked.
Barry Warsawd7d21942012-07-29 16:36:17 -0400463
464
Barry Warsawdadebab2012-07-31 16:03:09 -0400465Path entry finders
466------------------
Barry Warsawd7d21942012-07-29 16:36:17 -0400467
468.. index::
469 single: sys.path
470 single: sys.path_hooks
471 single: sys.path_importer_cache
472 single: PYTHONPATH
473
Barry Warsawdadebab2012-07-31 16:03:09 -0400474The :term:`path importer` is responsible for finding and loading Python
475modules and packages whose location is specified with a string :term:`path
476entry`. Most path entries name locations in the file system, but they need
477not be limited to this.
478
479As a meta path finder, the :term:`path importer` implements the
Barry Warsawd7d21942012-07-29 16:36:17 -0400480:meth:`find_module()` protocol previously described, however it exposes
481additional hooks that can be used to customize how modules are found and
Barry Warsawdadebab2012-07-31 16:03:09 -0400482loaded from the :term:`import path`.
Barry Warsawd7d21942012-07-29 16:36:17 -0400483
Barry Warsawdadebab2012-07-31 16:03:09 -0400484Three variables are used by the :term:`path importer`, :data:`sys.path`,
485:data:`sys.path_hooks` and :data:`sys.path_importer_cache`. The ``__path__``
486attribute on package objects is also used. These provide additional ways that
487the import machinery can be customized.
Barry Warsawd7d21942012-07-29 16:36:17 -0400488
489:data:`sys.path` contains a list of strings providing search locations for
490modules and packages. It is initialized from the :data:`PYTHONPATH`
491environment variable and various other installation- and
492implementation-specific defaults. Entries in :data:`sys.path` can name
493directories on the file system, zip files, and potentially other "locations"
Barry Warsawdadebab2012-07-31 16:03:09 -0400494(see the :mod:`site` module) that should be searched for modules, such as
495URLs, or database queries.
Barry Warsawd7d21942012-07-29 16:36:17 -0400496
Barry Warsawdadebab2012-07-31 16:03:09 -0400497The :term:`path importer` is a :term:`meta path finder`, so the import
498machinery begins :term:`import path` search by calling the path importer's
499:meth:`find_module()` method as described previously. When the ``path``
500argument to :meth:`find_module()` is given, it will be a list of string paths
501to traverse. If the ``path`` argument is not given or is ``None``,
Barry Warsawd7d21942012-07-29 16:36:17 -0400502:data:`sys.path` is used.
503
Barry Warsawdadebab2012-07-31 16:03:09 -0400504The :term:`path importer` iterates over every entry in the search path, and
505for each of these, looks for an appropriate :term:`path entry finder` for the
506path entry. Because this can be an expensive operation (e.g. there may be
507`stat()` call overheads for this search), the :term:`path importer` maintains
508a cache mapping path entries to path entry finders. This cache is maintained
509in :data:`sys.path_importer_cache`. In this way, the expensive search for a
510particular :term:`path entry` location's :term:`path entry finder` need only
511be done once. User code is free to remove cache entries from
512:data:`sys.path_importer_cache` forcing the :term:`path importer` to perform
513the path entry search again [#fnpic]_.
Barry Warsawd7d21942012-07-29 16:36:17 -0400514
515If the path entry is not present in the cache, the path importer iterates over
Barry Warsawdadebab2012-07-31 16:03:09 -0400516every callable in :data:`sys.path_hooks`. Each of the :term:`path entry hooks
517<path entry hook>` in this list is called with a single argument, the path
518entry being searched. This callable may either return a :term:`path entry
519finder` that can handle the path entry, or it may raise :exc:`ImportError`.
520An :exc:`ImportError` is used by the path importer to signal that the hook
521cannot find a :term:`path entry finder` for that :term:`path entry`. The
522exception is ignored and :term:`import path` iteration continues.
Barry Warsawd7d21942012-07-29 16:36:17 -0400523
Barry Warsawdadebab2012-07-31 16:03:09 -0400524If :data:`sys.path_hooks` iteration ends with no :term:`path entry finder`
525being returned, then the path importer's :meth:`find_module()` method will
526return ``None``, indicating that this :term:`meta path finder` could not find
527the module.
Barry Warsawd7d21942012-07-29 16:36:17 -0400528
Barry Warsawdadebab2012-07-31 16:03:09 -0400529If a :term:`path entry finder` *is* returned by one of the :term:`path entry
530hook` callables on :data:`sys.path_hooks`, then the following protocol is used
531to ask the finder for a module loader, which is then used to load the module.
Barry Warsawd7d21942012-07-29 16:36:17 -0400532
533
Barry Warsawdadebab2012-07-31 16:03:09 -0400534Path entry finder protocol
535--------------------------
Barry Warsawd7d21942012-07-29 16:36:17 -0400536
Barry Warsawdadebab2012-07-31 16:03:09 -0400537Path entry finders support the same :meth:`find_module()` method that meta
538path finders support, however path entry finder's :meth:`find_module()`
Barry Warsawd7d21942012-07-29 16:36:17 -0400539methods are never called with a ``path`` argument.
540
Barry Warsawdadebab2012-07-31 16:03:09 -0400541The :meth:`find_module()` method on path entry finders is deprecated though,
542and instead path entry finders should implement the :meth:`find_loader()`
543method. If it exists on the path entry finder, :meth:`find_loader()` will
544always be called instead of :meth:`find_module()`.
Barry Warsawd7d21942012-07-29 16:36:17 -0400545
546:meth:`find_loader()` takes one argument, the fully qualified name of the
547module being imported. :meth:`find_loader()` returns a 2-tuple where the
548first item is the loader and the second item is a namespace :term:`portion`.
549When the first item (i.e. the loader) is ``None``, this means that while the
Barry Warsawdadebab2012-07-31 16:03:09 -0400550path entry finder does not have a loader for the named module, it knows that
551the :term:`path entry` contributes to a namespace portion for the named
552module. This will almost always be the case where Python is asked to import a
553:term:`namespace package` that has no physical presence on the file system.
554When a path entry finder returns ``None`` for the loader, the second item of
555the 2-tuple return value must be a sequence, although it can be empty.
Barry Warsawd7d21942012-07-29 16:36:17 -0400556
557If :meth:`find_loader()` returns a non-``None`` loader value, the portion is
Barry Warsawdadebab2012-07-31 16:03:09 -0400558ignored and the loader is returned from the :term:`path importer`, terminating
559the :term:`import path` search.
Barry Warsawd7d21942012-07-29 16:36:17 -0400560
561
562Open issues
563===========
564
Barry Warsawd7d21942012-07-29 16:36:17 -0400565XXX It would be really nice to have a diagram.
566
Barry Warsawc1e721b2012-07-30 16:24:12 -0400567XXX * (import_machinery.rst) how about a section devoted just to the
568attributes of modules and packages, perhaps expanding upon or supplanting the
569related entries in the data model reference page?
570
Barry Warsawc1e721b2012-07-30 16:24:12 -0400571XXX Module reprs: how does module.__qualname__ fit in?
Barry Warsawd7d21942012-07-29 16:36:17 -0400572
Barry Warsawdadebab2012-07-31 16:03:09 -0400573XXX runpy, pkgutil, et al in the library manual should all get "See Also"
574links at the top pointing to the new import system section.
575
Barry Warsawd7d21942012-07-29 16:36:17 -0400576
577References
578==========
579
580The import machinery has evolved considerably since Python's early days. The
581original `specification for packages
582<http://www.python.org/doc/essays/packages.html>`_ is still available to read,
583although some details have changed since the writing of that document.
584
585The original specification for :data:`sys.meta_path` was :pep:`302`, with
Barry Warsawdadebab2012-07-31 16:03:09 -0400586subsequent extension in :pep:`420`.
587
588:pep:`420` introduced :term:`namespace packages <namespace package>` for
589Python 3.3. :pep:`420` also introduced the :meth:`find_loader` protocol as an
590alternative to :meth:`find_module`.
Barry Warsawd7d21942012-07-29 16:36:17 -0400591
592:pep:`366` describes the addition of the ``__package__`` attribute for
593explicit relative imports in main modules.
Barry Warsawc1e721b2012-07-30 16:24:12 -0400594
Barry Warsawdadebab2012-07-31 16:03:09 -0400595:pep:`328` introduced absolute and relative imports and initially proposed
596``__name__`` for semantics :pep:`366` would eventually specify for
597``__package__``.
598
599:pep:`338` defines executing modules as scripts.
600
Barry Warsawc1e721b2012-07-30 16:24:12 -0400601
602Footnotes
603=========
604
605.. [#fnmo] See :class:`types.ModuleType`.
606
607.. [#fnlo] The importlib implementation appears not to use the return value
608 directly. Instead, it gets the module object by looking the module name up
609 in :data:`sys.modules`.) The indirect effect of this is that an imported
610 module may replace itself in :data:`sys.modules`. This is
611 implementation-specific behavior that is not guaranteed to work in other
612 Python implementations.
613
Barry Warsawc1e721b2012-07-30 16:24:12 -0400614.. [#fnpic] In legacy code, it is possible to find instances of
615 :class:`imp.NullImporter` in the :data:`sys.path_importer_cache`. It
616 recommended that code be changed to use ``None`` instead. See
617 :ref:`portingpythoncode` for more details.