@@ -383,11 +383,11 @@ ABC hierarchy::
383
383
See :pep: `302 ` for the exact definition for a loader.
384
384
385
385
Loaders that wish to support resource reading should implement a
386
- `` get_resource_reader(fullname) ` ` method as specified by
386
+ :meth: ` get_resource_reader ` method as specified by
387
387
:class: `importlib.abc.ResourceReader `.
388
388
389
389
.. versionchanged :: 3.7
390
- Introduced the optional `` get_resource_reader() ` ` method.
390
+ Introduced the optional :meth: ` get_resource_reader ` method.
391
391
392
392
.. method :: create_module(spec)
393
393
@@ -405,80 +405,82 @@ ABC hierarchy::
405
405
406
406
An abstract method that executes the module in its own namespace
407
407
when a module is imported or reloaded. The module should already
408
- be initialized when `` exec_module() `` is called. When this method exists,
409
- :meth: `~importlib.abc.Loader. create_module ` must be defined.
408
+ be initialized when :meth: ` exec_module ` is called. When this method exists,
409
+ :meth: `create_module ` must be defined.
410
410
411
411
.. versionadded :: 3.4
412
412
413
413
.. versionchanged :: 3.6
414
- :meth: `~importlib.abc.Loader. create_module ` must also be defined.
414
+ :meth: `create_module ` must also be defined.
415
415
416
416
.. method :: load_module(fullname)
417
417
418
- A legacy method for loading a module. If the module cannot be
418
+ A legacy method for loading a module. If the module cannot be
419
419
loaded, :exc: `ImportError ` is raised, otherwise the loaded module is
420
420
returned.
421
421
422
422
If the requested module already exists in :data: `sys.modules `, that
423
423
module should be used and reloaded.
424
424
Otherwise the loader should create a new module and insert it into
425
425
:data: `sys.modules ` before any loading begins, to prevent recursion
426
- from the import. If the loader inserted a module and the load fails, it
426
+ from the import. If the loader inserted a module and the load fails, it
427
427
must be removed by the loader from :data: `sys.modules `; modules already
428
428
in :data: `sys.modules ` before the loader began execution should be left
429
429
alone (see :func: `importlib.util.module_for_loader `).
430
430
431
- The loader should set several attributes on the module.
432
- (Note that some of these attributes can change when a module is
431
+ The loader should set several attributes on the module
432
+ (note that some of these attributes can change when a module is
433
433
reloaded):
434
434
435
435
- :attr: `__name__ `
436
- The name of the module.
436
+ The module's fully-qualified name.
437
+ It is ``'__main__' `` for an executed module.
437
438
438
439
- :attr: `__file__ `
439
- The path to where the module data is stored (not set for built-in
440
- modules).
440
+ The location the :term: `loader ` used to load the module.
441
+ For example, for modules loaded from a .py file this is the filename.
442
+ It is not set on all modules (e.g. built-in modules).
441
443
442
444
- :attr: `__cached__ `
443
- The path to where a compiled version of the module is/should be
444
- stored ( not set when the attribute would be inappropriate ).
445
+ The filename of a compiled version of the module's code.
446
+ It is not set on all modules (e.g. built-in modules ).
445
447
446
448
- :attr: `__path__ `
447
- A list of strings specifying the search path within a
448
- package. This attribute is not set on modules.
449
+ The list of locations where the package's submodules will be found.
450
+ Most of the time this is a single directory.
451
+ The import system passes this attribute to ``__import__() `` and to finders
452
+ in the same way as :attr: `sys.path ` but just for the package.
453
+ It is not set on non-package modules so it can be used
454
+ as an indicator that the module is a package.
449
455
450
456
- :attr: `__package__ `
451
- The fully-qualified name of the package under which the module was
452
- loaded as a submodule (or the empty string for top-level modules).
453
- For packages, it is the same as :attr: `__name__ `. The
454
- :func: `importlib.util.module_for_loader ` decorator can handle the
455
- details for :attr: `__package__ `.
457
+ The fully-qualified name of the package the module is in (or the
458
+ empty string for a top-level module).
459
+ If the module is a package then this is the same as :attr: `__name__ `.
456
460
457
461
- :attr: `__loader__ `
458
- The loader used to load the module. The
459
- :func: `importlib.util.module_for_loader ` decorator can handle the
460
- details for :attr: `__package__ `.
462
+ The :term: `loader ` used to load the module.
461
463
462
464
When :meth: `exec_module ` is available then backwards-compatible
463
465
functionality is provided.
464
466
465
467
.. versionchanged :: 3.4
466
468
Raise :exc: `ImportError ` when called instead of
467
- :exc: `NotImplementedError `. Functionality provided when
469
+ :exc: `NotImplementedError `. Functionality provided when
468
470
:meth: `exec_module ` is available.
469
471
470
472
.. deprecated :: 3.4
471
473
The recommended API for loading a module is :meth: `exec_module `
472
- (and :meth: `create_module `). Loaders should implement
473
- it instead of load_module() . The import machinery takes care of
474
- all the other responsibilities of load_module() when exec_module()
475
- is implemented.
474
+ (and :meth: `create_module `). Loaders should implement it instead of
475
+ :meth: ` load_module ` . The import machinery takes care of all the
476
+ other responsibilities of :meth: ` load_module ` when
477
+ :meth: ` exec_module ` is implemented.
476
478
477
479
.. method :: module_repr(module)
478
480
479
- A legacy method which when implemented calculates and returns the
480
- given module's repr , as a string. The module type's default repr() will
481
- use the result of this method as appropriate.
481
+ A legacy method which when implemented calculates and returns the given
482
+ module's representation , as a string. The module type's default
483
+ :meth: ` __repr__ ` will use the result of this method as appropriate.
482
484
483
485
.. versionadded :: 3.3
484
486
@@ -1420,69 +1422,80 @@ find and load modules.
1420
1422
.. class :: ModuleSpec(name, loader, *, origin=None, loader_state=None, is_package=None)
1421
1423
1422
1424
A specification for a module's import-system-related state. This is
1423
- typically exposed as the module's `` __spec__ ` ` attribute. In the
1425
+ typically exposed as the module's :attr: ` __spec__ ` attribute. In the
1424
1426
descriptions below, the names in parentheses give the corresponding
1425
- attribute available directly on the module object.
1426
- E .g. ``module.__spec__.origin == module.__file__ ``. Note however that
1427
+ attribute available directly on the module object,
1428
+ e .g. ``module.__spec__.origin == module.__file__ ``. Note, however, that
1427
1429
while the *values * are usually equivalent, they can differ since there is
1428
- no synchronization between the two objects. Thus it is possible to update
1429
- the module's `` __path__ `` at runtime, and this will not be automatically
1430
- reflected in `` __spec__.submodule_search_locations `` .
1430
+ no synchronization between the two objects. For example, it is possible to update
1431
+ the module's :attr: ` __file__ ` at runtime and this will not be automatically
1432
+ reflected in the module's :attr: ` __spec__.origin `, and vice versa .
1431
1433
1432
1434
.. versionadded :: 3.4
1433
1435
1434
1436
.. attribute :: name
1435
1437
1436
- (`` __name__ ` `)
1438
+ (:attr: ` __name__ `)
1437
1439
1438
- A string for the fully-qualified name of the module.
1440
+ The module's fully-qualified name.
1441
+ The :term: `finder ` should always set this attribute to a non-empty string.
1439
1442
1440
1443
.. attribute :: loader
1441
1444
1442
- (`` __loader__ ` `)
1445
+ (:attr: ` __loader__ `)
1443
1446
1444
- The :term: `Loader < loader> ` that should be used when loading
1445
- the module. :term: `Finders < finder> ` should always set this.
1447
+ The :term: `loader ` used to load the module.
1448
+ The :term: `finder ` should always set this attribute .
1446
1449
1447
1450
.. attribute :: origin
1448
1451
1449
- (`` __file__ ` `)
1452
+ (:attr: ` __file__ `)
1450
1453
1451
- Name of the place from which the module is loaded, e.g. "builtin" for
1452
- built-in modules and the filename for modules loaded from source.
1453
- Normally "origin" should be set, but it may be ``None `` (the default)
1454
- which indicates it is unspecified (e.g. for namespace packages).
1454
+ The location the :term: `loader ` should use to load the module.
1455
+ For example, for modules loaded from a .py file this is the filename.
1456
+ The :term: `finder ` should always set this attribute to a meaningful value
1457
+ for the :term: `loader ` to use. In the uncommon case that there is not one
1458
+ (like for namespace packages), it should be set to ``None ``.
1455
1459
1456
1460
.. attribute :: submodule_search_locations
1457
1461
1458
- (`` __path__ ` `)
1462
+ (:attr: ` __path__ `)
1459
1463
1460
- List of strings for where to find submodules, if a package (``None ``
1461
- otherwise).
1464
+ The list of locations where the package's submodules will be found.
1465
+ Most of the time this is a single directory.
1466
+ The :term: `finder ` should set this attribute to a list, even an empty one, to indicate
1467
+ to the import system that the module is a package. It should be set to ``None `` for
1468
+ non-package modules. It is set automatically later to a special object for
1469
+ namespace packages.
1462
1470
1463
1471
.. attribute :: loader_state
1464
1472
1465
- Container of extra module-specific data for use during loading (or
1466
- ``None ``).
1473
+ The :term: `finder ` may set this attribute to an object containing additional,
1474
+ module-specific data to use when loading the module. Otherwise it should be
1475
+ set to ``None ``.
1467
1476
1468
1477
.. attribute :: cached
1469
1478
1470
- (`` __cached__ ` `)
1479
+ (:attr: ` __cached__ `)
1471
1480
1472
- String for where the compiled module should be stored (or ``None ``).
1481
+ The filename of a compiled version of the module's code.
1482
+ The :term: `finder ` should always set this attribute but it may be ``None ``
1483
+ for modules that do not need compiled code stored.
1473
1484
1474
1485
.. attribute :: parent
1475
1486
1476
- (`` __package__ ` `)
1487
+ (:attr: ` __package__ `)
1477
1488
1478
- (Read-only) The fully-qualified name of the package under which the module
1479
- should be loaded as a submodule (or the empty string for top-level modules ).
1480
- For packages, it is the same as :attr: `__name__ `.
1489
+ (Read-only) The fully-qualified name of the package the module is in (or the
1490
+ empty string for a top-level module ).
1491
+ If the module is a package then this is the same as :attr: `name `.
1481
1492
1482
1493
.. attribute :: has_location
1483
1494
1484
- Boolean indicating whether or not the module's "origin"
1485
- attribute refers to a loadable location.
1495
+ ``True `` if the spec's :attr: `origin ` refers to a loadable location,
1496
+ ``False `` otherwise. This value impacts how :attr: `origin ` is interpreted
1497
+ and how the module's :attr: `__file__ ` is populated.
1498
+
1486
1499
1487
1500
:mod: `importlib.util ` -- Utility code for importers
1488
1501
---------------------------------------------------
@@ -1574,8 +1587,9 @@ an :term:`importer`.
1574
1587
1575
1588
:exc: `ImportError ` is raised if **name ** is a relative module name but
1576
1589
**package ** is a false value (e.g. ``None `` or the empty string).
1577
- :exc: `ImportError ` is also raised a relative name would escape its containing
1578
- package (e.g. requesting ``..bacon `` from within the ``spam `` package).
1590
+ :exc: `ImportError ` is also raised if a relative name would escape its
1591
+ containing package (e.g. requesting ``..bacon `` from within the ``spam ``
1592
+ package).
1579
1593
1580
1594
.. versionadded :: 3.3
1581
1595
0 commit comments