1. Package
A package is a directory (folder) that contains an __init__.py file.
The __init__.py tells Python:
“This folder is a Python package, so you can import from it.”
Example:
myapp/
├── __init__.py
├── helpers.py
myapp is now a package.
You can import like this:
from myapp import helpers
Key idea:
A package lets you group related modules into one namespace.
2. Module
A module is just a single Python file (.py) that contains Python code.
You can import it and use its variables, functions, classes, etc.
Example:
# utils.py (This is a Python module)
def greet(name):
return f"Hello, {name}"
You can import this in another file:
import utils
print(utils.greet("Alice"))
Key idea:
Any .py file is a module.
3. Subpackages and Submodules
- Packages inside packages ⇒ Subpackages
- Modules inside subpackages ⇒ Submodules
Example structure:
myapp/
├── __init__.py
├── api/ → is a subpackage
│ ├── __init__.py
│ ├── users.py → is a submodule
│ └── items.py
├── core.py
Key idea:
Subpackages allow you to organize large codebases into smaller logical units.
Why so many __init__.py files?
The __init__.py file marks a directory as a package or subpackage.
Without it, Python won’t treat the folder as importable.
Top comments (0)