ShopperCove
Menu
All writingBlogTopicsCategoriesAboutRSS
Blog
Categories
Observability & SRE62All categories
Topics
sre111localhost lab102performance70python58javascript47bundle size36benchmarking35node.js35web development21typescript19benchmark16content creation15All topics
About
Loading article…

ShopperCove

Hands-on notes on developer tools, written after the tool has actually been run.

All writingBlogCategoriesTopicsAboutPrivacyRSS

© 2026 ShopperCove

On this page

  1. Why it works
  2. Example
  3. When to use it
  4. Sources
  1. Blog

What Does if __name__ == "__main__" Do in Python?

The idiom that separates script behaviour from importable code.

Aditya Challa·12 October 2026·1 min read

Summary

Disclosure

This post contains affiliate links. If you buy through them, I may earn a commission at no extra cost to you. I only make firsthand claims about products I have actually used.

On this page
  1. Why it works
  2. Example
  3. When to use it
  4. Sources

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.

Why it works

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.

Example

def main():
    print("running")

if __name__ == "__main__":
    main()

python app.py prints "running"; import app prints nothing.

When to use it

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.

Sources

  • 01docs.python.org/3/library/__main__.html
pythonscriptsmodulesmultiprocessingimportexecution

Lab evidence

What I found running this

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

Build ready-made client websites faster

Check out Ready Client Sites

Notes when a lab post goes up

Occasional email for new hands-on reviews. No sequence and no sponsors.

Explore the product

Freelancers and devs who want to turn coding skills into paid site builds

Build ready-made client websites faster

Check out Ready Client Sites

Keep reading

  • What Does yield Do in Python? Generators Explained

    Generators, lazily: what yield actually does to a function.

    Summary12 Oct 20261 min read

  • What Is the Walrus Operator (:=) in Python? Examples

    Assignment expressions, explained with the examples that actually help.

    Summary12 Oct 20261 min read

  • What Is the Difference Between is and == in Python?

    Equality versus identity, and why `is None` is the one place `is` is standard.

    Summary12 Oct 20261 min read