What is the difference between a module and a package in Python?
A module is a single .py file. A package is a directory of modules, normally with an __init__.py file, that you import using dotted names like shop.billing. Packages let you organise a large project into a hierarchy.
Explain it simply
A module is one notebook. A package is a bag that holds several related notebooks, say one bag for Maths with notebooks on algebra and geometry. The bag has a label, __init__.py, which runs when the bag is first opened and tells Python "this folder is importable".
Example
shop/
__init__.py
billing.py
inventory.py# using the package
import shop.billing
from shop import inventory
from shop.billing import total_price
print(total_price([100, 250])) # 350Python also supports namespace packages, folders without __init__.py (Python 3.3+). They work for imports, but a regular package with __init__.py is still the clear and common choice, and it gives you a place to put setup code or to expose a neat public API.
What to say in the interview
"A module is a single Python file; a package is a directory of modules, usually with an __init__.py that marks it as a package and can run initialisation or re-export names. Both are imported with import statements, and packages are how larger code bases are structured."
Common mistakes
- Naming your own file
random.pyorjson.py, which shadows the standard library module.