Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 1 | |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 2 | .. _importsystem: |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 3 | |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 4 | ***************** |
| 5 | The import system |
| 6 | ***************** |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 7 | |
| 8 | .. index:: single: import machinery |
| 9 | |
| 10 | Python code in one :term:`module` gains access to the code in another module |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 11 | by the process of :term:`importing` it. The :keyword:`import` statement is |
| 12 | the most common way of invoking the import machinery, but it is not the only |
| 13 | way. Functions such as :func:`importlib.import_module` and built-in |
| 14 | :func:`__import__` can also be used to invoke the import machinery. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 15 | |
| 16 | The :keyword:`import` statement combines two operations; it searches for the |
| 17 | named module, then it binds the results of that search to a name in the local |
Serhiy Storchaka | 2b57c43 | 2018-12-19 08:09:46 +0200 | [diff] [blame] | 18 | scope. The search operation of the :keyword:`!import` statement is defined as |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 19 | a call to the :func:`__import__` function, with the appropriate arguments. |
| 20 | The return value of :func:`__import__` is used to perform the name |
Serhiy Storchaka | 2b57c43 | 2018-12-19 08:09:46 +0200 | [diff] [blame] | 21 | binding operation of the :keyword:`!import` statement. See the |
| 22 | :keyword:`!import` statement for the exact details of that name binding |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 23 | operation. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 24 | |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 25 | A direct call to :func:`__import__` performs only the module search and, if |
| 26 | found, the module creation operation. While certain side-effects may occur, |
| 27 | such as the importing of parent packages, and the updating of various caches |
| 28 | (including :data:`sys.modules`), only the :keyword:`import` statement performs |
| 29 | a name binding operation. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 30 | |
Barry Warsaw | 93952f8 | 2017-11-03 13:45:46 -0700 | [diff] [blame] | 31 | When an :keyword:`import` statement is executed, the standard builtin |
| 32 | :func:`__import__` function is called. Other mechanisms for invoking the |
| 33 | import system (such as :func:`importlib.import_module`) may choose to bypass |
| 34 | :func:`__import__` and use their own solutions to implement import semantics. |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 35 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 36 | When a module is first imported, Python searches for the module and if found, |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 37 | it creates a module object [#fnmo]_, initializing it. If the named module |
kms70847 | 9c972b5 | 2017-05-30 12:12:33 -0400 | [diff] [blame] | 38 | cannot be found, a :exc:`ModuleNotFoundError` is raised. Python implements various |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 39 | strategies to search for the named module when the import machinery is |
| 40 | invoked. These strategies can be modified and extended by using various hooks |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 41 | described in the sections below. |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 42 | |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 43 | .. versionchanged:: 3.3 |
| 44 | The import system has been updated to fully implement the second phase |
Andrew Svetlov | e2cf03e | 2012-11-15 16:28:21 +0200 | [diff] [blame] | 45 | of :pep:`302`. There is no longer any implicit import machinery - the full |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 46 | import system is exposed through :data:`sys.meta_path`. In addition, |
Andrew Svetlov | e2cf03e | 2012-11-15 16:28:21 +0200 | [diff] [blame] | 47 | native namespace package support has been implemented (see :pep:`420`). |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 48 | |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 49 | |
| 50 | :mod:`importlib` |
| 51 | ================ |
| 52 | |
| 53 | The :mod:`importlib` module provides a rich API for interacting with the |
| 54 | import system. For example :func:`importlib.import_module` provides a |
| 55 | recommended, simpler API than built-in :func:`__import__` for invoking the |
| 56 | import machinery. Refer to the :mod:`importlib` library documentation for |
| 57 | additional detail. |
| 58 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 59 | |
| 60 | |
| 61 | Packages |
| 62 | ======== |
| 63 | |
| 64 | .. index:: |
| 65 | single: package |
| 66 | |
| 67 | Python has only one type of module object, and all modules are of this type, |
| 68 | regardless of whether the module is implemented in Python, C, or something |
| 69 | else. To help organize modules and provide a naming hierarchy, Python has a |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 70 | concept of :term:`packages <package>`. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 71 | |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 72 | You can think of packages as the directories on a file system and modules as |
| 73 | files within directories, but don't take this analogy too literally since |
| 74 | packages and modules need not originate from the file system. For the |
| 75 | purposes of this documentation, we'll use this convenient analogy of |
| 76 | directories and files. Like file system directories, packages are organized |
| 77 | hierarchically, and packages may themselves contain subpackages, as well as |
| 78 | regular modules. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 79 | |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 80 | It's important to keep in mind that all packages are modules, but not all |
| 81 | modules are packages. Or put another way, packages are just a special kind of |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 82 | module. Specifically, any module that contains a ``__path__`` attribute is |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 83 | considered a package. |
| 84 | |
| 85 | All modules have a name. Subpackage names are separated from their parent |
| 86 | package name by dots, akin to Python's standard attribute access syntax. Thus |
| 87 | you might have a module called :mod:`sys` and a package called :mod:`email`, |
| 88 | which in turn has a subpackage called :mod:`email.mime` and a module within |
| 89 | that subpackage called :mod:`email.mime.text`. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 90 | |
| 91 | |
| 92 | Regular packages |
| 93 | ---------------- |
| 94 | |
| 95 | .. index:: |
| 96 | pair: package; regular |
| 97 | |
| 98 | Python defines two types of packages, :term:`regular packages <regular |
| 99 | package>` and :term:`namespace packages <namespace package>`. Regular |
| 100 | packages are traditional packages as they existed in Python 3.2 and earlier. |
| 101 | A regular package is typically implemented as a directory containing an |
| 102 | ``__init__.py`` file. When a regular package is imported, this |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 103 | ``__init__.py`` file is implicitly executed, and the objects it defines are |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 104 | bound to names in the package's namespace. The ``__init__.py`` file can |
| 105 | contain the same Python code that any other module can contain, and Python |
| 106 | will add some additional attributes to the module when it is imported. |
| 107 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 108 | For example, the following file system layout defines a top level ``parent`` |
| 109 | package 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 Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 120 | Importing ``parent.one`` will implicitly execute ``parent/__init__.py`` and |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 121 | ``parent/one/__init__.py``. Subsequent imports of ``parent.two`` or |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 122 | ``parent.three`` will execute ``parent/two/__init__.py`` and |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 123 | ``parent/three/__init__.py`` respectively. |
| 124 | |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 125 | |
| 126 | Namespace packages |
| 127 | ------------------ |
| 128 | |
| 129 | .. index:: |
Serhiy Storchaka | ddb961d | 2018-10-26 09:00:49 +0300 | [diff] [blame] | 130 | pair: package; namespace |
| 131 | pair: package; portion |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 132 | |
| 133 | A namespace package is a composite of various :term:`portions <portion>`, |
| 134 | where each portion contributes a subpackage to the parent package. Portions |
| 135 | may reside in different locations on the file system. Portions may also be |
| 136 | found in zip files, on the network, or anywhere else that Python searches |
| 137 | during import. Namespace packages may or may not correspond directly to |
| 138 | objects on the file system; they may be virtual modules that have no concrete |
| 139 | representation. |
| 140 | |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 141 | Namespace packages do not use an ordinary list for their ``__path__`` |
| 142 | attribute. They instead use a custom iterable type which will automatically |
| 143 | perform a new search for package portions on the next import attempt within |
| 144 | that package if the path of their parent package (or :data:`sys.path` for a |
| 145 | top level package) changes. |
| 146 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 147 | With namespace packages, there is no ``parent/__init__.py`` file. In fact, |
| 148 | there may be multiple ``parent`` directories found during import search, where |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 149 | each one is provided by a different portion. Thus ``parent/one`` may not be |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 150 | physically located next to ``parent/two``. In this case, Python will create a |
| 151 | namespace package for the top-level ``parent`` package whenever it or one of |
| 152 | its subpackages is imported. |
| 153 | |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 154 | See also :pep:`420` for the namespace package specification. |
| 155 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 156 | |
| 157 | Searching |
| 158 | ========= |
| 159 | |
| 160 | To begin the search, Python needs the :term:`fully qualified <qualified name>` |
| 161 | name of the module (or package, but for the purposes of this discussion, the |
| 162 | difference is immaterial) being imported. This name may come from various |
| 163 | arguments to the :keyword:`import` statement, or from the parameters to the |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 164 | :func:`importlib.import_module` or :func:`__import__` functions. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 165 | |
| 166 | This name will be used in various phases of the import search, and it may be |
| 167 | the dotted path to a submodule, e.g. ``foo.bar.baz``. In this case, Python |
| 168 | first tries to import ``foo``, then ``foo.bar``, and finally ``foo.bar.baz``. |
kms70847 | 9c972b5 | 2017-05-30 12:12:33 -0400 | [diff] [blame] | 169 | If any of the intermediate imports fail, a :exc:`ModuleNotFoundError` is raised. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 170 | |
| 171 | |
| 172 | The module cache |
| 173 | ---------------- |
| 174 | |
| 175 | .. index:: |
| 176 | single: sys.modules |
| 177 | |
| 178 | The first place checked during import search is :data:`sys.modules`. This |
| 179 | mapping serves as a cache of all modules that have been previously imported, |
| 180 | including the intermediate paths. So if ``foo.bar.baz`` was previously |
| 181 | imported, :data:`sys.modules` will contain entries for ``foo``, ``foo.bar``, |
| 182 | and ``foo.bar.baz``. Each key will have as its value the corresponding module |
| 183 | object. |
| 184 | |
| 185 | During import, the module name is looked up in :data:`sys.modules` and if |
| 186 | present, the associated value is the module satisfying the import, and the |
kms70847 | 9c972b5 | 2017-05-30 12:12:33 -0400 | [diff] [blame] | 187 | process completes. However, if the value is ``None``, then a |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 188 | :exc:`ModuleNotFoundError` is raised. If the module name is missing, Python will |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 189 | continue searching for the module. |
| 190 | |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 191 | :data:`sys.modules` is writable. Deleting a key may not destroy the |
| 192 | associated module (as other modules may hold references to it), |
| 193 | but it will invalidate the cache entry for the named module, causing |
| 194 | Python to search anew for the named module upon its next |
| 195 | import. The key can also be assigned to ``None``, forcing the next import |
kms70847 | 9c972b5 | 2017-05-30 12:12:33 -0400 | [diff] [blame] | 196 | of the module to result in a :exc:`ModuleNotFoundError`. |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 197 | |
| 198 | Beware though, as if you keep a reference to the module object, |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 199 | invalidate its cache entry in :data:`sys.modules`, and then re-import the |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 200 | named module, the two module objects will *not* be the same. By contrast, |
Berker Peksag | 7e732a7 | 2015-07-25 13:02:37 +0300 | [diff] [blame] | 201 | :func:`importlib.reload` will reuse the *same* module object, and simply |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 202 | reinitialise the module contents by rerunning the module's code. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 203 | |
| 204 | |
| 205 | Finders and loaders |
| 206 | ------------------- |
| 207 | |
| 208 | .. index:: |
| 209 | single: finder |
| 210 | single: loader |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 211 | single: module spec |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 212 | |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 213 | If the named module is not found in :data:`sys.modules`, then Python's import |
| 214 | protocol is invoked to find and load the module. This protocol consists of |
| 215 | two conceptual objects, :term:`finders <finder>` and :term:`loaders <loader>`. |
| 216 | A finder's job is to determine whether it can find the named module using |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 217 | whatever strategy it knows about. Objects that implement both of these |
| 218 | interfaces are referred to as :term:`importers <importer>` - they return |
| 219 | themselves when they find that they can load the requested module. |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 220 | |
Andrew Svetlov | e2cf03e | 2012-11-15 16:28:21 +0200 | [diff] [blame] | 221 | Python includes a number of default finders and importers. The first one |
| 222 | knows how to locate built-in modules, and the second knows how to locate |
| 223 | frozen modules. A third default finder searches an :term:`import path` |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 224 | for modules. The :term:`import path` is a list of locations that may |
| 225 | name file system paths or zip files. It can also be extended to search |
| 226 | for any locatable resource, such as those identified by URLs. |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 227 | |
| 228 | The import machinery is extensible, so new finders can be added to extend the |
| 229 | range and scope of module searching. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 230 | |
| 231 | Finders do not actually load modules. If they can find the named module, they |
Georg Brandl | 472a65a | 2013-11-24 12:39:56 +0100 | [diff] [blame] | 232 | return a :dfn:`module spec`, an encapsulation of the module's import-related |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 233 | information, which the import machinery then uses when loading the module. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 234 | |
| 235 | The following sections describe the protocol for finders and loaders in more |
| 236 | detail, including how you can create and register new ones to extend the |
| 237 | import machinery. |
| 238 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 239 | .. versionchanged:: 3.4 |
| 240 | In previous versions of Python, finders returned :term:`loaders <loader>` |
| 241 | directly, whereas now they return module specs which *contain* loaders. |
| 242 | Loaders are still used during import but have fewer responsibilities. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 243 | |
| 244 | Import hooks |
| 245 | ------------ |
| 246 | |
| 247 | .. index:: |
| 248 | single: import hooks |
| 249 | single: meta hooks |
| 250 | single: path hooks |
| 251 | pair: hooks; import |
| 252 | pair: hooks; meta |
| 253 | pair: hooks; path |
| 254 | |
| 255 | The import machinery is designed to be extensible; the primary mechanism for |
| 256 | this are the *import hooks*. There are two types of import hooks: *meta |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 257 | hooks* and *import path hooks*. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 258 | |
| 259 | Meta hooks are called at the start of import processing, before any other |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 260 | import processing has occurred, other than :data:`sys.modules` cache look up. |
| 261 | This allows meta hooks to override :data:`sys.path` processing, frozen |
| 262 | modules, or even built-in modules. Meta hooks are registered by adding new |
| 263 | finder objects to :data:`sys.meta_path`, as described below. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 264 | |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 265 | Import path hooks are called as part of :data:`sys.path` (or |
| 266 | ``package.__path__``) processing, at the point where their associated path |
| 267 | item is encountered. Import path hooks are registered by adding new callables |
| 268 | to :data:`sys.path_hooks` as described below. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 269 | |
| 270 | |
| 271 | The meta path |
| 272 | ------------- |
| 273 | |
| 274 | .. index:: |
| 275 | single: sys.meta_path |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 276 | pair: finder; find_spec |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 277 | |
| 278 | When the named module is not found in :data:`sys.modules`, Python next |
| 279 | searches :data:`sys.meta_path`, which contains a list of meta path finder |
| 280 | objects. These finders are queried in order to see if they know how to handle |
| 281 | the named module. Meta path finders must implement a method called |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 282 | :meth:`~importlib.abc.MetaPathFinder.find_spec()` which takes three arguments: |
| 283 | a name, an import path, and (optionally) a target module. The meta path |
| 284 | finder can use any strategy it wants to determine whether it can handle |
| 285 | the named module or not. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 286 | |
| 287 | If the meta path finder knows how to handle the named module, it returns a |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 288 | spec object. If it cannot handle the named module, it returns ``None``. If |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 289 | :data:`sys.meta_path` processing reaches the end of its list without returning |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 290 | a spec, then a :exc:`ModuleNotFoundError` is raised. Any other exceptions |
| 291 | raised are simply propagated up, aborting the import process. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 292 | |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 293 | The :meth:`~importlib.abc.MetaPathFinder.find_spec()` method of meta path |
| 294 | finders is called with two or three arguments. The first is the fully |
| 295 | qualified name of the module being imported, for example ``foo.bar.baz``. |
| 296 | The second argument is the path entries to use for the module search. For |
| 297 | top-level modules, the second argument is ``None``, but for submodules or |
| 298 | subpackages, the second argument is the value of the parent package's |
| 299 | ``__path__`` attribute. If the appropriate ``__path__`` attribute cannot |
kms70847 | 9c972b5 | 2017-05-30 12:12:33 -0400 | [diff] [blame] | 300 | be accessed, a :exc:`ModuleNotFoundError` is raised. The third argument |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 301 | is an existing module object that will be the target of loading later. |
| 302 | The import system passes in a target module only during reload. |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 303 | |
| 304 | The meta path may be traversed multiple times for a single import request. |
| 305 | For example, assuming none of the modules involved has already been cached, |
| 306 | importing ``foo.bar.baz`` will first perform a top level import, calling |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 307 | ``mpf.find_spec("foo", None, None)`` on each meta path finder (``mpf``). After |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 308 | ``foo`` has been imported, ``foo.bar`` will be imported by traversing the |
| 309 | meta path a second time, calling |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 310 | ``mpf.find_spec("foo.bar", foo.__path__, None)``. Once ``foo.bar`` has been |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 311 | imported, the final traversal will call |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 312 | ``mpf.find_spec("foo.bar.baz", foo.bar.__path__, None)``. |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 313 | |
| 314 | Some meta path finders only support top level imports. These importers will |
| 315 | always return ``None`` when anything other than ``None`` is passed as the |
| 316 | second argument. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 317 | |
| 318 | Python's default :data:`sys.meta_path` has three meta path finders, one that |
| 319 | knows how to import built-in modules, one that knows how to import frozen |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 320 | modules, and one that knows how to import modules from an :term:`import path` |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 321 | (i.e. the :term:`path based finder`). |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 322 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 323 | .. versionchanged:: 3.4 |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 324 | The :meth:`~importlib.abc.MetaPathFinder.find_spec` method of meta path |
| 325 | finders replaced :meth:`~importlib.abc.MetaPathFinder.find_module`, which |
| 326 | is now deprecated. While it will continue to work without change, the |
| 327 | import machinery will try it only if the finder does not implement |
| 328 | ``find_spec()``. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 329 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 330 | |
| 331 | Loading |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 332 | ======= |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 333 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 334 | If and when a module spec is found, the import machinery will use it (and |
| 335 | the loader it contains) when loading the module. Here is an approximation |
| 336 | of what happens during the loading portion of import:: |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 337 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 338 | module = None |
| 339 | if spec.loader is not None and hasattr(spec.loader, 'create_module'): |
Brett Cannon | 02d8454 | 2015-01-09 11:39:21 -0500 | [diff] [blame] | 340 | # It is assumed 'exec_module' will also be defined on the loader. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 341 | module = spec.loader.create_module(spec) |
| 342 | if module is None: |
| 343 | module = ModuleType(spec.name) |
| 344 | # The import-related module attributes get set here: |
| 345 | _init_module_attrs(spec, module) |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 346 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 347 | if spec.loader is None: |
| 348 | if spec.submodule_search_locations is not None: |
| 349 | # namespace package |
| 350 | sys.modules[spec.name] = module |
| 351 | else: |
| 352 | # unsupported |
| 353 | raise ImportError |
| 354 | elif not hasattr(spec.loader, 'exec_module'): |
| 355 | module = spec.loader.load_module(spec.name) |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 356 | # Set __loader__ and __package__ if missing. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 357 | else: |
| 358 | sys.modules[spec.name] = module |
| 359 | try: |
| 360 | spec.loader.exec_module(module) |
| 361 | except BaseException: |
| 362 | try: |
| 363 | del sys.modules[spec.name] |
| 364 | except KeyError: |
| 365 | pass |
| 366 | raise |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 367 | return sys.modules[spec.name] |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 368 | |
| 369 | Note the following details: |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 370 | |
| 371 | * If there is an existing module object with the given name in |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 372 | :data:`sys.modules`, import will have already returned it. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 373 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 374 | * The module will exist in :data:`sys.modules` before the loader |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 375 | executes the module code. This is crucial because the module code may |
| 376 | (directly or indirectly) import itself; adding it to :data:`sys.modules` |
| 377 | beforehand prevents unbounded recursion in the worst case and multiple |
| 378 | loading in the best. |
| 379 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 380 | * If loading fails, the failing module -- and only the failing module -- |
| 381 | gets removed from :data:`sys.modules`. Any module already in the |
| 382 | :data:`sys.modules` cache, and any module that was successfully loaded |
| 383 | as a side-effect, must remain in the cache. This contrasts with |
| 384 | reloading where even the failing module is left in :data:`sys.modules`. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 385 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 386 | * After the module is created but before execution, the import machinery |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 387 | sets the import-related module attributes ("_init_module_attrs" in |
| 388 | the pseudo-code example above), as summarized in a |
| 389 | :ref:`later section <import-mod-attrs>`. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 390 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 391 | * Module execution is the key moment of loading in which the module's |
| 392 | namespace gets populated. Execution is entirely delegated to the |
| 393 | loader, which gets to decide what gets populated and how. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 394 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 395 | * The module created during loading and passed to exec_module() may |
| 396 | not be the one returned at the end of import [#fnlo]_. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 397 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 398 | .. versionchanged:: 3.4 |
| 399 | The import system has taken over the boilerplate responsibilities of |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 400 | loaders. These were previously performed by the |
| 401 | :meth:`importlib.abc.Loader.load_module` method. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 402 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 403 | Loaders |
| 404 | ------- |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 405 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 406 | Module loaders provide the critical function of loading: module execution. |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 407 | The import machinery calls the :meth:`importlib.abc.Loader.exec_module` |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 408 | method with a single argument, the module object to execute. Any value |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 409 | returned from :meth:`~importlib.abc.Loader.exec_module` is ignored. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 410 | |
| 411 | Loaders must satisfy the following requirements: |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 412 | |
| 413 | * If the module is a Python module (as opposed to a built-in module or a |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 414 | dynamically loaded extension), the loader should execute the module's code |
| 415 | in the module's global name space (``module.__dict__``). |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 416 | |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 417 | * If the loader cannot execute the module, it should raise an |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 418 | :exc:`ImportError`, although any other exception raised during |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 419 | :meth:`~importlib.abc.Loader.exec_module` will be propagated. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 420 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 421 | In many cases, the finder and loader can be the same object; in such cases the |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 422 | :meth:`~importlib.abc.MetaPathFinder.find_spec` method would just return a |
| 423 | spec with the loader set to ``self``. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 424 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 425 | Module loaders may opt in to creating the module object during loading |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 426 | by implementing a :meth:`~importlib.abc.Loader.create_module` method. |
| 427 | It takes one argument, the module spec, and returns the new module object |
| 428 | to use during loading. ``create_module()`` does not need to set any attributes |
Brett Cannon | 02d8454 | 2015-01-09 11:39:21 -0500 | [diff] [blame] | 429 | on the module object. If the method returns ``None``, the |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 430 | import machinery will create the new module itself. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 431 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 432 | .. versionadded:: 3.4 |
Marco Buttu | 46ce759 | 2017-02-26 16:14:45 +0100 | [diff] [blame] | 433 | The :meth:`~importlib.abc.Loader.create_module` method of loaders. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 434 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 435 | .. versionchanged:: 3.4 |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 436 | The :meth:`~importlib.abc.Loader.load_module` method was replaced by |
| 437 | :meth:`~importlib.abc.Loader.exec_module` and the import |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 438 | machinery assumed all the boilerplate responsibilities of loading. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 439 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 440 | For compatibility with existing loaders, the import machinery will use |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 441 | the ``load_module()`` method of loaders if it exists and the loader does |
Larry Hastings | bfd715e | 2014-01-05 04:35:56 -0800 | [diff] [blame] | 442 | not also implement ``exec_module()``. However, ``load_module()`` has been |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 443 | deprecated and loaders should implement ``exec_module()`` instead. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 444 | |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 445 | The ``load_module()`` method must implement all the boilerplate loading |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 446 | functionality described above in addition to executing the module. All |
| 447 | the same constraints apply, with some additional clarification: |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 448 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 449 | * If there is an existing module object with the given name in |
| 450 | :data:`sys.modules`, the loader must use that existing module. |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 451 | (Otherwise, :func:`importlib.reload` will not work correctly.) If the |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 452 | named module does not exist in :data:`sys.modules`, the loader |
| 453 | must create a new module object and add it to :data:`sys.modules`. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 454 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 455 | * The module *must* exist in :data:`sys.modules` before the loader |
| 456 | executes the module code, to prevent unbounded recursion or multiple |
| 457 | loading. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 458 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 459 | * If loading fails, the loader must remove any modules it has inserted |
| 460 | into :data:`sys.modules`, but it must remove **only** the failing |
Brett Cannon | d0c4ef1 | 2014-11-07 11:29:33 -0500 | [diff] [blame] | 461 | module(s), and only if the loader itself has loaded the module(s) |
| 462 | explicitly. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 463 | |
Brett Cannon | 02d8454 | 2015-01-09 11:39:21 -0500 | [diff] [blame] | 464 | .. versionchanged:: 3.5 |
| 465 | A :exc:`DeprecationWarning` is raised when ``exec_module()`` is defined but |
Mariatta | 6b4a5f4 | 2017-02-26 07:36:57 -0800 | [diff] [blame] | 466 | ``create_module()`` is not. |
| 467 | |
| 468 | .. versionchanged:: 3.6 |
| 469 | An :exc:`ImportError` is raised when ``exec_module()`` is defined but |
Mariatta | 1f5639c | 2017-02-26 13:23:38 -0800 | [diff] [blame] | 470 | ``create_module()`` is not. |
Brett Cannon | 02d8454 | 2015-01-09 11:39:21 -0500 | [diff] [blame] | 471 | |
Barry Warsaw | 2097f53 | 2015-04-22 18:29:16 -0400 | [diff] [blame] | 472 | Submodules |
| 473 | ---------- |
| 474 | |
| 475 | When a submodule is loaded using any mechanism (e.g. ``importlib`` APIs, the |
| 476 | ``import`` or ``import-from`` statements, or built-in ``__import__()``) a |
| 477 | binding is placed in the parent module's namespace to the submodule object. |
| 478 | For example, if package ``spam`` has a submodule ``foo``, after importing |
| 479 | ``spam.foo``, ``spam`` will have an attribute ``foo`` which is bound to the |
| 480 | submodule. Let's say you have the following directory structure:: |
| 481 | |
| 482 | spam/ |
| 483 | __init__.py |
| 484 | foo.py |
| 485 | bar.py |
| 486 | |
| 487 | and ``spam/__init__.py`` has the following lines in it:: |
| 488 | |
| 489 | from .foo import Foo |
| 490 | from .bar import Bar |
| 491 | |
| 492 | then executing the following puts a name binding to ``foo`` and ``bar`` in the |
| 493 | ``spam`` module:: |
| 494 | |
| 495 | >>> import spam |
| 496 | >>> spam.foo |
| 497 | <module 'spam.foo' from '/tmp/imports/spam/foo.py'> |
| 498 | >>> spam.bar |
| 499 | <module 'spam.bar' from '/tmp/imports/spam/bar.py'> |
| 500 | |
| 501 | Given Python's familiar name binding rules this might seem surprising, but |
| 502 | it's actually a fundamental feature of the import system. The invariant |
| 503 | holding is that if you have ``sys.modules['spam']`` and |
| 504 | ``sys.modules['spam.foo']`` (as you would after the above import), the latter |
| 505 | must appear as the ``foo`` attribute of the former. |
| 506 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 507 | Module spec |
| 508 | ----------- |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 509 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 510 | The import machinery uses a variety of information about each module |
| 511 | during import, especially before loading. Most of the information is |
| 512 | common to all modules. The purpose of a module's spec is to encapsulate |
| 513 | this import-related information on a per-module basis. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 514 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 515 | Using a spec during import allows state to be transferred between import |
| 516 | system components, e.g. between the finder that creates the module spec |
| 517 | and the loader that executes it. Most importantly, it allows the |
| 518 | import machinery to perform the boilerplate operations of loading, |
| 519 | whereas without a module spec the loader had that responsibility. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 520 | |
Barry Warsaw | 191e313 | 2017-10-17 15:52:38 -0400 | [diff] [blame] | 521 | The module's spec is exposed as the ``__spec__`` attribute on a module object. |
| 522 | See :class:`~importlib.machinery.ModuleSpec` for details on the contents of |
| 523 | the module spec. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 524 | |
| 525 | .. versionadded:: 3.4 |
| 526 | |
Georg Brandl | 472a65a | 2013-11-24 12:39:56 +0100 | [diff] [blame] | 527 | .. _import-mod-attrs: |
| 528 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 529 | Import-related module attributes |
| 530 | -------------------------------- |
| 531 | |
| 532 | The import machinery fills in these attributes on each module object |
| 533 | during loading, based on the module's spec, before the loader executes |
| 534 | the module. |
| 535 | |
| 536 | .. attribute:: __name__ |
| 537 | |
| 538 | The ``__name__`` attribute must be set to the fully-qualified name of |
| 539 | the module. This name is used to uniquely identify the module in |
| 540 | the import system. |
| 541 | |
| 542 | .. attribute:: __loader__ |
| 543 | |
| 544 | The ``__loader__`` attribute must be set to the loader object that |
| 545 | the import machinery used when loading the module. This is mostly |
| 546 | for introspection, but can be used for additional loader-specific |
| 547 | functionality, for example getting data associated with a loader. |
| 548 | |
| 549 | .. attribute:: __package__ |
| 550 | |
| 551 | The module's ``__package__`` attribute must be set. Its value must |
| 552 | be a string, but it can be the same value as its ``__name__``. When |
| 553 | the module is a package, its ``__package__`` value should be set to |
| 554 | its ``__name__``. When the module is not a package, ``__package__`` |
| 555 | should be set to the empty string for top-level modules, or for |
| 556 | submodules, to the parent package's name. See :pep:`366` for further |
| 557 | details. |
| 558 | |
| 559 | This attribute is used instead of ``__name__`` to calculate explicit |
Brett Cannon | 849113a | 2016-01-22 15:25:50 -0800 | [diff] [blame] | 560 | relative imports for main modules, as defined in :pep:`366`. It is |
| 561 | expected to have the same value as ``__spec__.parent``. |
| 562 | |
| 563 | .. versionchanged:: 3.6 |
| 564 | The value of ``__package__`` is expected to be the same as |
| 565 | ``__spec__.parent``. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 566 | |
| 567 | .. attribute:: __spec__ |
| 568 | |
| 569 | The ``__spec__`` attribute must be set to the module spec that was |
Brett Cannon | 849113a | 2016-01-22 15:25:50 -0800 | [diff] [blame] | 570 | used when importing the module. Setting ``__spec__`` |
Eric Snow | e50f9aa | 2014-03-28 18:10:33 -0600 | [diff] [blame] | 571 | appropriately applies equally to :ref:`modules initialized during |
| 572 | interpreter startup <programs>`. The one exception is ``__main__``, |
| 573 | where ``__spec__`` is :ref:`set to None in some cases <main_spec>`. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 574 | |
Brett Cannon | 849113a | 2016-01-22 15:25:50 -0800 | [diff] [blame] | 575 | When ``__package__`` is not defined, ``__spec__.parent`` is used as |
| 576 | a fallback. |
| 577 | |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 578 | .. versionadded:: 3.4 |
| 579 | |
Brett Cannon | 849113a | 2016-01-22 15:25:50 -0800 | [diff] [blame] | 580 | .. versionchanged:: 3.6 |
| 581 | ``__spec__.parent`` is used as a fallback when ``__package__`` is |
| 582 | not defined. |
| 583 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 584 | .. attribute:: __path__ |
| 585 | |
| 586 | If the module is a package (either regular or namespace), the module |
| 587 | object's ``__path__`` attribute must be set. The value must be |
| 588 | iterable, but may be empty if ``__path__`` has no further significance. |
| 589 | If ``__path__`` is not empty, it must produce strings when iterated |
| 590 | over. More details on the semantics of ``__path__`` are given |
| 591 | :ref:`below <package-path-rules>`. |
| 592 | |
| 593 | Non-package modules should not have a ``__path__`` attribute. |
| 594 | |
| 595 | .. attribute:: __file__ |
| 596 | .. attribute:: __cached__ |
| 597 | |
| 598 | ``__file__`` is optional. If set, this attribute's value must be a |
| 599 | string. The import system may opt to leave ``__file__`` unset if it |
| 600 | has no semantic meaning (e.g. a module loaded from a database). |
| 601 | |
| 602 | If ``__file__`` is set, it may also be appropriate to set the |
| 603 | ``__cached__`` attribute which is the path to any compiled version of |
| 604 | the code (e.g. byte-compiled file). The file does not need to exist |
| 605 | to set this attribute; the path can simply point to where the |
| 606 | compiled file would exist (see :pep:`3147`). |
| 607 | |
| 608 | It is also appropriate to set ``__cached__`` when ``__file__`` is not |
| 609 | set. However, that scenario is quite atypical. Ultimately, the |
| 610 | loader is what makes use of ``__file__`` and/or ``__cached__``. So |
| 611 | if a loader can load from a cached module but otherwise does not load |
| 612 | from a file, that atypical scenario may be appropriate. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 613 | |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 614 | .. _package-path-rules: |
| 615 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 616 | module.__path__ |
| 617 | --------------- |
| 618 | |
Brett Cannon | 441d945 | 2018-04-20 16:32:46 -0700 | [diff] [blame] | 619 | By definition, if a module has a ``__path__`` attribute, it is a package. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 620 | |
| 621 | A package's ``__path__`` attribute is used during imports of its subpackages. |
| 622 | Within the import machinery, it functions much the same as :data:`sys.path`, |
| 623 | i.e. providing a list of locations to search for modules during import. |
| 624 | However, ``__path__`` is typically much more constrained than |
| 625 | :data:`sys.path`. |
| 626 | |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 627 | ``__path__`` must be an iterable of strings, but it may be empty. |
| 628 | The same rules used for :data:`sys.path` also apply to a package's |
| 629 | ``__path__``, and :data:`sys.path_hooks` (described below) are |
| 630 | consulted when traversing a package's ``__path__``. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 631 | |
| 632 | A package's ``__init__.py`` file may set or alter the package's ``__path__`` |
| 633 | attribute, and this was typically the way namespace packages were implemented |
| 634 | prior to :pep:`420`. With the adoption of :pep:`420`, namespace packages no |
| 635 | longer need to supply ``__init__.py`` files containing only ``__path__`` |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 636 | manipulation code; the import machinery automatically sets ``__path__`` |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 637 | correctly for the namespace package. |
| 638 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 639 | Module reprs |
| 640 | ------------ |
| 641 | |
| 642 | By default, all modules have a usable repr, however depending on the |
| 643 | attributes set above, and in the module's spec, you can more explicitly |
| 644 | control the repr of module objects. |
| 645 | |
| 646 | If the module has a spec (``__spec__``), the import machinery will try |
| 647 | to generate a repr from it. If that fails or there is no spec, the import |
| 648 | system will craft a default repr using whatever information is available |
| 649 | on the module. It will try to use the ``module.__name__``, |
| 650 | ``module.__file__``, and ``module.__loader__`` as input into the repr, |
| 651 | with defaults for whatever information is missing. |
| 652 | |
| 653 | Here are the exact rules used: |
| 654 | |
| 655 | * If the module has a ``__spec__`` attribute, the information in the spec |
| 656 | is used to generate the repr. The "name", "loader", "origin", and |
| 657 | "has_location" attributes are consulted. |
| 658 | |
| 659 | * If the module has a ``__file__`` attribute, this is used as part of the |
| 660 | module's repr. |
| 661 | |
| 662 | * If the module has no ``__file__`` but does have a ``__loader__`` that is not |
| 663 | ``None``, then the loader's repr is used as part of the module's repr. |
| 664 | |
| 665 | * Otherwise, just use the module's ``__name__`` in the repr. |
| 666 | |
| 667 | .. versionchanged:: 3.4 |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 668 | Use of :meth:`loader.module_repr() <importlib.abc.Loader.module_repr>` |
| 669 | has been deprecated and the module spec is now used by the import |
| 670 | machinery to generate a module repr. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 671 | |
| 672 | For backward compatibility with Python 3.3, the module repr will be |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 673 | generated by calling the loader's |
| 674 | :meth:`~importlib.abc.Loader.module_repr` method, if defined, before |
| 675 | trying either approach described above. However, the method is deprecated. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 676 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 677 | .. _pyc-invalidation: |
| 678 | |
| 679 | Cached bytecode invalidation |
| 680 | ---------------------------- |
| 681 | |
| 682 | Before Python loads cached bytecode from ``.pyc`` file, it checks whether the |
| 683 | cache is up-to-date with the source ``.py`` file. By default, Python does this |
| 684 | by storing the source's last-modified timestamp and size in the cache file when |
| 685 | writing it. At runtime, the import system then validates the cache file by |
| 686 | checking the stored metadata in the cache file against at source's |
| 687 | metadata. |
| 688 | |
| 689 | Python also supports "hash-based" cache files, which store a hash of the source |
| 690 | file's contents rather than its metadata. There are two variants of hash-based |
| 691 | ``.pyc`` files: checked and unchecked. For checked hash-based ``.pyc`` files, |
| 692 | Python validates the cache file by hashing the source file and comparing the |
| 693 | resulting hash with the hash in the cache file. If a checked hash-based cache |
| 694 | file is found to be invalid, Python regenerates it and writes a new checked |
| 695 | hash-based cache file. For unchecked hash-based ``.pyc`` files, Python simply |
| 696 | assumes the cache file is valid if it exists. Hash-based ``.pyc`` files |
| 697 | validation behavior may be overridden with the :option:`--check-hash-based-pycs` |
| 698 | flag. |
| 699 | |
| 700 | .. versionchanged:: 3.7 |
| 701 | Added hash-based ``.pyc`` files. Previously, Python only supported |
| 702 | timestamp-based invalidation of bytecode caches. |
| 703 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 704 | |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 705 | The Path Based Finder |
| 706 | ===================== |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 707 | |
| 708 | .. index:: |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 709 | single: path based finder |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 710 | |
| 711 | As mentioned previously, Python comes with several default meta path finders. |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 712 | One of these, called the :term:`path based finder` |
Serhiy Storchaka | 2a61452 | 2013-12-23 18:21:57 +0200 | [diff] [blame] | 713 | (:class:`~importlib.machinery.PathFinder`), searches an :term:`import path`, |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 714 | which contains a list of :term:`path entries <path entry>`. Each path |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 715 | entry names a location to search for modules. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 716 | |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 717 | The path based finder itself doesn't know how to import anything. Instead, it |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 718 | traverses the individual path entries, associating each of them with a |
| 719 | path entry finder that knows how to handle that particular kind of path. |
| 720 | |
| 721 | The default set of path entry finders implement all the semantics for finding |
| 722 | modules on the file system, handling special file types such as Python source |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 723 | code (``.py`` files), Python byte code (``.pyc`` files) and |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 724 | shared libraries (e.g. ``.so`` files). When supported by the :mod:`zipimport` |
| 725 | module in the standard library, the default path entry finders also handle |
| 726 | loading all of these file types (other than shared libraries) from zipfiles. |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 727 | |
| 728 | Path entries need not be limited to file system locations. They can refer to |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 729 | URLs, database queries, or any other location that can be specified as a |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 730 | string. |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 731 | |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 732 | The path based finder provides additional hooks and protocols so that you |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 733 | can extend and customize the types of searchable path entries. For example, |
| 734 | if you wanted to support path entries as network URLs, you could write a hook |
| 735 | that implements HTTP semantics to find modules on the web. This hook (a |
| 736 | callable) would return a :term:`path entry finder` supporting the protocol |
| 737 | described below, which was then used to get a loader for the module from the |
| 738 | web. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 739 | |
| 740 | A word of warning: this section and the previous both use the term *finder*, |
| 741 | distinguishing between them by using the terms :term:`meta path finder` and |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 742 | :term:`path entry finder`. These two types of finders are very similar, |
| 743 | support similar protocols, and function in similar ways during the import |
| 744 | process, but it's important to keep in mind that they are subtly different. |
| 745 | In particular, meta path finders operate at the beginning of the import |
| 746 | process, as keyed off the :data:`sys.meta_path` traversal. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 747 | |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 748 | By contrast, path entry finders are in a sense an implementation detail |
| 749 | of the path based finder, and in fact, if the path based finder were to be |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 750 | removed from :data:`sys.meta_path`, none of the path entry finder semantics |
| 751 | would be invoked. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 752 | |
| 753 | |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 754 | Path entry finders |
| 755 | ------------------ |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 756 | |
| 757 | .. index:: |
| 758 | single: sys.path |
| 759 | single: sys.path_hooks |
| 760 | single: sys.path_importer_cache |
| 761 | single: PYTHONPATH |
| 762 | |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 763 | The :term:`path based finder` is responsible for finding and loading |
| 764 | Python modules and packages whose location is specified with a string |
| 765 | :term:`path entry`. Most path entries name locations in the file system, |
| 766 | but they need not be limited to this. |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 767 | |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 768 | As a meta path finder, the :term:`path based finder` implements the |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 769 | :meth:`~importlib.abc.MetaPathFinder.find_spec` protocol previously |
| 770 | described, however it exposes additional hooks that can be used to |
| 771 | customize how modules are found and loaded from the :term:`import path`. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 772 | |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 773 | Three variables are used by the :term:`path based finder`, :data:`sys.path`, |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 774 | :data:`sys.path_hooks` and :data:`sys.path_importer_cache`. The ``__path__`` |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 775 | attributes on package objects are also used. These provide additional ways |
| 776 | that the import machinery can be customized. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 777 | |
| 778 | :data:`sys.path` contains a list of strings providing search locations for |
| 779 | modules and packages. It is initialized from the :data:`PYTHONPATH` |
| 780 | environment variable and various other installation- and |
| 781 | implementation-specific defaults. Entries in :data:`sys.path` can name |
| 782 | directories on the file system, zip files, and potentially other "locations" |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 783 | (see the :mod:`site` module) that should be searched for modules, such as |
Barry Warsaw | 82c1c78 | 2012-11-20 15:22:51 -0500 | [diff] [blame] | 784 | URLs, or database queries. Only strings and bytes should be present on |
| 785 | :data:`sys.path`; all other data types are ignored. The encoding of bytes |
| 786 | entries is determined by the individual :term:`path entry finders <path entry |
| 787 | finder>`. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 788 | |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 789 | The :term:`path based finder` is a :term:`meta path finder`, so the import |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 790 | machinery begins the :term:`import path` search by calling the path |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 791 | based finder's :meth:`~importlib.machinery.PathFinder.find_spec` method as |
| 792 | described previously. When the ``path`` argument to |
| 793 | :meth:`~importlib.machinery.PathFinder.find_spec` is given, it will be a |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 794 | list of string paths to traverse - typically a package's ``__path__`` |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 795 | attribute for an import within that package. If the ``path`` argument is |
| 796 | ``None``, this indicates a top level import and :data:`sys.path` is used. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 797 | |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 798 | The path based finder iterates over every entry in the search path, and |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 799 | for each of these, looks for an appropriate :term:`path entry finder` |
| 800 | (:class:`~importlib.abc.PathEntryFinder`) for the |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 801 | path entry. Because this can be an expensive operation (e.g. there may be |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 802 | `stat()` call overheads for this search), the path based finder maintains |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 803 | a cache mapping path entries to path entry finders. This cache is maintained |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 804 | in :data:`sys.path_importer_cache` (despite the name, this cache actually |
| 805 | stores finder objects rather than being limited to :term:`importer` objects). |
| 806 | In this way, the expensive search for a particular :term:`path entry` |
| 807 | location's :term:`path entry finder` need only be done once. User code is |
| 808 | free to remove cache entries from :data:`sys.path_importer_cache` forcing |
| 809 | the path based finder to perform the path entry search again [#fnpic]_. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 810 | |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 811 | If the path entry is not present in the cache, the path based finder iterates |
Barry Warsaw | 82c1c78 | 2012-11-20 15:22:51 -0500 | [diff] [blame] | 812 | over every callable in :data:`sys.path_hooks`. Each of the :term:`path entry |
| 813 | hooks <path entry hook>` in this list is called with a single argument, the |
| 814 | path entry to be searched. This callable may either return a :term:`path |
| 815 | entry finder` that can handle the path entry, or it may raise |
| 816 | :exc:`ImportError`. An :exc:`ImportError` is used by the path based finder to |
Brett Cannon | 6336fb2 | 2016-08-12 10:56:48 -0700 | [diff] [blame] | 817 | signal that the hook cannot find a :term:`path entry finder` |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 818 | for that :term:`path entry`. The |
| 819 | exception is ignored and :term:`import path` iteration continues. The hook |
| 820 | should expect either a string or bytes object; the encoding of bytes objects |
| 821 | is up to the hook (e.g. it may be a file system encoding, UTF-8, or something |
| 822 | else), and if the hook cannot decode the argument, it should raise |
| 823 | :exc:`ImportError`. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 824 | |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 825 | If :data:`sys.path_hooks` iteration ends with no :term:`path entry finder` |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 826 | being returned, then the path based finder's |
| 827 | :meth:`~importlib.machinery.PathFinder.find_spec` method will store ``None`` |
| 828 | in :data:`sys.path_importer_cache` (to indicate that there is no finder for |
| 829 | this path entry) and return ``None``, indicating that this |
| 830 | :term:`meta path finder` could not find the module. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 831 | |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 832 | If a :term:`path entry finder` *is* returned by one of the :term:`path entry |
| 833 | hook` callables on :data:`sys.path_hooks`, then the following protocol is used |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 834 | to ask the finder for a module spec, which is then used when loading the |
| 835 | module. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 836 | |
Brett Cannon | b6e2556 | 2014-11-21 12:19:28 -0500 | [diff] [blame] | 837 | The current working directory -- denoted by an empty string -- is handled |
| 838 | slightly differently from other entries on :data:`sys.path`. First, if the |
| 839 | current working directory is found to not exist, no value is stored in |
| 840 | :data:`sys.path_importer_cache`. Second, the value for the current working |
| 841 | directory is looked up fresh for each module lookup. Third, the path used for |
| 842 | :data:`sys.path_importer_cache` and returned by |
| 843 | :meth:`importlib.machinery.PathFinder.find_spec` will be the actual current |
| 844 | working directory and not the empty string. |
| 845 | |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 846 | Path entry finder protocol |
| 847 | -------------------------- |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 848 | |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 849 | In order to support imports of modules and initialized packages and also to |
| 850 | contribute portions to namespace packages, path entry finders must implement |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 851 | the :meth:`~importlib.abc.PathEntryFinder.find_spec` method. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 852 | |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 853 | :meth:`~importlib.abc.PathEntryFinder.find_spec` takes two argument, the |
| 854 | fully qualified name of the module being imported, and the (optional) target |
| 855 | module. ``find_spec()`` returns a fully populated spec for the module. |
| 856 | This spec will always have "loader" set (with one exception). |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 857 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 858 | To indicate to the import machinery that the spec represents a namespace |
| 859 | :term:`portion`. the path entry finder sets "loader" on the spec to |
| 860 | ``None`` and "submodule_search_locations" to a list containing the |
| 861 | portion. |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 862 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 863 | .. versionchanged:: 3.4 |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 864 | :meth:`~importlib.abc.PathEntryFinder.find_spec` replaced |
| 865 | :meth:`~importlib.abc.PathEntryFinder.find_loader` and |
| 866 | :meth:`~importlib.abc.PathEntryFinder.find_module`, both of which |
| 867 | are now deprecated, but will be used if ``find_spec()`` is not defined. |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 868 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 869 | Older path entry finders may implement one of these two deprecated methods |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 870 | instead of ``find_spec()``. The methods are still respected for the |
Brett Cannon | 6336fb2 | 2016-08-12 10:56:48 -0700 | [diff] [blame] | 871 | sake of backward compatibility. However, if ``find_spec()`` is |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 872 | implemented on the path entry finder, the legacy methods are ignored. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 873 | |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 874 | :meth:`~importlib.abc.PathEntryFinder.find_loader` takes one argument, the |
| 875 | fully qualified name of the module being imported. ``find_loader()`` |
| 876 | returns a 2-tuple where the first item is the loader and the second item |
| 877 | is a namespace :term:`portion`. When the first item (i.e. the loader) is |
| 878 | ``None``, this means that while the path entry finder does not have a |
| 879 | loader for the named module, it knows that the path entry contributes to |
| 880 | a namespace portion for the named module. This will almost always be the |
| 881 | case where Python is asked to import a namespace package that has no |
| 882 | physical presence on the file system. When a path entry finder returns |
| 883 | ``None`` for the loader, the second item of the 2-tuple return value must |
| 884 | be a sequence, although it can be empty. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 885 | |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 886 | If ``find_loader()`` returns a non-``None`` loader value, the portion is |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 887 | ignored and the loader is returned from the path based finder, terminating |
| 888 | the search through the path entries. |
| 889 | |
| 890 | For backwards compatibility with other implementations of the import |
| 891 | protocol, many path entry finders also support the same, |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 892 | traditional ``find_module()`` method that meta path finders support. |
| 893 | However path entry finder ``find_module()`` methods are never called |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 894 | with a ``path`` argument (they are expected to record the appropriate |
| 895 | path information from the initial call to the path hook). |
| 896 | |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 897 | The ``find_module()`` method on path entry finders is deprecated, |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 898 | as it does not allow the path entry finder to contribute portions to |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 899 | namespace packages. If both ``find_loader()`` and ``find_module()`` |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 900 | exist on a path entry finder, the import system will always call |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 901 | ``find_loader()`` in preference to ``find_module()``. |
Nick Coghlan | 4941774 | 2012-08-02 23:03:58 +1000 | [diff] [blame] | 902 | |
| 903 | |
| 904 | Replacing the standard import system |
| 905 | ==================================== |
| 906 | |
| 907 | The most reliable mechanism for replacing the entire import system is to |
| 908 | delete the default contents of :data:`sys.meta_path`, replacing them |
| 909 | entirely with a custom meta path hook. |
| 910 | |
| 911 | If it is acceptable to only alter the behaviour of import statements |
| 912 | without affecting other APIs that access the import system, then replacing |
| 913 | the builtin :func:`__import__` function may be sufficient. This technique |
| 914 | may also be employed at the module level to only alter the behaviour of |
| 915 | import statements within that module. |
| 916 | |
| 917 | To selectively prevent import of some modules from a hook early on the |
| 918 | meta path (rather than disabling the standard import system entirely), |
Dominik Miedziński | af34e0a | 2017-05-16 18:40:17 +0200 | [diff] [blame] | 919 | it is sufficient to raise :exc:`ModuleNotFoundError` directly from |
Eric Snow | 7cff4cd | 2013-12-16 23:10:50 -0700 | [diff] [blame] | 920 | :meth:`~importlib.abc.MetaPathFinder.find_spec` instead of returning |
| 921 | ``None``. The latter indicates that the meta path search should continue, |
| 922 | while raising an exception terminates it immediately. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 923 | |
| 924 | |
Eric Snow | e50f9aa | 2014-03-28 18:10:33 -0600 | [diff] [blame] | 925 | Special considerations for __main__ |
| 926 | =================================== |
| 927 | |
| 928 | The :mod:`__main__` module is a special case relative to Python's import |
| 929 | system. As noted :ref:`elsewhere <programs>`, the ``__main__`` module |
| 930 | is directly initialized at interpreter startup, much like :mod:`sys` and |
| 931 | :mod:`builtins`. However, unlike those two, it doesn't strictly |
| 932 | qualify as a built-in module. This is because the manner in which |
| 933 | ``__main__`` is initialized depends on the flags and other options with |
| 934 | which the interpreter is invoked. |
| 935 | |
| 936 | .. _main_spec: |
| 937 | |
| 938 | __main__.__spec__ |
| 939 | ----------------- |
| 940 | |
| 941 | Depending on how :mod:`__main__` is initialized, ``__main__.__spec__`` |
| 942 | gets set appropriately or to ``None``. |
| 943 | |
| 944 | When Python is started with the :option:`-m` option, ``__spec__`` is set |
Nick Coghlan | 9aa00d1 | 2014-03-29 15:39:42 +1000 | [diff] [blame] | 945 | to the module spec of the corresponding module or package. ``__spec__`` is |
| 946 | also populated when the ``__main__`` module is loaded as part of executing a |
| 947 | directory, zipfile or other :data:`sys.path` entry. |
Eric Snow | e50f9aa | 2014-03-28 18:10:33 -0600 | [diff] [blame] | 948 | |
| 949 | In :ref:`the remaining cases <using-on-interface-options>` |
Nick Coghlan | 9aa00d1 | 2014-03-29 15:39:42 +1000 | [diff] [blame] | 950 | ``__main__.__spec__`` is set to ``None``, as the code used to populate the |
| 951 | :mod:`__main__` does not correspond directly with an importable module: |
Eric Snow | e50f9aa | 2014-03-28 18:10:33 -0600 | [diff] [blame] | 952 | |
| 953 | - interactive prompt |
Andrés Delfino | ea6a28c | 2018-11-07 14:06:45 -0300 | [diff] [blame] | 954 | - :option:`-c` option |
Eric Snow | e50f9aa | 2014-03-28 18:10:33 -0600 | [diff] [blame] | 955 | - running from stdin |
| 956 | - running directly from a source or bytecode file |
| 957 | |
Nick Coghlan | 9aa00d1 | 2014-03-29 15:39:42 +1000 | [diff] [blame] | 958 | Note that ``__main__.__spec__`` is always ``None`` in the last case, |
| 959 | *even if* the file could technically be imported directly as a module |
| 960 | instead. Use the :option:`-m` switch if valid module metadata is desired |
| 961 | in :mod:`__main__`. |
| 962 | |
| 963 | Note also that even when ``__main__`` corresponds with an importable module |
| 964 | and ``__main__.__spec__`` is set accordingly, they're still considered |
| 965 | *distinct* modules. This is due to the fact that blocks guarded by |
| 966 | ``if __name__ == "__main__":`` checks only execute when the module is used |
| 967 | to populate the ``__main__`` namespace, and not during normal import. |
| 968 | |
Eric Snow | e50f9aa | 2014-03-28 18:10:33 -0600 | [diff] [blame] | 969 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 970 | Open issues |
| 971 | =========== |
| 972 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 973 | XXX It would be really nice to have a diagram. |
| 974 | |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 975 | XXX * (import_machinery.rst) how about a section devoted just to the |
| 976 | attributes of modules and packages, perhaps expanding upon or supplanting the |
| 977 | related entries in the data model reference page? |
| 978 | |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 979 | XXX runpy, pkgutil, et al in the library manual should all get "See Also" |
| 980 | links at the top pointing to the new import system section. |
| 981 | |
Eric Snow | e50f9aa | 2014-03-28 18:10:33 -0600 | [diff] [blame] | 982 | XXX Add more explanation regarding the different ways in which |
| 983 | ``__main__`` is initialized? |
| 984 | |
| 985 | XXX Add more info on ``__main__`` quirks/pitfalls (i.e. copy from |
| 986 | :pep:`395`). |
| 987 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 988 | |
| 989 | References |
| 990 | ========== |
| 991 | |
| 992 | The import machinery has evolved considerably since Python's early days. The |
| 993 | original `specification for packages |
Benjamin Peterson | 60dbed1 | 2017-09-05 16:24:39 -0700 | [diff] [blame] | 994 | <https://www.python.org/doc/essays/packages/>`_ is still available to read, |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 995 | although some details have changed since the writing of that document. |
| 996 | |
| 997 | The original specification for :data:`sys.meta_path` was :pep:`302`, with |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 998 | subsequent extension in :pep:`420`. |
| 999 | |
| 1000 | :pep:`420` introduced :term:`namespace packages <namespace package>` for |
| 1001 | Python 3.3. :pep:`420` also introduced the :meth:`find_loader` protocol as an |
| 1002 | alternative to :meth:`find_module`. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 1003 | |
| 1004 | :pep:`366` describes the addition of the ``__package__`` attribute for |
| 1005 | explicit relative imports in main modules. |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 1006 | |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 1007 | :pep:`328` introduced absolute and explicit relative imports and initially |
| 1008 | proposed ``__name__`` for semantics :pep:`366` would eventually specify for |
Barry Warsaw | dadebab | 2012-07-31 16:03:09 -0400 | [diff] [blame] | 1009 | ``__package__``. |
| 1010 | |
| 1011 | :pep:`338` defines executing modules as scripts. |
| 1012 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 1013 | :pep:`451` adds the encapsulation of per-module import state in spec |
| 1014 | objects. It also off-loads most of the boilerplate responsibilities of |
| 1015 | loaders back onto the import machinery. These changes allow the |
| 1016 | deprecation of several APIs in the import system and also addition of new |
| 1017 | methods to finders and loaders. |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 1018 | |
Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 1019 | .. rubric:: Footnotes |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 1020 | |
| 1021 | .. [#fnmo] See :class:`types.ModuleType`. |
| 1022 | |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 1023 | .. [#fnlo] The importlib implementation avoids using the return value |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 1024 | directly. Instead, it gets the module object by looking the module name up |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 1025 | in :data:`sys.modules`. The indirect effect of this is that an imported |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 1026 | module may replace itself in :data:`sys.modules`. This is |
| 1027 | implementation-specific behavior that is not guaranteed to work in other |
| 1028 | Python implementations. |
| 1029 | |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 1030 | .. [#fnpic] In legacy code, it is possible to find instances of |
| 1031 | :class:`imp.NullImporter` in the :data:`sys.path_importer_cache`. It |
Nick Coghlan | 1685db0 | 2012-08-20 13:49:08 +1000 | [diff] [blame] | 1032 | is recommended that code be changed to use ``None`` instead. See |
Barry Warsaw | c1e721b | 2012-07-30 16:24:12 -0400 | [diff] [blame] | 1033 | :ref:`portingpythoncode` for more details. |