Stop 06 · Files, Errors & ModulesTopics · 8/9

Packages & the __main__ Guard

Part of the Python Roadmap

In short

As projects grow, even modules need organising into folders. Packages do that, and the __main__ guard lets a file be both a runnable script and an importable module without surprises.

A package is a folder of modules, usually with an __init__.py file, that you import with dotted names. The check if __name__ == '__main__': lets a file run as a script without that code running when it is imported.

The jaadu sits at the reception desk of a small multi-storey building, pointing visitors to different floors.

Think of it like this

A building with a reception desk

A package is an office building, each module is a floor, and __init__.py is the reception desk that tells visitors what's available. The __main__ guard is like a light that only switches on when you enter through the front door yourself, not when someone just borrows a chair from one of the floors.

Watch it happen

Same file, two ways of using it

python report.py

  • __name__ is '__main__'
  • Code under the guard runs
  • Acts like a script
  • Prints the demo report

import report

  • __name__ is 'report'
  • Code under the guard is skipped
  • Acts like a library
  • Only functions are loaded

Put example runs and CLI code under if __name__ == '__main__': so importing the file never triggers them by accident.

The big ideas

What you need to know

Folder = package

A folder of .py files, usually with __init__.py, is a package. Import with dots: from shop.billing import invoice.

__init__.py

It runs when the package is imported. Keep it small, often just re-exporting the most useful names.

The guard

Every module has __name__. It's '__main__' only for the file you ran directly.

Run with -m

python -m shop.cli runs a module inside a package with imports working correctly from the project root.

See it in code

Walk through the code

python
1# Project layout2# shop/3#   __init__.py4#   billing.py5#   cli.py6 7# shop/billing.py8def invoice_total(items: dict[str, float]) -> float:9    return sum(items.values())10 11if __name__ == "__main__":12    demo = {"pen": 20, "notebook": 60}13    print("Demo total:", invoice_total(demo))14 15# shop/__init__.py16from .billing import invoice_total17 18# shop/cli.py19from shop import invoice_total20 21def main() -> None:22    print(invoice_total({"bag": 799, "bottle": 249}))23 24if __name__ == "__main__":25    main()26 27# Run from the project root:28#   python -m shop.cli

1/6A folder called shop with __init__.py is a package containing two modules.

A tiny shop package: billing logic, a tidy __init__.py and a CLI entry point protected by the guard.

Save yourself the pain

Common mistakes

Oops

Putting print statements and test calls at the top level of a module.

Do this instead

Move them under if __name__ == '__main__': so they don't run on import.

Oops

Running python shop/cli.py and getting ModuleNotFoundError: No module named 'shop'.

Do this instead

Run python -m shop.cli from the project root so Python can find the package.

Your mission

Package your utilities

~30 min

Quick check

When main.py runs import helpers, what is __name__ inside helpers.py?

Go deeper

  1. 1Group two related modules into a package folder and import from it.
  2. 2Add a main() function and the __main__ guard to every script you write.
  3. 3Import your script from another file and confirm the guarded code doesn't run.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge