`__name__ == \"__main__\"`
__name__ is a module's dotted import name. When you run a file as a program, __name__ is "__main__". The if __name__ == "__main__": guard runs a block only in that case, not when the file is imported.
Detailed Explanation
Every module has a __name__. After import shop, shop.__name__ is "shop". After import payments.stripe, the name is "payments.stripe". The interpreter needs that string for sys.modules, tracebacks, and relative imports.
Running a file changes the rule. python shop.py still executes shop.py, but it does not import it under the name shop. It loads the file as the main program and sets __name__ to "__main__". That is how one file can be both a library and a command-line tool.
The guard uses that difference. Code above the guard runs on import and on direct execution: function and class definitions, constants, imports. Code inside the guard runs only when the file is the program: argument parsing, starting a server, running a demo. Importing the module for tests or for reuse then does not launch the program.
Without the guard, a module that prints a report at top level will print that report every time a test imports it. A module that starts a network listener at top level will start one during import. The guard is how you keep import quiet.
python -m shop is the package-friendly way to do the same thing. The file still has __name__ == "__main__", but __package__ is set so relative imports work. python shop.py leaves __package__ empty. Prefer -m once the project is a package.
A common pattern is a main() function that takes arguments and returns an exit code, with the guard calling sys.exit(main()). Tests can then call main(["--dry-run"]) without spawning a process. Click, argparse, and Typer all sit behind that function.
__main__.py inside a package is the module python -m packagename runs. It is the package equivalent of the guard: a dedicated entry file so the rest of the package stays import-safe.
Code Examples
# shop.py
TAX = 0.2
def with_tax(cents: int) -> int:
return int(cents * (1 + TAX))
def main(argv: list[str] | None = None) -> int:
cents = int((argv or ["1000"])[0])
print(with_tax(cents))
return 0
if __name__ == "__main__":
raise SystemExit(main())# Importing shop does not print. Running it does.
import shop
print(shop.__name__) # shop
print(shop.with_tax(1000)) # 1200
print(shop.main(["2500"])) # prints 3000, returns 0import sys
print(__name__) # __main__ in a script, the module name on import
print(sys.argv[0]) # the file or `-m` name that started the processCommon Gotchas
Work at module top level runs on import
a `print`, a thread, or a network connect above the guard will fire in tests and in other modules. Keep top level to definitions.
`python file.py` breaks relative imports
the file is `__main__`, not `package.module`. Use `python -m package.module` or absolute imports.
Two copies of the same file
`import shop` and running `shop.py` as `__main__` can load the file twice under two names. Singletons and registries then exist twice. Run with `-m` and import the package name.
`__name__` is a string, not a boolean
the comparison is `== "__main__"`. There is no `__main__` builtin you should test with `is`.