The idiom that separates script behaviour from importable code.
Aditya Challa1 min read
if __name__ == "__main__": runs the indented code only when the file is executed directly, for example with python app.py. When the same file is imported as a module, that block is skipped.
Python sets the variable __name__ for every module. When a file is run as the top-level script, __name__ is "__main__". When it is imported, __name__ is the module's name.
python app.py prints "running"; import app prints nothing.
Use it for scripts that also expose reusable functions, so tests and other modules can import them without side effects. It is also needed with multiprocessing on platforms that spawn new processes.
Lab evidence
Facts in this draft were checked on 12 Oct 2026 against the primary sources listed in the body (official docs, release schedules or Google/YouTube help pages). No benchmarks or timings were run, so the post makes no performance or measurement claims; dates are quoted as published by the maintainers, and any estimate is labelled as such.
Explore the product
Freelancers and devs who want to turn coding skills into paid site builds
Generators, lazily: what yield actually does to a function.
Summary12 Oct 20261 min read
Assignment expressions, explained with the examples that actually help.
Summary12 Oct 20261 min read
Equality versus identity, and why `is None` is the one place `is` is standard.
Summary12 Oct 20261 min read