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