Modules and packages
A module is a single .py file loaded as a namespace. A package is a directory of modules, typically with an __init__.py, that lets you group related files under one dotted name.
Detailed Explanation
Python does not run every file in a folder just because they sit together. A file becomes a module when it is imported (or run as a script). Import executes the file once, stores the resulting namespace in sys.modules under its dotted name, and binds that module object to a name in the importer.
The module's namespace is its global scope. Top-level assignments, defs, and classes become attributes: shop.tax after import shop. Later imports of the same name return the cached object. Reloading is possible with importlib.reload and is almost never what production code wants, because other modules may still hold the old objects.
A package is how you scale past one file. A directory payments/ with __init__.py is the payments package. payments/stripe.py is the submodule payments.stripe. Importing payments runs __init__.py and does not automatically import submodules unless __init__.py imports them. That is why import payments followed by payments.stripe can raise AttributeError until you import payments.stripe.
Namespace packages, introduced by PEP 420, omit __init__.py and can span several directories on sys.path. Regular packages with __init__.py are the default for application code because they make the package boundary obvious and let you put package-level setup in one place.
The name of a module depends on where sys.path starts looking, not on the file's absolute path. The first matching shop.py wins. Running a file as a script puts "" (the current directory) at the front of sys.path, which is why python shop.py and python -m shop can import different things. Prefer python -m for packages so relative imports work.
Relative imports (from . import tax, from ..cart import items) are resolved against the current package. They only work when the module was imported as part of a package, not when it was run as __main__ from its own file. Absolute imports (from payments import stripe) are clearer in application code.
Circular imports happen when A imports B while B is still importing A. The usual fixes are to move the shared names to a third module, or to import inside the function that needs them so the cycle is finished first.
Code Examples
# shop.py — a module is a file. These names become attributes.
TAX = 0.2
def with_tax(cents: int) -> int:
return int(cents * (1 + TAX))# After `import shop`, the module object is cached.
import sys
import shop
print(shop.with_tax(1000)) # 1200
print(sys.modules["shop"] is shop) # True
print(shop.__name__, shop.__file__)# payments/__init__.py can re-export submodules so `import payments` is enough.
# from . import stripe
#
# Usage:
# import payments.stripe # loads the submodule
# from payments.stripe import chargeCommon Gotchas
Importing the package does not load submodules
`import payments` then `payments.stripe` fails unless `__init__.py` imported it. Import the submodule explicitly.
Running a file as a script breaks relative imports
`python payments/stripe.py` sets `__name__` to `__main__` and `__package__` to `None`. Use `python -m payments.stripe`.
The first `sys.path` hit wins
a local `json.py` shadows the standard library. Do not name files after modules you need to import.
A module runs at import time
expensive work or side effects at top level happen on first import, including in tests. Keep module level to definitions.