@@ -358,7 +358,6 @@ of what happens during the loading portion of import::
358358 sys.modules[spec.name] = module
359359 elif not hasattr(spec.loader, 'exec_module'):
360360 module = spec.loader.load_module(spec.name)
361- # Set __loader__ and __package__ if missing.
362361 else:
363362 sys.modules[spec.name] = module
364363 try:
@@ -539,6 +538,10 @@ The import machinery fills in these attributes on each module object
539538during loading, based on the module's spec, before the loader executes
540539the module.
541540
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+
542545.. attribute :: __name__
543546
544547 The ``__name__ `` attribute must be set to the fully qualified name of
@@ -552,24 +555,36 @@ the module.
552555 for introspection, but can be used for additional loader-specific
553556 functionality, for example getting data associated with a loader.
554557
558+ It is **strongly ** recommended that you rely on :attr: `__spec__ `
559+ instead instead of this attribute.
560+
555561.. attribute :: __package__
556562
557- The module's ``__package__ `` attribute must be set. Its value must
563+ The module's ``__package__ `` attribute may be set. Its value must
558564 be a string, but it can be the same value as its ``__name__ ``. When
559565 the module is a package, its ``__package__ `` value should be set to
560566 its ``__name__ ``. When the module is not a package, ``__package__ ``
561567 should be set to the empty string for top-level modules, or for
562568 submodules, to the parent package's name. See :pep: `366 ` for further
563569 details.
564570
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.
568573
569574 .. versionchanged :: 3.6
570575 The value of ``__package__ `` is expected to be the same as
571576 ``__spec__.parent ``.
572577
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+
573588.. attribute :: __spec__
574589
575590 The ``__spec__ `` attribute must be set to the module spec that was
@@ -578,7 +593,7 @@ the module.
578593 interpreter startup <programs>`. The one exception is ``__main__ ``,
579594 where ``__spec__ `` is :ref: `set to None in some cases <main_spec >`.
580595
581- When ``__package__ `` is not defined , ``__spec__.parent `` is used as
596+ When ``__spec__.parent `` is not set , ``__package__ `` is used as
582597 a fallback.
583598
584599 .. versionadded :: 3.4
@@ -623,6 +638,9 @@ the module.
623638 if a loader can load from a cached module but otherwise does not load
624639 from a file, that atypical scenario may be appropriate.
625640
641+ It is **strongly ** recommended that you rely on :attr: `__spec__ `
642+ instead instead of ``__cached__ ``.
643+
626644.. _package-path-rules :
627645
628646module.__path__
0 commit comments