from .foo import Foo from .bar import Bar >>> import spam >>> spam.foo <module 'spam.foo' from '/tmp/imports/spam/foo.py'> >>> spam.bar <module 'spam.bar' from '/tmp/imports/spam/bar.py'> The import machinery fills in these attributes on each module object during loading, based on the module’s spec, before the loader executes the module. New in version 3.4. The module’s spec is exposed as the$_{__spec__}$ attribute on a module object. See$_{ModuleSpec}$ for details on the contents of the module spec. Using a spec during import allows state to be transferred between import system components, e.g. between the finder that creates the module spec and the loader that executes it. Most importantly, it allows the import machinery to perform the boilerplate operations of loading, whereas without a module spec the loader had that responsibility. The import machinery uses a variety of information about each module during import, especially before loading. Most of the information is common to all modules. The purpose of a module’s spec is to encapsulate this import-related information on a per-module basis. 5.4.3 Module spec 5.4.4 Import-related module attributes then executing the following puts a name binding to$_{foo}$ and$_{bar}$ in the$_{spam}$module: Given Python’s familiar name binding rules this might seem surprising, but it’s actually a fundamental feature of the import system. The invariant holding is that if you have$_{sys.modules['spam']}$ and$_{sys.modules['spam.}$ $_{foo']}$(as you would after the above import), the latter must appear as the$_{foo}$ attribute of the former. The Python Language Reference, Release 3.9.5 55 5.4. Loading The$_{__name__}$ attribute must be set to the fully-qualified name of the module. This name is used to uniquely identify the module in the import system. The$_{__loader__}$ attribute must be set to the loader object that the import machinery used when loading the module. This is mostly for introspection, but can be used for additional loader-specific functionality, for example getting data associated with a loader. The module’s$_{__package__}$ attribute must be set. Its value must be a string, but it can be the same value as its$_{__name__}$ . When the module is a package, its$_{__package__}$ value should be set to its$_{__name__}$ . When the module is not a package,$_{__package__}$ should be set to the empty string for top-level modules, or for submodules, to the parent package’s name. See PEP 366 for further details. This attribute is used instead of$_{__name__}$ to calculate explicit relative imports for main modules, as defined in PEP 366 . It is expected to have the same value as$_{__spec__.parent}$ . Changed in version 3.6: The value of$_{__package__}$ is expected to be the same as$_{__spec__.parent}$ . The$_{__spec__}$ attribute must be set to the module spec that was used when importing the module. Setting __spec__ appropriately applies equally to modules initialized during interpreter startup . The one exception is$_{__main__}$ , where$_{__spec__}$ is set to None in some cases . When$_{__package__}$ is not defined,$_{__spec__.parent}$ is used as a fallback. __spec__ __package__ __loader__ __name__