The Python Language Reference, Release 3.9.5 (continued from previous page) Chapter 5. The import system 60 In either subpackage1/moduleX.py or subpackage1/__init__.py , the following are valid relative imports: moduleZ.py moduleA.py from .moduleY import spam from .moduleY import spam as ham from .import moduleY from ..subpackage1import moduleY from ..subpackage2 .moduleZ import eggs from ..moduleA import foo Absolute imports may use either the import <> or from <> import <> syntax, but relative imports may only use the second form; the reason for this is that: import XXX .YYY .ZZZ should expose$_{XXX.YYY.ZZZ}$ as a usable expression, but .moduleY is not a valid expression. The$_{__main__}$ module is a special case relative to Python’s import system. As noted elsewhere , the$_{__main__}$ module is directly initialized at interpreter startup, much like$_{sys}$ and$_{builtins}$. However, unlike those two, it doesn’t strictly qualify as a built-in module. This is because the manner in which$_{__main__}$ is initialized depends on the flags and other options with which the interpreter is invoked. Depending on how$_{__main__}$ is initialized,$_{__main__.__spec__}$ gets set appropriately or to$_{None}$ . When Python is started with the$_{-m}$option,$_{__spec__}$ is set to the module spec of the corresponding module or package. __spec__ is also populated when the$_{__main__}$ module is loaded as part of executing a directory, zipfile or other$_{sys.path}$entry. In the remaining cases$_{__main__.__spec__}$ is set to$_{None}$ , as the code used to populate the$_{__main__}$ does not correspond directly with an importable module: Note that$_{__main__.__spec__}$ is always$_{None}$ in the last case, even if the file could technically be imported directly as a module instead. Use the$_{-m}$switch if valid module metadata is desired in$_{__main__}$ . Note also that even when$_{__main__}$ corresponds with an importable module and$_{__main__.__spec__}$ is set accordingly, they’re still considered distinct modules. This is due to the fact that blocks guarded by$_{if __name__}$ == "__main__": checks only execute when the module is used to populate the$_{__main__}$ namespace, and not during normal import. • running directly from a source or bytecode file • running from stdin • -c option • interactive prompt 5.8.1 __main__.__spec__ 5.8 Special considerations for __main__