Definition
A package is a folder of modules that can be imported together. A file named __init__.py marks it as a regular package, and can set up what the package exposes. Modules inside can import each other using relative imports.
In simple words
If modules are chapters, a package is the book. import shop.billing means: open the shop folder, then the billing file.
Why it matters
Beyond a few files, you need folders. NumPy, scikit-learn and PyTorch are all packages, and knowing the layout helps you read their code and errors.
How it works
shop/
__init__.py # marks the package
billing.py # def total(items): ...
inventory/
__init__.py
stock.py
import shop.billing
from shop.billing import total
from shop.inventory.stock import count
# inside shop/inventory/stock.py
from ..billing import total # relative: up one folder, then billingCommon mistakes
- Running a file inside a package directly when it uses relative imports. Use python -m.
- Putting heavy work in __init__.py, which slows every import.
Check yourself
Summary
- A package is a folder of modules.
- __init__.py marks it and sets up its contents.
- Run packaged files with python -m.
Finished reading?
Sources: The Python Tutorial, python.org