← Back to Python, page 1

python-0015 · Easy fresher

How do modules and packages work in Python, and what does if __name__ == '__main__' do?

A module is a .py file; a package is a directory of modules, normally with an __init__.py. import runs a module once, caches it in sys.modules and binds a name to it. When a file is run directly, its __name__ is '__main__', so code under if __name__ == '__main__' runs only as a script, not on import.

Full answer
python
import os, sys, tempfile, importlib, runpy
root = tempfile.mkdtemp()
os.makedirs(os.path.join(root, "shop"))
open(os.path.join(root, "shop", "__init__.py"), "w").close()
with open(os.path.join(root, "shop", "pricing.py"), "w") as f:
    f.write("print('loading', __name__)\n"
            "def add_tax(x): return round(x * 1.18, 2)\n"
            "if __name__ == '__main__':\n    print('run as script:', add_tax(100))\n")
sys.path.insert(0, root)
import shop.pricing
import shop.pricing
print(shop.pricing.add_tax(50), "shop.pricing" in sys.modules)
runpy.run_path(os.path.join(root, "shop", "pricing.py"), run_name="__main__")

Output:

text
loading shop.pricing
59.0 True
loading __main__
run as script: 118.0

The run creates a small package in a temporary directory: shop/__init__.py and shop/pricing.py. Importing shop.pricing runs the module's top-level code once, printing its __name__, which is the dotted module name. A second import does not print again because the module is cached in sys.modules. Running the same file as a script with runpy sets __name__ to '__main__', so the guarded block runs.

Points interviewers look for:

  • from shop.pricing import add_tax binds only that name; import shop.pricing binds the package and attribute path.
  • Relative imports (from .pricing import add_tax) work inside packages.
  • Python finds modules via sys.path, which starts with the script's directory.
  • Circular imports fail when two modules need each other's names at import time; moving imports or restructuring fixes them.
python
import os, sys, tempfile, importlib, runpy
root = tempfile.mkdtemp()
os.makedirs(os.path.join(root, "shop"))
open(os.path.join(root, "shop", "__init__.py"), "w").close()
with open(os.path.join(root, "shop", "pricing.py"), "w") as f:
    f.write("print('loading', __name__)\n"
            "def add_tax(x): return round(x * 1.18, 2)\n"
            "if __name__ == '__main__':\n    print('run as script:', add_tax(100))\n")
sys.path.insert(0, root)
import shop.pricing
import shop.pricing
print(shop.pricing.add_tax(50), "shop.pricing" in sys.modules)
runpy.run_path(os.path.join(root, "shop", "pricing.py"), run_name="__main__")

Run on Python 3.13.5, twice in fresh interpreters, with identical output.

Key points

  • Modules are files, packages are directories
  • Imports run once and are cached in sys.modules
  • __name__ is '__main__' for scripts
  • Relative imports work inside packages
  • sys.path decides where modules are found

Common mistakes

  • Putting side effects at module top level
  • Naming a file the same as a standard module, such as random.py

Follow-ups

  • What causes circular import errors?
  • What is a namespace package?

Tips

  • Explain the guard with both cases