1:mod:`!importlib` --- The implementation of :keyword:`!import` 2============================================================== 3 4.. module:: importlib 5 :synopsis: The implementation of the import machinery. 6 7.. moduleauthor:: Brett Cannon <[email protected]> 8.. sectionauthor:: Brett Cannon <[email protected]> 9 10.. versionadded:: 3.1 11 12**Source code:** :source:`Lib/importlib/__init__.py` 13 14-------------- 15 16 17Introduction 18------------ 19 20The purpose of the :mod:`importlib` package is three-fold. 21 22One is to provide the 23implementation of the :keyword:`import` statement (and thus, by extension, the 24:func:`__import__` function) in Python source code. This provides an 25implementation of :keyword:`!import` which is portable to any Python 26interpreter. This also provides an implementation which is easier to 27comprehend than one implemented in a programming language other than Python. 28 29Two, the components to implement :keyword:`import` are exposed in this 30package, making it easier for users to create their own custom objects (known 31generically as an :term:`importer`) to participate in the import process. 32 33Three, the package contains modules exposing additional functionality for 34managing aspects of Python packages: 35 36* :mod:`importlib.metadata` presents access to metadata from third-party 37 distributions. 38* :mod:`importlib.resources` provides routines for accessing non-code 39 "resources" from Python packages. 40 41.. seealso:: 42 43 :ref:`import` 44 The language reference for the :keyword:`import` statement. 45 46 `Packages specification <https://www.python.org/doc/essays/packages/>`__ 47 Original specification of packages. Some semantics have changed since 48 the writing of this document (e.g. redirecting based on ``None`` 49 in :data:`sys.modules`). 50 51 The :func:`.__import__` function 52 The :keyword:`import` statement is syntactic sugar for this function. 53 54 :ref:`sys-path-init` 55 The initialization of :data:`sys.path`. 56 57 :pep:`235` 58 Import on Case-Insensitive Platforms 59 60 :pep:`263` 61 Defining Python Source Code Encodings 62 63 :pep:`302` 64 New Import Hooks 65 66 :pep:`328` 67 Imports: Multi-Line and Absolute/Relative 68 69 :pep:`366` 70 Main module explicit relative imports 71 72 :pep:`420` 73 Implicit namespace packages 74 75 :pep:`451` 76 A ModuleSpec Type for the Import System 77 78 :pep:`488` 79 Elimination of PYO files 80 81 :pep:`489` 82 Multi-phase extension module initialization 83 84 :pep:`552` 85 Deterministic pycs 86 87 :pep:`3120` 88 Using UTF-8 as the Default Source Encoding 89 90 :pep:`3147` 91 PYC Repository Directories 92 93 94Functions 95--------- 96 97.. function:: __import__(name, globals=None, locals=None, fromlist=(), level=0) 98 99 An implementation of the built-in :func:`__import__` function. 100 101 .. note:: 102 Programmatic importing of modules should use :func:`import_module` 103 instead of this function. 104 105.. function:: import_module(name, package=None) 106 107 Import a module. The *name* argument specifies what module to 108 import in absolute or relative terms 109 (e.g. either ``pkg.mod`` or ``..mod``). If the name is 110 specified in relative terms, then the *package* argument must be set to 111 the name of the package which is to act as the anchor for resolving the 112 package name (e.g. ``import_module('..mod', 'pkg.subpkg')`` will import 113 ``pkg.mod``). 114 115 The :func:`import_module` function acts as a simplifying wrapper around 116 :func:`importlib.__import__`. This means all semantics of the function are 117 derived from :func:`importlib.__import__`. The most important difference 118 between these two functions is that :func:`import_module` returns the 119 specified package or module (e.g. ``pkg.mod``), while :func:`__import__` 120 returns the top-level package or module (e.g. ``pkg``). 121 122 If you are dynamically importing a module that was created since the 123 interpreter began execution (e.g., created a Python source file), you may 124 need to call :func:`invalidate_caches` in order for the new module to be 125 noticed by the import system. 126 127 .. versionchanged:: 3.3 128 Parent packages are automatically imported. 129 130.. function:: find_loader(name, path=None) 131 132 Find the loader for a module, optionally within the specified *path*. If the 133 module is in :attr:`sys.modules`, then ``sys.modules[name].__loader__`` is 134 returned (unless the loader would be ``None`` or is not set, in which case 135 :exc:`ValueError` is raised). Otherwise a search using :attr:`sys.meta_path` 136 is done. ``None`` is returned if no loader is found. 137 138 A dotted name does not have its parents implicitly imported as that requires 139 loading them and that may not be desired. To properly import a submodule you 140 will need to import all parent packages of the submodule and use the correct 141 argument to *path*. 142 143 .. versionadded:: 3.3 144 145 .. versionchanged:: 3.4 146 If ``__loader__`` is not set, raise :exc:`ValueError`, just like when the 147 attribute is set to ``None``. 148 149 .. deprecated:: 3.4 150 Use :func:`importlib.util.find_spec` instead. 151 152.. function:: invalidate_caches() 153 154 Invalidate the internal caches of finders stored at 155 :data:`sys.meta_path`. If a finder implements ``invalidate_caches()`` then it 156 will be called to perform the invalidation. This function should be called 157 if any modules are created/installed while your program is running to 158 guarantee all finders will notice the new module's existence. 159 160 .. versionadded:: 3.3 161 162 .. versionchanged:: 3.10 163 Namespace packages created/installed in a different :data:`sys.path` 164 location after the same namespace was already imported are noticed. 165 166.. function:: reload(module) 167 168 Reload a previously imported *module*. The argument must be a module object, 169 so it must have been successfully imported before. This is useful if you 170 have edited the module source file using an external editor and want to try 171 out the new version without leaving the Python interpreter. The return value 172 is the module object (which can be different if re-importing causes a 173 different object to be placed in :data:`sys.modules`). 174 175 When :func:`reload` is executed: 176 177 * Python module's code is recompiled and the module-level code re-executed, 178 defining a new set of objects which are bound to names in the module's 179 dictionary by reusing the :term:`loader` which originally loaded the 180 module. The ``init`` function of extension modules is not called a second 181 time. 182 183 * As with all other objects in Python the old objects are only reclaimed 184 after their reference counts drop to zero. 185 186 * The names in the module namespace are updated to point to any new or 187 changed objects. 188 189 * Other references to the old objects (such as names external to the module) are 190 not rebound to refer to the new objects and must be updated in each namespace 191 where they occur if that is desired. 192 193 There are a number of other caveats: 194 195 When a module is reloaded, its dictionary (containing the module's global 196 variables) is retained. Redefinitions of names will override the old 197 definitions, so this is generally not a problem. If the new version of a 198 module does not define a name that was defined by the old version, the old 199 definition remains. This feature can be used to the module's advantage if it 200 maintains a global table or cache of objects --- with a :keyword:`try` 201 statement it can test for the table's presence and skip its initialization if 202 desired:: 203 204 try: 205 cache 206 except NameError: 207 cache = {} 208 209 It is generally not very useful to reload built-in or dynamically loaded 210 modules. Reloading :mod:`sys`, :mod:`__main__`, :mod:`builtins` and other 211 key modules is not recommended. In many cases extension modules are not 212 designed to be initialized more than once, and may fail in arbitrary ways 213 when reloaded. 214 215 If a module imports objects from another module using :keyword:`from` ... 216 :keyword:`import` ..., calling :func:`reload` for the other module does not 217 redefine the objects imported from it --- one way around this is to 218 re-execute the :keyword:`!from` statement, another is to use :keyword:`!import` 219 and qualified names (*module.name*) instead. 220 221 If a module instantiates instances of a class, reloading the module that 222 defines the class does not affect the method definitions of the instances --- 223 they continue to use the old class definition. The same is true for derived 224 classes. 225 226 .. versionadded:: 3.4 227 .. versionchanged:: 3.7 228 :exc:`ModuleNotFoundError` is raised when the module being reloaded lacks 229 a :class:`~importlib.machinery.ModuleSpec`. 230 231 232:mod:`importlib.abc` -- Abstract base classes related to import 233--------------------------------------------------------------- 234 235.. module:: importlib.abc 236 :synopsis: Abstract base classes related to import 237 238**Source code:** :source:`Lib/importlib/abc.py` 239 240-------------- 241 242 243The :mod:`importlib.abc` module contains all of the core abstract base classes 244used by :keyword:`import`. Some subclasses of the core abstract base classes 245are also provided to help in implementing the core ABCs. 246 247ABC hierarchy:: 248 249 object 250 +-- Finder (deprecated) 251 +-- MetaPathFinder 252 +-- PathEntryFinder 253 +-- Loader 254 +-- ResourceLoader --------+ 255 +-- InspectLoader | 256 +-- ExecutionLoader --+ 257 +-- FileLoader 258 +-- SourceLoader 259 260 261.. class:: Finder 262 263 An abstract base class representing a :term:`finder`. 264 265 .. deprecated:: 3.3 266 Use :class:`MetaPathFinder` or :class:`PathEntryFinder` instead. 267 268 .. abstractmethod:: find_module(fullname, path=None) 269 270 An abstract method for finding a :term:`loader` for the specified 271 module. Originally specified in :pep:`302`, this method was meant 272 for use in :data:`sys.meta_path` and in the path-based import subsystem. 273 274 .. versionchanged:: 3.4 275 Returns ``None`` when called instead of raising 276 :exc:`NotImplementedError`. 277 278 .. deprecated:: 3.10 279 Implement :meth:`MetaPathFinder.find_spec` or 280 :meth:`PathEntryFinder.find_spec` instead. 281 282 283.. class:: MetaPathFinder 284 285 An abstract base class representing a :term:`meta path finder`. 286 287 .. versionadded:: 3.3 288 289 .. versionchanged:: 3.10 290 No longer a subclass of :class:`Finder`. 291 292 .. method:: find_spec(fullname, path, target=None) 293 294 An abstract method for finding a :term:`spec <module spec>` for 295 the specified module. If this is a top-level import, *path* will 296 be ``None``. Otherwise, this is a search for a subpackage or 297 module and *path* will be the value of :attr:`__path__` from the 298 parent package. If a spec cannot be found, ``None`` is returned. 299 When passed in, ``target`` is a module object that the finder may 300 use to make a more educated guess about what spec to return. 301 :func:`importlib.util.spec_from_loader` may be useful for implementing 302 concrete ``MetaPathFinders``. 303 304 .. versionadded:: 3.4 305 306 .. method:: find_module(fullname, path) 307 308 A legacy method for finding a :term:`loader` for the specified 309 module. If this is a top-level import, *path* will be ``None``. 310 Otherwise, this is a search for a subpackage or module and *path* 311 will be the value of :attr:`__path__` from the parent 312 package. If a loader cannot be found, ``None`` is returned. 313 314 If :meth:`find_spec` is defined, backwards-compatible functionality is 315 provided. 316 317 .. versionchanged:: 3.4 318 Returns ``None`` when called instead of raising 319 :exc:`NotImplementedError`. Can use :meth:`find_spec` to provide 320 functionality. 321 322 .. deprecated:: 3.4 323 Use :meth:`find_spec` instead. 324 325 .. method:: invalidate_caches() 326 327 An optional method which, when called, should invalidate any internal 328 cache used by the finder. Used by :func:`importlib.invalidate_caches` 329 when invalidating the caches of all finders on :data:`sys.meta_path`. 330 331 .. versionchanged:: 3.4 332 Returns ``None`` when called instead of ``NotImplemented``. 333 334 335.. class:: PathEntryFinder 336 337 An abstract base class representing a :term:`path entry finder`. Though 338 it bears some similarities to :class:`MetaPathFinder`, ``PathEntryFinder`` 339 is meant for use only within the path-based import subsystem provided 340 by :class:`importlib.machinery.PathFinder`. 341 342 .. versionadded:: 3.3 343 344 .. versionchanged:: 3.10 345 No longer a subclass of :class:`Finder`. 346 347 .. method:: find_spec(fullname, target=None) 348 349 An abstract method for finding a :term:`spec <module spec>` for 350 the specified module. The finder will search for the module only 351 within the :term:`path entry` to which it is assigned. If a spec 352 cannot be found, ``None`` is returned. When passed in, ``target`` 353 is a module object that the finder may use to make a more educated 354 guess about what spec to return. :func:`importlib.util.spec_from_loader` 355 may be useful for implementing concrete ``PathEntryFinders``. 356 357 .. versionadded:: 3.4 358 359 .. method:: find_loader(fullname) 360 361 A legacy method for finding a :term:`loader` for the specified 362 module. Returns a 2-tuple of ``(loader, portion)`` where ``portion`` 363 is a sequence of file system locations contributing to part of a namespace 364 package. The loader may be ``None`` while specifying ``portion`` to 365 signify the contribution of the file system locations to a namespace 366 package. An empty list can be used for ``portion`` to signify the loader 367 is not part of a namespace package. If ``loader`` is ``None`` and 368 ``portion`` is the empty list then no loader or location for a namespace 369 package were found (i.e. failure to find anything for the module). 370 371 If :meth:`find_spec` is defined then backwards-compatible functionality is 372 provided. 373 374 .. versionchanged:: 3.4 375 Returns ``(None, [])`` instead of raising :exc:`NotImplementedError`. 376 Uses :meth:`find_spec` when available to provide functionality. 377 378 .. deprecated:: 3.4 379 Use :meth:`find_spec` instead. 380 381 .. method:: find_module(fullname) 382 383 A concrete implementation of :meth:`Finder.find_module` which is 384 equivalent to ``self.find_loader(fullname)[0]``. 385 386 .. deprecated:: 3.4 387 Use :meth:`find_spec` instead. 388 389 .. method:: invalidate_caches() 390 391 An optional method which, when called, should invalidate any internal 392 cache used by the finder. Used by 393 :meth:`importlib.machinery.PathFinder.invalidate_caches` 394 when invalidating the caches of all cached finders. 395 396 397.. class:: Loader 398 399 An abstract base class for a :term:`loader`. 400 See :pep:`302` for the exact definition for a loader. 401 402 Loaders that wish to support resource reading should implement a 403 :meth:`get_resource_reader` method as specified by 404 :class:`importlib.resources.abc.ResourceReader`. 405 406 .. versionchanged:: 3.7 407 Introduced the optional :meth:`get_resource_reader` method. 408 409 .. method:: create_module(spec) 410 411 A method that returns the module object to use when 412 importing a module. This method may return ``None``, 413 indicating that default module creation semantics should take place. 414 415 .. versionadded:: 3.4 416 417 .. versionchanged:: 3.6 418 This method is no longer optional when 419 :meth:`exec_module` is defined. 420 421 .. method:: exec_module(module) 422 423 An abstract method that executes the module in its own namespace 424 when a module is imported or reloaded. The module should already 425 be initialized when :meth:`exec_module` is called. When this method exists, 426 :meth:`create_module` must be defined. 427 428 .. versionadded:: 3.4 429 430 .. versionchanged:: 3.6 431 :meth:`create_module` must also be defined. 432 433 .. method:: load_module(fullname) 434 435 A legacy method for loading a module. If the module cannot be 436 loaded, :exc:`ImportError` is raised, otherwise the loaded module is 437 returned. 438 439 If the requested module already exists in :data:`sys.modules`, that 440 module should be used and reloaded. 441 Otherwise the loader should create a new module and insert it into 442 :data:`sys.modules` before any loading begins, to prevent recursion 443 from the import. If the loader inserted a module and the load fails, it 444 must be removed by the loader from :data:`sys.modules`; modules already 445 in :data:`sys.modules` before the loader began execution should be left 446 alone (see :func:`importlib.util.module_for_loader`). 447 448 The loader should set several attributes on the module 449 (note that some of these attributes can change when a module is 450 reloaded): 451 452 - :attr:`__name__` 453 The module's fully qualified name. 454 It is ``'__main__'`` for an executed module. 455 456 - :attr:`__file__` 457 The location the :term:`loader` used to load the module. 458 For example, for modules loaded from a .py file this is the filename. 459 It is not set on all modules (e.g. built-in modules). 460 461 - :attr:`__cached__` 462 The filename of a compiled version of the module's code. 463 It is not set on all modules (e.g. built-in modules). 464 465 - :attr:`__path__` 466 The list of locations where the package's submodules will be found. 467 Most of the time this is a single directory. 468 The import system passes this attribute to ``__import__()`` and to finders 469 in the same way as :attr:`sys.path` but just for the package. 470 It is not set on non-package modules so it can be used 471 as an indicator that the module is a package. 472 473 - :attr:`__package__` 474 The fully qualified name of the package the module is in (or the 475 empty string for a top-level module). 476 If the module is a package then this is the same as :attr:`__name__`. 477 478 - :attr:`__loader__` 479 The :term:`loader` used to load the module. 480 481 When :meth:`exec_module` is available then backwards-compatible 482 functionality is provided. 483 484 .. versionchanged:: 3.4 485 Raise :exc:`ImportError` when called instead of 486 :exc:`NotImplementedError`. Functionality provided when 487 :meth:`exec_module` is available. 488 489 .. deprecated:: 3.4 490 The recommended API for loading a module is :meth:`exec_module` 491 (and :meth:`create_module`). Loaders should implement it instead of 492 :meth:`load_module`. The import machinery takes care of all the 493 other responsibilities of :meth:`load_module` when 494 :meth:`exec_module` is implemented. 495 496 .. method:: module_repr(module) 497 498 A legacy method which when implemented calculates and returns the given 499 module's representation, as a string. The module type's default 500 :meth:`__repr__` will use the result of this method as appropriate. 501 502 .. versionadded:: 3.3 503 504 .. versionchanged:: 3.4 505 Made optional instead of an abstractmethod. 506 507 .. deprecated:: 3.4 508 The import machinery now takes care of this automatically. 509 510 511.. class:: ResourceLoader 512 513 An abstract base class for a :term:`loader` which implements the optional 514 :pep:`302` protocol for loading arbitrary resources from the storage 515 back-end. 516 517 .. deprecated:: 3.7 518 This ABC is deprecated in favour of supporting resource loading 519 through :class:`importlib.resources.abc.ResourceReader`. 520 521 .. abstractmethod:: get_data(path) 522 523 An abstract method to return the bytes for the data located at *path*. 524 Loaders that have a file-like storage back-end 525 that allows storing arbitrary data 526 can implement this abstract method to give direct access 527 to the data stored. :exc:`OSError` is to be raised if the *path* cannot 528 be found. The *path* is expected to be constructed using a module's 529 :attr:`__file__` attribute or an item from a package's :attr:`__path__`. 530 531 .. versionchanged:: 3.4 532 Raises :exc:`OSError` instead of :exc:`NotImplementedError`. 533 534 535.. class:: InspectLoader 536 537 An abstract base class for a :term:`loader` which implements the optional 538 :pep:`302` protocol for loaders that inspect modules. 539 540 .. method:: get_code(fullname) 541 542 Return the code object for a module, or ``None`` if the module does not 543 have a code object (as would be the case, for example, for a built-in 544 module). Raise an :exc:`ImportError` if loader cannot find the 545 requested module. 546 547 .. note:: 548 While the method has a default implementation, it is suggested that 549 it be overridden if possible for performance. 550 551 .. index:: 552 single: universal newlines; importlib.abc.InspectLoader.get_source method 553 554 .. versionchanged:: 3.4 555 No longer abstract and a concrete implementation is provided. 556 557 .. abstractmethod:: get_source(fullname) 558 559 An abstract method to return the source of a module. It is returned as 560 a text string using :term:`universal newlines`, translating all 561 recognized line separators into ``'\n'`` characters. Returns ``None`` 562 if no source is available (e.g. a built-in module). Raises 563 :exc:`ImportError` if the loader cannot find the module specified. 564 565 .. versionchanged:: 3.4 566 Raises :exc:`ImportError` instead of :exc:`NotImplementedError`. 567 568 .. method:: is_package(fullname) 569 570 An optional method to return a true value if the module is a package, a 571 false value otherwise. :exc:`ImportError` is raised if the 572 :term:`loader` cannot find the module. 573 574 .. versionchanged:: 3.4 575 Raises :exc:`ImportError` instead of :exc:`NotImplementedError`. 576 577 .. staticmethod:: source_to_code(data, path='<string>') 578 579 Create a code object from Python source. 580 581 The *data* argument can be whatever the :func:`compile` function 582 supports (i.e. string or bytes). The *path* argument should be 583 the "path" to where the source code originated from, which can be an 584 abstract concept (e.g. location in a zip file). 585 586 With the subsequent code object one can execute it in a module by 587 running ``exec(code, module.__dict__)``. 588 589 .. versionadded:: 3.4 590 591 .. versionchanged:: 3.5 592 Made the method static. 593 594 .. method:: exec_module(module) 595 596 Implementation of :meth:`Loader.exec_module`. 597 598 .. versionadded:: 3.4 599 600 .. method:: load_module(fullname) 601 602 Implementation of :meth:`Loader.load_module`. 603 604 .. deprecated:: 3.4 605 use :meth:`exec_module` instead. 606 607 608.. class:: ExecutionLoader 609 610 An abstract base class which inherits from :class:`InspectLoader` that, 611 when implemented, helps a module to be executed as a script. The ABC 612 represents an optional :pep:`302` protocol. 613 614 .. abstractmethod:: get_filename(fullname) 615 616 An abstract method that is to return the value of :attr:`__file__` for 617 the specified module. If no path is available, :exc:`ImportError` is 618 raised. 619 620 If source code is available, then the method should return the path to 621 the source file, regardless of whether a bytecode was used to load the 622 module. 623 624 .. versionchanged:: 3.4 625 Raises :exc:`ImportError` instead of :exc:`NotImplementedError`. 626 627 628.. class:: FileLoader(fullname, path) 629 630 An abstract base class which inherits from :class:`ResourceLoader` and 631 :class:`ExecutionLoader`, providing concrete implementations of 632 :meth:`ResourceLoader.get_data` and :meth:`ExecutionLoader.get_filename`. 633 634 The *fullname* argument is a fully resolved name of the module the loader is 635 to handle. The *path* argument is the path to the file for the module. 636 637 .. versionadded:: 3.3 638 639 .. attribute:: name 640 641 The name of the module the loader can handle. 642 643 .. attribute:: path 644 645 Path to the file of the module. 646 647 .. method:: load_module(fullname) 648 649 Calls super's ``load_module()``. 650 651 .. deprecated:: 3.4 652 Use :meth:`Loader.exec_module` instead. 653 654 .. abstractmethod:: get_filename(fullname) 655 656 Returns :attr:`path`. 657 658 .. abstractmethod:: get_data(path) 659 660 Reads *path* as a binary file and returns the bytes from it. 661 662 663.. class:: SourceLoader 664 665 An abstract base class for implementing source (and optionally bytecode) 666 file loading. The class inherits from both :class:`ResourceLoader` and 667 :class:`ExecutionLoader`, requiring the implementation of: 668 669 * :meth:`ResourceLoader.get_data` 670 * :meth:`ExecutionLoader.get_filename` 671 Should only return the path to the source file; sourceless 672 loading is not supported. 673 674 The abstract methods defined by this class are to add optional bytecode 675 file support. Not implementing these optional methods (or causing them to 676 raise :exc:`NotImplementedError`) causes the loader to 677 only work with source code. Implementing the methods allows the loader to 678 work with source *and* bytecode files; it does not allow for *sourceless* 679 loading where only bytecode is provided. Bytecode files are an 680 optimization to speed up loading by removing the parsing step of Python's 681 compiler, and so no bytecode-specific API is exposed. 682 683 .. method:: path_stats(path) 684 685 Optional abstract method which returns a :class:`dict` containing 686 metadata about the specified path. Supported dictionary keys are: 687 688 - ``'mtime'`` (mandatory): an integer or floating-point number 689 representing the modification time of the source code; 690 - ``'size'`` (optional): the size in bytes of the source code. 691 692 Any other keys in the dictionary are ignored, to allow for future 693 extensions. If the path cannot be handled, :exc:`OSError` is raised. 694 695 .. versionadded:: 3.3 696 697 .. versionchanged:: 3.4 698 Raise :exc:`OSError` instead of :exc:`NotImplementedError`. 699 700 .. method:: path_mtime(path) 701 702 Optional abstract method which returns the modification time for the 703 specified path. 704 705 .. deprecated:: 3.3 706 This method is deprecated in favour of :meth:`path_stats`. You don't 707 have to implement it, but it is still available for compatibility 708 purposes. Raise :exc:`OSError` if the path cannot be handled. 709 710 .. versionchanged:: 3.4 711 Raise :exc:`OSError` instead of :exc:`NotImplementedError`. 712 713 .. method:: set_data(path, data) 714 715 Optional abstract method which writes the specified bytes to a file 716 path. Any intermediate directories which do not exist are to be created 717 automatically. 718 719 When writing to the path fails because the path is read-only 720 (:attr:`errno.EACCES`/:exc:`PermissionError`), do not propagate the 721 exception. 722 723 .. versionchanged:: 3.4 724 No longer raises :exc:`NotImplementedError` when called. 725 726 .. method:: get_code(fullname) 727 728 Concrete implementation of :meth:`InspectLoader.get_code`. 729 730 .. method:: exec_module(module) 731 732 Concrete implementation of :meth:`Loader.exec_module`. 733 734 .. versionadded:: 3.4 735 736 .. method:: load_module(fullname) 737 738 Concrete implementation of :meth:`Loader.load_module`. 739 740 .. deprecated:: 3.4 741 Use :meth:`exec_module` instead. 742 743 .. method:: get_source(fullname) 744 745 Concrete implementation of :meth:`InspectLoader.get_source`. 746 747 .. method:: is_package(fullname) 748 749 Concrete implementation of :meth:`InspectLoader.is_package`. A module 750 is determined to be a package if its file path (as provided by 751 :meth:`ExecutionLoader.get_filename`) is a file named 752 ``__init__`` when the file extension is removed **and** the module name 753 itself does not end in ``__init__``. 754 755 756 757:mod:`importlib.machinery` -- Importers and path hooks 758------------------------------------------------------ 759 760.. module:: importlib.machinery 761 :synopsis: Importers and path hooks 762 763**Source code:** :source:`Lib/importlib/machinery.py` 764 765-------------- 766 767This module contains the various objects that help :keyword:`import` 768find and load modules. 769 770.. attribute:: SOURCE_SUFFIXES 771 772 A list of strings representing the recognized file suffixes for source 773 modules. 774 775 .. versionadded:: 3.3 776 777.. attribute:: DEBUG_BYTECODE_SUFFIXES 778 779 A list of strings representing the file suffixes for non-optimized bytecode 780 modules. 781 782 .. versionadded:: 3.3 783 784 .. deprecated:: 3.5 785 Use :attr:`BYTECODE_SUFFIXES` instead. 786 787.. attribute:: OPTIMIZED_BYTECODE_SUFFIXES 788 789 A list of strings representing the file suffixes for optimized bytecode 790 modules. 791 792 .. versionadded:: 3.3 793 794 .. deprecated:: 3.5 795 Use :attr:`BYTECODE_SUFFIXES` instead. 796 797.. attribute:: BYTECODE_SUFFIXES 798 799 A list of strings representing the recognized file suffixes for bytecode 800 modules (including the leading dot). 801 802 .. versionadded:: 3.3 803 804 .. versionchanged:: 3.5 805 The value is no longer dependent on ``__debug__``. 806 807.. attribute:: EXTENSION_SUFFIXES 808 809 A list of strings representing the recognized file suffixes for 810 extension modules. 811 812 .. versionadded:: 3.3 813 814.. function:: all_suffixes() 815 816 Returns a combined list of strings representing all file suffixes for 817 modules recognized by the standard import machinery. This is a 818 helper for code which simply needs to know if a filesystem path 819 potentially refers to a module without needing any details on the kind 820 of module (for example, :func:`inspect.getmodulename`). 821 822 .. versionadded:: 3.3 823 824 825.. class:: BuiltinImporter 826 827 An :term:`importer` for built-in modules. All known built-in modules are 828 listed in :data:`sys.builtin_module_names`. This class implements the 829 :class:`importlib.abc.MetaPathFinder` and 830 :class:`importlib.abc.InspectLoader` ABCs. 831 832 Only class methods are defined by this class to alleviate the need for 833 instantiation. 834 835 .. versionchanged:: 3.5 836 As part of :pep:`489`, the builtin importer now implements 837 :meth:`Loader.create_module` and :meth:`Loader.exec_module` 838 839 840.. class:: FrozenImporter 841 842 An :term:`importer` for frozen modules. This class implements the 843 :class:`importlib.abc.MetaPathFinder` and 844 :class:`importlib.abc.InspectLoader` ABCs. 845 846 Only class methods are defined by this class to alleviate the need for 847 instantiation. 848 849 .. versionchanged:: 3.4 850 Gained :meth:`~Loader.create_module` and :meth:`~Loader.exec_module` 851 methods. 852 853 854.. class:: WindowsRegistryFinder 855 856 :term:`Finder <finder>` for modules declared in the Windows registry. This class 857 implements the :class:`importlib.abc.MetaPathFinder` ABC. 858 859 Only class methods are defined by this class to alleviate the need for 860 instantiation. 861 862 .. versionadded:: 3.3 863 864 .. deprecated:: 3.6 865 Use :mod:`site` configuration instead. Future versions of Python may 866 not enable this finder by default. 867 868 869.. class:: PathFinder 870 871 A :term:`Finder <finder>` for :data:`sys.path` and package ``__path__`` attributes. 872 This class implements the :class:`importlib.abc.MetaPathFinder` ABC. 873 874 Only class methods are defined by this class to alleviate the need for 875 instantiation. 876 877 .. classmethod:: find_spec(fullname, path=None, target=None) 878 879 Class method that attempts to find a :term:`spec <module spec>` 880 for the module specified by *fullname* on :data:`sys.path` or, if 881 defined, on *path*. For each path entry that is searched, 882 :data:`sys.path_importer_cache` is checked. If a non-false object 883 is found then it is used as the :term:`path entry finder` to look 884 for the module being searched for. If no entry is found in 885 :data:`sys.path_importer_cache`, then :data:`sys.path_hooks` is 886 searched for a finder for the path entry and, if found, is stored 887 in :data:`sys.path_importer_cache` along with being queried about 888 the module. If no finder is ever found then ``None`` is both 889 stored in the cache and returned. 890 891 .. versionadded:: 3.4 892 893 .. versionchanged:: 3.5 894 If the current working directory -- represented by an empty string -- 895 is no longer valid then ``None`` is returned but no value is cached 896 in :data:`sys.path_importer_cache`. 897 898 .. classmethod:: find_module(fullname, path=None) 899 900 A legacy wrapper around :meth:`find_spec`. 901 902 .. deprecated:: 3.4 903 Use :meth:`find_spec` instead. 904 905 .. classmethod:: invalidate_caches() 906 907 Calls :meth:`importlib.abc.PathEntryFinder.invalidate_caches` on all 908 finders stored in :data:`sys.path_importer_cache` that define the method. 909 Otherwise entries in :data:`sys.path_importer_cache` set to ``None`` are 910 deleted. 911 912 .. versionchanged:: 3.7 913 Entries of ``None`` in :data:`sys.path_importer_cache` are deleted. 914 915 .. versionchanged:: 3.4 916 Calls objects in :data:`sys.path_hooks` with the current working 917 directory for ``''`` (i.e. the empty string). 918 919 920.. class:: FileFinder(path, *loader_details) 921 922 A concrete implementation of :class:`importlib.abc.PathEntryFinder` which 923 caches results from the file system. 924 925 The *path* argument is the directory for which the finder is in charge of 926 searching. 927 928 The *loader_details* argument is a variable number of 2-item tuples each 929 containing a loader and a sequence of file suffixes the loader recognizes. 930 The loaders are expected to be callables which accept two arguments of 931 the module's name and the path to the file found. 932 933 The finder will cache the directory contents as necessary, making stat calls 934 for each module search to verify the cache is not outdated. Because cache 935 staleness relies upon the granularity of the operating system's state 936 information of the file system, there is a potential race condition of 937 searching for a module, creating a new file, and then searching for the 938 module the new file represents. If the operations happen fast enough to fit 939 within the granularity of stat calls, then the module search will fail. To 940 prevent this from happening, when you create a module dynamically, make sure 941 to call :func:`importlib.invalidate_caches`. 942 943 .. versionadded:: 3.3 944 945 .. attribute:: path 946 947 The path the finder will search in. 948 949 .. method:: find_spec(fullname, target=None) 950 951 Attempt to find the spec to handle *fullname* within :attr:`path`. 952 953 .. versionadded:: 3.4 954 955 .. method:: find_loader(fullname) 956 957 Attempt to find the loader to handle *fullname* within :attr:`path`. 958 959 .. deprecated:: 3.10 960 Use :meth:`find_spec` instead. 961 962 .. method:: invalidate_caches() 963 964 Clear out the internal cache. 965 966 .. classmethod:: path_hook(*loader_details) 967 968 A class method which returns a closure for use on :attr:`sys.path_hooks`. 969 An instance of :class:`FileFinder` is returned by the closure using the 970 path argument given to the closure directly and *loader_details* 971 indirectly. 972 973 If the argument to the closure is not an existing directory, 974 :exc:`ImportError` is raised. 975 976 977.. class:: SourceFileLoader(fullname, path) 978 979 A concrete implementation of :class:`importlib.abc.SourceLoader` by 980 subclassing :class:`importlib.abc.FileLoader` and providing some concrete 981 implementations of other methods. 982 983 .. versionadded:: 3.3 984 985 .. attribute:: name 986 987 The name of the module that this loader will handle. 988 989 .. attribute:: path 990 991 The path to the source file. 992 993 .. method:: is_package(fullname) 994 995 Return ``True`` if :attr:`path` appears to be for a package. 996 997 .. method:: path_stats(path) 998 999 Concrete implementation of :meth:`importlib.abc.SourceLoader.path_stats`. 1000 1001 .. method:: set_data(path, data) 1002 1003 Concrete implementation of :meth:`importlib.abc.SourceLoader.set_data`. 1004 1005 .. method:: load_module(name=None) 1006 1007 Concrete implementation of :meth:`importlib.abc.Loader.load_module` where 1008 specifying the name of the module to load is optional. 1009 1010 .. deprecated:: 3.6 1011 1012 Use :meth:`importlib.abc.Loader.exec_module` instead. 1013 1014 1015.. class:: SourcelessFileLoader(fullname, path) 1016 1017 A concrete implementation of :class:`importlib.abc.FileLoader` which can 1018 import bytecode files (i.e. no source code files exist). 1019 1020 Please note that direct use of bytecode files (and thus not source code 1021 files) inhibits your modules from being usable by all Python 1022 implementations or new versions of Python which change the bytecode 1023 format. 1024 1025 .. versionadded:: 3.3 1026 1027 .. attribute:: name 1028 1029 The name of the module the loader will handle. 1030 1031 .. attribute:: path 1032 1033 The path to the bytecode file. 1034 1035 .. method:: is_package(fullname) 1036 1037 Determines if the module is a package based on :attr:`path`. 1038 1039 .. method:: get_code(fullname) 1040 1041 Returns the code object for :attr:`name` created from :attr:`path`. 1042 1043 .. method:: get_source(fullname) 1044 1045 Returns ``None`` as bytecode files have no source when this loader is 1046 used. 1047 1048 .. method:: load_module(name=None) 1049 1050 Concrete implementation of :meth:`importlib.abc.Loader.load_module` where 1051 specifying the name of the module to load is optional. 1052 1053 .. deprecated:: 3.6 1054 1055 Use :meth:`importlib.abc.Loader.exec_module` instead. 1056 1057 1058.. class:: ExtensionFileLoader(fullname, path) 1059 1060 A concrete implementation of :class:`importlib.abc.ExecutionLoader` for 1061 extension modules. 1062 1063 The *fullname* argument specifies the name of the module the loader is to 1064 support. The *path* argument is the path to the extension module's file. 1065 1066 .. versionadded:: 3.3 1067 1068 .. attribute:: name 1069 1070 Name of the module the loader supports. 1071 1072 .. attribute:: path 1073 1074 Path to the extension module. 1075 1076 .. method:: create_module(spec) 1077 1078 Creates the module object from the given specification in accordance 1079 with :pep:`489`. 1080 1081 .. versionadded:: 3.5 1082 1083 .. method:: exec_module(module) 1084 1085 Initializes the given module object in accordance with :pep:`489`. 1086 1087 .. versionadded:: 3.5 1088 1089 .. method:: is_package(fullname) 1090 1091 Returns ``True`` if the file path points to a package's ``__init__`` 1092 module based on :attr:`EXTENSION_SUFFIXES`. 1093 1094 .. method:: get_code(fullname) 1095 1096 Returns ``None`` as extension modules lack a code object. 1097 1098 .. method:: get_source(fullname) 1099 1100 Returns ``None`` as extension modules do not have source code. 1101 1102 .. method:: get_filename(fullname) 1103 1104 Returns :attr:`path`. 1105 1106 .. versionadded:: 3.4 1107 1108 1109.. class:: NamespaceLoader(name, path, path_finder): 1110 1111 A concrete implementation of :class:`importlib.abc.InspectLoader` for 1112 namespace packages. This is an alias for a private class and is only made 1113 public for introspecting the ``__loader__`` attribute on namespace 1114 packages:: 1115 1116 >>> from importlib.machinery import NamespaceLoader 1117 >>> import my_namespace 1118 >>> isinstance(my_namespace.__loader__, NamespaceLoader) 1119 True 1120 >>> import importlib.abc 1121 >>> isinstance(my_namespace.__loader__, importlib.abc.Loader) 1122 True 1123 1124 .. versionadded:: 3.11 1125 1126 1127.. class:: ModuleSpec(name, loader, *, origin=None, loader_state=None, is_package=None) 1128 1129 A specification for a module's import-system-related state. This is 1130 typically exposed as the module's :attr:`__spec__` attribute. In the 1131 descriptions below, the names in parentheses give the corresponding 1132 attribute available directly on the module object, 1133 e.g. ``module.__spec__.origin == module.__file__``. Note, however, that 1134 while the *values* are usually equivalent, they can differ since there is 1135 no synchronization between the two objects. For example, it is possible to update 1136 the module's :attr:`__file__` at runtime and this will not be automatically 1137 reflected in the module's :attr:`__spec__.origin`, and vice versa. 1138 1139 .. versionadded:: 3.4 1140 1141 .. attribute:: name 1142 1143 (:attr:`__name__`) 1144 1145 The module's fully qualified name. 1146 The :term:`finder` should always set this attribute to a non-empty string. 1147 1148 .. attribute:: loader 1149 1150 (:attr:`__loader__`) 1151 1152 The :term:`loader` used to load the module. 1153 The :term:`finder` should always set this attribute. 1154 1155 .. attribute:: origin 1156 1157 (:attr:`__file__`) 1158 1159 The location the :term:`loader` should use to load the module. 1160 For example, for modules loaded from a .py file this is the filename. 1161 The :term:`finder` should always set this attribute to a meaningful value 1162 for the :term:`loader` to use. In the uncommon case that there is not one 1163 (like for namespace packages), it should be set to ``None``. 1164 1165 .. attribute:: submodule_search_locations 1166 1167 (:attr:`__path__`) 1168 1169 The list of locations where the package's submodules will be found. 1170 Most of the time this is a single directory. 1171 The :term:`finder` should set this attribute to a list, even an empty one, to indicate 1172 to the import system that the module is a package. It should be set to ``None`` for 1173 non-package modules. It is set automatically later to a special object for 1174 namespace packages. 1175 1176 .. attribute:: loader_state 1177 1178 The :term:`finder` may set this attribute to an object containing additional, 1179 module-specific data to use when loading the module. Otherwise it should be 1180 set to ``None``. 1181 1182 .. attribute:: cached 1183 1184 (:attr:`__cached__`) 1185 1186 The filename of a compiled version of the module's code. 1187 The :term:`finder` should always set this attribute but it may be ``None`` 1188 for modules that do not need compiled code stored. 1189 1190 .. attribute:: parent 1191 1192 (:attr:`__package__`) 1193 1194 (Read-only) The fully qualified name of the package the module is in (or the 1195 empty string for a top-level module). 1196 If the module is a package then this is the same as :attr:`name`. 1197 1198 .. attribute:: has_location 1199 1200 ``True`` if the spec's :attr:`origin` refers to a loadable location, 1201 ``False`` otherwise. This value impacts how :attr:`origin` is interpreted 1202 and how the module's :attr:`__file__` is populated. 1203 1204 1205:mod:`importlib.util` -- Utility code for importers 1206--------------------------------------------------- 1207 1208.. module:: importlib.util 1209 :synopsis: Utility code for importers 1210 1211 1212**Source code:** :source:`Lib/importlib/util.py` 1213 1214-------------- 1215 1216This module contains the various objects that help in the construction of 1217an :term:`importer`. 1218 1219.. attribute:: MAGIC_NUMBER 1220 1221 The bytes which represent the bytecode version number. If you need help with 1222 loading/writing bytecode then consider :class:`importlib.abc.SourceLoader`. 1223 1224 .. versionadded:: 3.4 1225 1226.. function:: cache_from_source(path, debug_override=None, *, optimization=None) 1227 1228 Return the :pep:`3147`/:pep:`488` path to the byte-compiled file associated 1229 with the source *path*. For example, if *path* is ``/foo/bar/baz.py`` the return 1230 value would be ``/foo/bar/__pycache__/baz.cpython-32.pyc`` for Python 3.2. 1231 The ``cpython-32`` string comes from the current magic tag (see 1232 :func:`get_tag`; if :attr:`sys.implementation.cache_tag` is not defined then 1233 :exc:`NotImplementedError` will be raised). 1234 1235 The *optimization* parameter is used to specify the optimization level of the 1236 bytecode file. An empty string represents no optimization, so 1237 ``/foo/bar/baz.py`` with an *optimization* of ``''`` will result in a 1238 bytecode path of ``/foo/bar/__pycache__/baz.cpython-32.pyc``. ``None`` causes 1239 the interpreter's optimization level to be used. Any other value's string 1240 representation is used, so ``/foo/bar/baz.py`` with an *optimization* of 1241 ``2`` will lead to the bytecode path of 1242 ``/foo/bar/__pycache__/baz.cpython-32.opt-2.pyc``. The string representation 1243 of *optimization* can only be alphanumeric, else :exc:`ValueError` is raised. 1244 1245 The *debug_override* parameter is deprecated and can be used to override 1246 the system's value for ``__debug__``. A ``True`` value is the equivalent of 1247 setting *optimization* to the empty string. A ``False`` value is the same as 1248 setting *optimization* to ``1``. If both *debug_override* an *optimization* 1249 are not ``None`` then :exc:`TypeError` is raised. 1250 1251 .. versionadded:: 3.4 1252 1253 .. versionchanged:: 3.5 1254 The *optimization* parameter was added and the *debug_override* parameter 1255 was deprecated. 1256 1257 .. versionchanged:: 3.6 1258 Accepts a :term:`path-like object`. 1259 1260 1261.. function:: source_from_cache(path) 1262 1263 Given the *path* to a :pep:`3147` file name, return the associated source code 1264 file path. For example, if *path* is 1265 ``/foo/bar/__pycache__/baz.cpython-32.pyc`` the returned path would be 1266 ``/foo/bar/baz.py``. *path* need not exist, however if it does not conform 1267 to :pep:`3147` or :pep:`488` format, a :exc:`ValueError` is raised. If 1268 :attr:`sys.implementation.cache_tag` is not defined, 1269 :exc:`NotImplementedError` is raised. 1270 1271 .. versionadded:: 3.4 1272 1273 .. versionchanged:: 3.6 1274 Accepts a :term:`path-like object`. 1275 1276.. function:: decode_source(source_bytes) 1277 1278 Decode the given bytes representing source code and return it as a string 1279 with universal newlines (as required by 1280 :meth:`importlib.abc.InspectLoader.get_source`). 1281 1282 .. versionadded:: 3.4 1283 1284.. function:: resolve_name(name, package) 1285 1286 Resolve a relative module name to an absolute one. 1287 1288 If **name** has no leading dots, then **name** is simply returned. This 1289 allows for usage such as 1290 ``importlib.util.resolve_name('sys', __spec__.parent)`` without doing a 1291 check to see if the **package** argument is needed. 1292 1293 :exc:`ImportError` is raised if **name** is a relative module name but 1294 **package** is a false value (e.g. ``None`` or the empty string). 1295 :exc:`ImportError` is also raised if a relative name would escape its 1296 containing package (e.g. requesting ``..bacon`` from within the ``spam`` 1297 package). 1298 1299 .. versionadded:: 3.3 1300 1301 .. versionchanged:: 3.9 1302 To improve consistency with import statements, raise 1303 :exc:`ImportError` instead of :exc:`ValueError` for invalid relative 1304 import attempts. 1305 1306.. function:: find_spec(name, package=None) 1307 1308 Find the :term:`spec <module spec>` for a module, optionally relative to 1309 the specified **package** name. If the module is in :attr:`sys.modules`, 1310 then ``sys.modules[name].__spec__`` is returned (unless the spec would be 1311 ``None`` or is not set, in which case :exc:`ValueError` is raised). 1312 Otherwise a search using :attr:`sys.meta_path` is done. ``None`` is 1313 returned if no spec is found. 1314 1315 If **name** is for a submodule (contains a dot), the parent module is 1316 automatically imported. 1317 1318 **name** and **package** work the same as for :func:`import_module`. 1319 1320 .. versionadded:: 3.4 1321 1322 .. versionchanged:: 3.7 1323 Raises :exc:`ModuleNotFoundError` instead of :exc:`AttributeError` if 1324 **package** is in fact not a package (i.e. lacks a :attr:`__path__` 1325 attribute). 1326 1327.. function:: module_from_spec(spec) 1328 1329 Create a new module based on **spec** and 1330 :meth:`spec.loader.create_module <importlib.abc.Loader.create_module>`. 1331 1332 If :meth:`spec.loader.create_module <importlib.abc.Loader.create_module>` 1333 does not return ``None``, then any pre-existing attributes will not be reset. 1334 Also, no :exc:`AttributeError` will be raised if triggered while accessing 1335 **spec** or setting an attribute on the module. 1336 1337 This function is preferred over using :class:`types.ModuleType` to create a 1338 new module as **spec** is used to set as many import-controlled attributes on 1339 the module as possible. 1340 1341 .. versionadded:: 3.5 1342 1343.. decorator:: module_for_loader 1344 1345 A :term:`decorator` for :meth:`importlib.abc.Loader.load_module` 1346 to handle selecting the proper 1347 module object to load with. The decorated method is expected to have a call 1348 signature taking two positional arguments 1349 (e.g. ``load_module(self, module)``) for which the second argument 1350 will be the module **object** to be used by the loader. 1351 Note that the decorator will not work on static methods because of the 1352 assumption of two arguments. 1353 1354 The decorated method will take in the **name** of the module to be loaded 1355 as expected for a :term:`loader`. If the module is not found in 1356 :data:`sys.modules` then a new one is constructed. Regardless of where the 1357 module came from, :attr:`__loader__` set to **self** and :attr:`__package__` 1358 is set based on what :meth:`importlib.abc.InspectLoader.is_package` returns 1359 (if available). These attributes are set unconditionally to support 1360 reloading. 1361 1362 If an exception is raised by the decorated method and a module was added to 1363 :data:`sys.modules`, then the module will be removed to prevent a partially 1364 initialized module from being in left in :data:`sys.modules`. If the module 1365 was already in :data:`sys.modules` then it is left alone. 1366 1367 .. versionchanged:: 3.3 1368 :attr:`__loader__` and :attr:`__package__` are automatically set 1369 (when possible). 1370 1371 .. versionchanged:: 3.4 1372 Set :attr:`__name__`, :attr:`__loader__` :attr:`__package__` 1373 unconditionally to support reloading. 1374 1375 .. deprecated:: 3.4 1376 The import machinery now directly performs all the functionality 1377 provided by this function. 1378 1379.. decorator:: set_loader 1380 1381 A :term:`decorator` for :meth:`importlib.abc.Loader.load_module` 1382 to set the :attr:`__loader__` 1383 attribute on the returned module. If the attribute is already set the 1384 decorator does nothing. It is assumed that the first positional argument to 1385 the wrapped method (i.e. ``self``) is what :attr:`__loader__` should be set 1386 to. 1387 1388 .. versionchanged:: 3.4 1389 Set ``__loader__`` if set to ``None``, as if the attribute does not 1390 exist. 1391 1392 .. deprecated:: 3.4 1393 The import machinery takes care of this automatically. 1394 1395.. decorator:: set_package 1396 1397 A :term:`decorator` for :meth:`importlib.abc.Loader.load_module` to set the 1398 :attr:`__package__` attribute on the returned module. If :attr:`__package__` 1399 is set and has a value other than ``None`` it will not be changed. 1400 1401 .. deprecated:: 3.4 1402 The import machinery takes care of this automatically. 1403 1404.. function:: spec_from_loader(name, loader, *, origin=None, is_package=None) 1405 1406 A factory function for creating a :class:`~importlib.machinery.ModuleSpec` 1407 instance based on a loader. The parameters have the same meaning as they do 1408 for ModuleSpec. The function uses available :term:`loader` APIs, such as 1409 :meth:`InspectLoader.is_package`, to fill in any missing 1410 information on the spec. 1411 1412 .. versionadded:: 3.4 1413 1414.. function:: spec_from_file_location(name, location, *, loader=None, submodule_search_locations=None) 1415 1416 A factory function for creating a :class:`~importlib.machinery.ModuleSpec` 1417 instance based on the path to a file. Missing information will be filled in 1418 on the spec by making use of loader APIs and by the implication that the 1419 module will be file-based. 1420 1421 .. versionadded:: 3.4 1422 1423 .. versionchanged:: 3.6 1424 Accepts a :term:`path-like object`. 1425 1426.. function:: source_hash(source_bytes) 1427 1428 Return the hash of *source_bytes* as bytes. A hash-based ``.pyc`` file embeds 1429 the :func:`source_hash` of the corresponding source file's contents in its 1430 header. 1431 1432 .. versionadded:: 3.7 1433 1434.. class:: LazyLoader(loader) 1435 1436 A class which postpones the execution of the loader of a module until the 1437 module has an attribute accessed. 1438 1439 This class **only** works with loaders that define 1440 :meth:`~importlib.abc.Loader.exec_module` as control over what module type 1441 is used for the module is required. For those same reasons, the loader's 1442 :meth:`~importlib.abc.Loader.create_module` method must return ``None`` or a 1443 type for which its ``__class__`` attribute can be mutated along with not 1444 using :term:`slots <__slots__>`. Finally, modules which substitute the object 1445 placed into :attr:`sys.modules` will not work as there is no way to properly 1446 replace the module references throughout the interpreter safely; 1447 :exc:`ValueError` is raised if such a substitution is detected. 1448 1449 .. note:: 1450 For projects where startup time is critical, this class allows for 1451 potentially minimizing the cost of loading a module if it is never used. 1452 For projects where startup time is not essential then use of this class is 1453 **heavily** discouraged due to error messages created during loading being 1454 postponed and thus occurring out of context. 1455 1456 .. versionadded:: 3.5 1457 1458 .. versionchanged:: 3.6 1459 Began calling :meth:`~importlib.abc.Loader.create_module`, removing the 1460 compatibility warning for :class:`importlib.machinery.BuiltinImporter` and 1461 :class:`importlib.machinery.ExtensionFileLoader`. 1462 1463 .. classmethod:: factory(loader) 1464 1465 A class method which returns a callable that creates a lazy loader. This 1466 is meant to be used in situations where the loader is passed by class 1467 instead of by instance. 1468 :: 1469 1470 suffixes = importlib.machinery.SOURCE_SUFFIXES 1471 loader = importlib.machinery.SourceFileLoader 1472 lazy_loader = importlib.util.LazyLoader.factory(loader) 1473 finder = importlib.machinery.FileFinder(path, (lazy_loader, suffixes)) 1474 1475.. _importlib-examples: 1476 1477Examples 1478-------- 1479 1480Importing programmatically 1481'''''''''''''''''''''''''' 1482 1483To programmatically import a module, use :func:`importlib.import_module`. 1484:: 1485 1486 import importlib 1487 1488 itertools = importlib.import_module('itertools') 1489 1490 1491Checking if a module can be imported 1492'''''''''''''''''''''''''''''''''''' 1493 1494If you need to find out if a module can be imported without actually doing the 1495import, then you should use :func:`importlib.util.find_spec`. 1496 1497Note that if ``name`` is a submodule (contains a dot), 1498:func:`importlib.util.find_spec` will import the parent module. 1499:: 1500 1501 import importlib.util 1502 import sys 1503 1504 # For illustrative purposes. 1505 name = 'itertools' 1506 1507 if name in sys.modules: 1508 print(f"{name!r} already in sys.modules") 1509 elif (spec := importlib.util.find_spec(name)) is not None: 1510 # If you chose to perform the actual import ... 1511 module = importlib.util.module_from_spec(spec) 1512 sys.modules[name] = module 1513 spec.loader.exec_module(module) 1514 print(f"{name!r} has been imported") 1515 else: 1516 print(f"can't find the {name!r} module") 1517 1518 1519Importing a source file directly 1520'''''''''''''''''''''''''''''''' 1521 1522To import a Python source file directly, use the following recipe:: 1523 1524 import importlib.util 1525 import sys 1526 1527 # For illustrative purposes. 1528 import tokenize 1529 file_path = tokenize.__file__ 1530 module_name = tokenize.__name__ 1531 1532 spec = importlib.util.spec_from_file_location(module_name, file_path) 1533 module = importlib.util.module_from_spec(spec) 1534 sys.modules[module_name] = module 1535 spec.loader.exec_module(module) 1536 1537 1538Implementing lazy imports 1539''''''''''''''''''''''''' 1540 1541The example below shows how to implement lazy imports:: 1542 1543 >>> import importlib.util 1544 >>> import sys 1545 >>> def lazy_import(name): 1546 ... spec = importlib.util.find_spec(name) 1547 ... loader = importlib.util.LazyLoader(spec.loader) 1548 ... spec.loader = loader 1549 ... module = importlib.util.module_from_spec(spec) 1550 ... sys.modules[name] = module 1551 ... loader.exec_module(module) 1552 ... return module 1553 ... 1554 >>> lazy_typing = lazy_import("typing") 1555 >>> #lazy_typing is a real module object, 1556 >>> #but it is not loaded in memory yet. 1557 >>> lazy_typing.TYPE_CHECKING 1558 False 1559 1560 1561 1562Setting up an importer 1563'''''''''''''''''''''' 1564 1565For deep customizations of import, you typically want to implement an 1566:term:`importer`. This means managing both the :term:`finder` and :term:`loader` 1567side of things. For finders there are two flavours to choose from depending on 1568your needs: a :term:`meta path finder` or a :term:`path entry finder`. The 1569former is what you would put on :attr:`sys.meta_path` while the latter is what 1570you create using a :term:`path entry hook` on :attr:`sys.path_hooks` which works 1571with :attr:`sys.path` entries to potentially create a finder. This example will 1572show you how to register your own importers so that import will use them (for 1573creating an importer for yourself, read the documentation for the appropriate 1574classes defined within this package):: 1575 1576 import importlib.machinery 1577 import sys 1578 1579 # For illustrative purposes only. 1580 SpamMetaPathFinder = importlib.machinery.PathFinder 1581 SpamPathEntryFinder = importlib.machinery.FileFinder 1582 loader_details = (importlib.machinery.SourceFileLoader, 1583 importlib.machinery.SOURCE_SUFFIXES) 1584 1585 # Setting up a meta path finder. 1586 # Make sure to put the finder in the proper location in the list in terms of 1587 # priority. 1588 sys.meta_path.append(SpamMetaPathFinder) 1589 1590 # Setting up a path entry finder. 1591 # Make sure to put the path hook in the proper location in the list in terms 1592 # of priority. 1593 sys.path_hooks.append(SpamPathEntryFinder.path_hook(loader_details)) 1594 1595 1596Approximating :func:`importlib.import_module` 1597''''''''''''''''''''''''''''''''''''''''''''' 1598 1599Import itself is implemented in Python code, making it possible to 1600expose most of the import machinery through importlib. The following 1601helps illustrate the various APIs that importlib exposes by providing an 1602approximate implementation of 1603:func:`importlib.import_module`:: 1604 1605 import importlib.util 1606 import sys 1607 1608 def import_module(name, package=None): 1609 """An approximate implementation of import.""" 1610 absolute_name = importlib.util.resolve_name(name, package) 1611 try: 1612 return sys.modules[absolute_name] 1613 except KeyError: 1614 pass 1615 1616 path = None 1617 if '.' in absolute_name: 1618 parent_name, _, child_name = absolute_name.rpartition('.') 1619 parent_module = import_module(parent_name) 1620 path = parent_module.__spec__.submodule_search_locations 1621 for finder in sys.meta_path: 1622 spec = finder.find_spec(absolute_name, path) 1623 if spec is not None: 1624 break 1625 else: 1626 msg = f'No module named {absolute_name!r}' 1627 raise ModuleNotFoundError(msg, name=absolute_name) 1628 module = importlib.util.module_from_spec(spec) 1629 sys.modules[absolute_name] = module 1630 spec.loader.exec_module(module) 1631 if path is not None: 1632 setattr(parent_module, child_name, module) 1633 return module 1634