Language and Compiler Modules

__main__

Names the top-level execution environment. Importing __main__ refers to the current entry module rather than rerunning the program. A main guard keeps script-only behavior from running during an ordinary import. Packages can supply __main__.py for python -m execution.

Formal Syntax & Structure

python syntaxpython
import __main__

Return Value

module

Existing top-level execution module, not a separate library loaded from disk.

Working Example

Example Snippetpython
import __main__print(__main__.__name__)
Python Reference