What __init__.py does in Python packages
June 15, 2026
TIL: __init__.py is used to make a folder behave like a package and also helps expose cleaner imports.
Example
Folder structure
project/
└── mypackage/
├── __init__.py
└── math_utils.pymath_utils.py
def add(a, b):
return a + bWithout __init__.py re-export
Usage:
from mypackage.math_utils import add
print(add(2, 3))Output:
5Import path is longer because we directly access the module file.
With __init__.py re-export
__init__.py
from .math_utils import addThe . means: current package.
Usage:
from mypackage import add
print(add(2, 3))Output:
5Now the import is cleaner because __init__.py re-exported add.
Another example
__init__.py
print("Initializing package...")Usage:
import mypackageOutput:
Initializing package...This happens because Python executes __init__.py when importing the package.
Mental model
__init__.py = package entrypoint/export file.