@@ -358,7 +358,6 @@ of what happens during the loading portion of import::
358
358
sys.modules[spec.name] = module
359
359
elif not hasattr(spec.loader, 'exec_module'):
360
360
module = spec.loader.load_module(spec.name)
361
- # Set __loader__ and __package__ if missing.
362
361
else:
363
362
sys.modules[spec.name] = module
364
363
try:
@@ -539,6 +538,10 @@ The import machinery fills in these attributes on each module object
539
538
during loading, based on the module's spec, before the loader executes
540
539
the module.
541
540
541
+ It is **strongly ** recommended that you rely on :attr: `__spec__ ` and
542
+ its attributes instead of any of the other individual attributes
543
+ listed below.
544
+
542
545
.. attribute :: __name__
543
546
544
547
The ``__name__ `` attribute must be set to the fully qualified name of
@@ -552,24 +555,36 @@ the module.
552
555
for introspection, but can be used for additional loader-specific
553
556
functionality, for example getting data associated with a loader.
554
557
558
+ It is **strongly ** recommended that you rely on :attr: `__spec__ `
559
+ instead instead of this attribute.
560
+
555
561
.. attribute :: __package__
556
562
557
- The module's ``__package__ `` attribute must be set. Its value must
563
+ The module's ``__package__ `` attribute may be set. Its value must
558
564
be a string, but it can be the same value as its ``__name__ ``. When
559
565
the module is a package, its ``__package__ `` value should be set to
560
566
its ``__name__ ``. When the module is not a package, ``__package__ ``
561
567
should be set to the empty string for top-level modules, or for
562
568
submodules, to the parent package's name. See :pep: `366 ` for further
563
569
details.
564
570
565
- This attribute is used instead of ``__name__ `` to calculate explicit
566
- relative imports for main modules, as defined in :pep: `366 `. It is
567
- expected to have the same value as ``__spec__.parent ``.
571
+ It is **strongly ** recommended that you rely on :attr: `__spec__ `
572
+ instead instead of this attribute.
568
573
569
574
.. versionchanged :: 3.6
570
575
The value of ``__package__ `` is expected to be the same as
571
576
``__spec__.parent ``.
572
577
578
+ .. versionchanged :: 3.10
579
+ :exc: `ImportWarning ` is raised if import falls back to
580
+ ``__package__ `` instead of
581
+ :attr: `~importlib.machinery.ModuleSpec.parent `.
582
+
583
+ .. versionchanged :: 3.12
584
+ Raise :exc: `DeprecationWarning ` instead of :exc: `ImportWarning `
585
+ when falling back to ``__package__ ``.
586
+
587
+
573
588
.. attribute :: __spec__
574
589
575
590
The ``__spec__ `` attribute must be set to the module spec that was
@@ -578,7 +593,7 @@ the module.
578
593
interpreter startup <programs>`. The one exception is ``__main__ ``,
579
594
where ``__spec__ `` is :ref: `set to None in some cases <main_spec >`.
580
595
581
- When ``__package__ `` is not defined , ``__spec__.parent `` is used as
596
+ When ``__spec__.parent `` is not set , ``__package__ `` is used as
582
597
a fallback.
583
598
584
599
.. versionadded :: 3.4
@@ -623,6 +638,9 @@ the module.
623
638
if a loader can load from a cached module but otherwise does not load
624
639
from a file, that atypical scenario may be appropriate.
625
640
641
+ It is **strongly ** recommended that you rely on :attr: `__spec__ `
642
+ instead instead of ``__cached__ ``.
643
+
626
644
.. _package-path-rules :
627
645
628
646
module.__path__
0 commit comments