Featureadvanced
The __main__ module
Also called __main__, __main__.py, python -m package, __main__.__spec__, import __main__, top-level code environment, pickle __main__.
The program runs in a module named __main__. python -m pkg runs pkg/__main__.py, __main__.__spec__ depends on how Python started, and objects defined in __main__ pickle under that name.
- Language
- Python
Synopsis
bandclass/
__init__.py
__main__.py # runs for: python -m bandclass
student.py
python file.py # __main__.__spec__ is None
python -m pkg.mod # __main__.__spec__.name == "pkg.mod"
python -m pkg # runs pkg.__main__; __spec__.name == "pkg.__main__"
python some_dir # runs some_dir/__main__.py; __spec__.name == "__main__"
import __main__ # the module of the program, not a file named __main__.py
Semantics
r1. "main is the name of the environment where top-level code is run. “Top-level code” is the first user-specified Python module that starts running."1 "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."2
r2. A package can be a program. With -m, "When a package name is supplied instead of a normal module, the interpreter will execute .main as the main module."3 A directory or a zip file works the same way as the script argument: the path can refer to "a directory containing a main.py file, or a zipfile".3 Example: package-main.
r3. The library page advises a short __main__.py: "The content of main.py typically isn’t fenced with an if name == 'main' block. Instead, those files are kept short and import functions to execute from other modules."1 Example: package-main.
r4. "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."2 For a file run by its path, "main.spec is always None in the last case, even if the file could technically be imported directly as a module instead."2 For a package, runpy sets the name to mod_name + '.__main__'.4 Example: spec-by-start.
r5. Any module can import the program: "other modules running within that same program can import the top-level environment’s scope (namespace) by importing the main module. This doesn’t import a main.py file but rather whichever module that received the special name 'main'."1 Example: import-main.
r6. "Python inserts an empty main module in sys.modules at interpreter startup, and populates it by running top-level code."1 So a module that imports __main__ while the program runs its top-level code gets a module that is only partly filled. Example: import-main.
r7. The main module and an import of the same file are two modules: "even when main corresponds with an importable module and main.spec is set accordingly, they’re still considered distinct modules."2
Examples
python -m greet runs greet/__main__.py (rules r2 and r3). Its __name__ is '__main__' and its spec name is greet.__main__, so the relative import works.
"""The greet package."""
def hello(name):
return f"hello, {name}"
import sys
from .core import hello
print(hello(sys.argv[1]))
print(__name__, __spec__.name)
import subprocess
import sys
child = subprocess.run([sys.executable, "-m", "greet", "Ada"], capture_output=True, text=True)
print(child.stdout, end="")
hello, Ada
__main__ greet.__main__
The same code started in four ways (rule r4). Only a file run by its path has no spec.
print(__name__, __spec__.name if __spec__ else None)
print(__name__, __spec__.name if __spec__ else None)
"""A package with a __main__ module."""
print(__name__, __spec__.name if __spec__ else None)
import subprocess
import sys
for args in (["tool.py"], ["-m", "tool"], ["-m", "pkg"], ["app"]):
child = subprocess.run([sys.executable, *args], capture_output=True, text=True)
print(" ".join(args), "->", child.stdout.strip())
tool.py -> __main__ None
-m tool -> __main__ tool
-m pkg -> __main__ pkg.__main__
app -> __main__ __main__
A helper module reads a setting from the program with import __main__ (rule r5). The import gives the module that is running, as far as its top-level code has gone (rule r6).
import __main__
def user_name():
return getattr(__main__, "my_name", "not set")
print("at import:", user_name())
import sys
import namely
my_name = "Dinsdale"
print("later:", namely.user_name())
print(sys.modules["__main__"] is sys.modules[__name__])
at import: not set
later: Dinsdale
True
The examples give the same output on CPython 3.12.3, 3.13.14, 3.14.6 and 3.15.0b2.
Pitfalls
A class defined in __main__ and pickle
pickle stores a class by name: "classes are pickled by fully qualified name, so the same restrictions in the unpickling environment apply."5 "Thus the defining module must be importable in the unpickling environment, and the module must contain the named object".5 A class of the program has the module name __main__, so another program cannot load the data.
import pickle
with open("point.pickle", "rb") as f:
print(pickle.load(f))
This code runs but gives a wrong answer on purpose.
import pickle
import subprocess
import sys
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
data = pickle.dumps(Point(1, 2))
print(b"__main__" in data)
with open("point.pickle", "wb") as f:
f.write(data)
child = subprocess.run([sys.executable, "load.py"], capture_output=True, text=True)
print(child.stderr.splitlines()[-1].split(":")[0])
True
AttributeError
Define the class in a module that both programs import. Then pickle stores the name of that module.
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
import pickle
with open("point.pickle", "rb") as f:
p = pickle.load(f)
print(type(p).__module__, p.x, p.y)
import pickle
import subprocess
import sys
from shapes import Point
with open("point.pickle", "wb") as f:
pickle.dump(Point(1, 2), f)
child = subprocess.run([sys.executable, "load.py"], capture_output=True, text=True)
print(child.stdout.strip())
shapes 1 2
An if __name__ guard in a zip file
A __main__.py in a package has the name pkg.__main__ when it is imported, so a guard there works. The library page warns: "This won’t work for main.py files in the root directory of a .zip file though. Hence, for consistency, a minimal main.py without a name check is preferred."1
Two copies of the program module
Rule r7 means that import main inside the program makes a second module from main.py, with its own globals. Keep the state in a module that is not the program. The page Executing modules as scripts shows this case.
Comparison
Go has one fixed form for the program: "A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively."6 In Python, any module, package, directory or zip file can be the main module, and its name is __main__ in each case (rule r1).
Implementation notes
python -m uses runpy. runpy.run_module sets __spec__ "appropriately for the actually imported module (that is, spec.name will always be mod_name or mod_name + '.main', never run_name)."4 The library page names two users of the __main__ namespace: "The main scope is used in the implementation of pdb and rlcompleter."1
References
- __main__ — Top-level code environment, What is the “top-level code environment”?; __main__.py in Python Packages; Idiomatic Usage; import __main__
- The Python Language Reference, 5. The import system, 5.8. Special considerations for __main__; 5.8.1. __main__.__spec__
- 1. Command line and environment, Python Setup and Usage, 1.1.1. Interface options, -m and <script>
- runpy — Locating and executing Python modules, runpy.run_module
- pickle — Python object serialization, What can be pickled and unpickled?
- The Go Programming Language Specification, Program execution
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag R45JM7 Edit this page History Report an error Markdown