Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 07 / Topic 4 of 9

Packages and __init__.py

A package is a folder of modules, like a drawer that holds several toolboxes. It has an __init__.py file (it can be empty). That file runs once, the first time the package is imported. Put package-wide settings there.

Real-world example: a billing package with a tax module.

examples/
├── 03_use_package.py
└── billing/
    ├── __init__.py     ← __version__ = "1.0", SHOP_NAME = "Sharma Electronics"
    └── tax.py          ← GST_SLABS, gst_amount(), price_with_gst()
import billing.tax                                   # package.module
from billing.tax import GST_SLABS, price_with_gst    # names from inside a module
from billing import SHOP_NAME                        # a name from __init__.py

print(billing.tax.gst_amount(1000, "standard"))   # 120.0
print(price_with_gst(799))                        # 942.82
print(GST_SLABS["luxury"])                        # 0.28
print(SHOP_NAME, billing.__version__)             # Sharma Electronics 1.0

(This works in a script saved next to the billing/ folder, see examples/03_use_package.py.)

🔍 Going deeper: bigger packages often re-export names in __init__.py with a relative import, e.g. from .tax import price_with_gst (the dot means "this package"), so users can simply write from billing import price_with_gst. A folder without __init__.py can still be imported (a "namespace package"), but always add one to your own packages to avoid surprises.

Done here? The practice set for Part 7 — 7 exercises with solutions — is on the part page.

Search the guide