Packages and Modern Project Structure
Organize complex Python codebases into modular packages with __init__.py, relative vs absolute imports, and the modern src-layout standard.
If a Python module is like a single document or sheet of notes, a Package is a labeled filing folder containing multiple related modules.
As applications grow from simple scripts into real systemsโlike web APIs, games, or data pipelinesโyou will eventually write dozens of modules:
- User management (
auth.py,profile.py) - Billing & Payments (
stripe.py,paypal.py,invoices.py) - Database connectors (
postgres.py,redis.py)
Dumping 60 different .py files into a single root folder produces chaotic clutter. A Package lets you group related modules into clean, hierarchical folders.
1. What Makes a Folder a Package? The __init__.py File
In Python, a directory is recognized as an importable package when it contains a special file named __init__.py:
ecommerce/ # Package folder
โโโ __init__.py # Package initializer
โโโ cart.py # Module inside package
โโโ checkout.py # Module inside package
โโโ inventory.py # Module inside package
The Two Roles of __init__.py:
- Package Marker: It tells Pythonโs import engine that this folder should be treated as a Python package.
- Package Initializer: When someone writes
import ecommerce, Python executes__init__.pyautomatically!
2. Exposing a Clean Public API
Without thoughtful design, consumers of your package would be forced to navigate deep internal file paths:
# Clumsy, verbose imports:
from ecommerce.billing.processors.stripe_gateway import StripePayment
Inside ecommerce/__init__.py, you can import your key classes and expose them directly at the root of the package:
# ecommerce/__init__.py
"""E-Commerce Core Package."""
from .cart import ShoppingCart
from .checkout import process_payment
# Control what exports when someone types `from ecommerce import *`
__all__ = ["ShoppingCart", "process_payment"]
Now, anyone using your package enjoys a beautiful, effortless import experience:
# Clean and professional!
from ecommerce import ShoppingCart, process_payment
3. Absolute vs Relative Imports
When modules inside the same package need to call one another, you can choose between Absolute and Relative imports:
Absolute Imports (PEP 8 Recommended)
Specify the complete path starting from the projectโs root package name:
# Inside ecommerce/checkout.py:
from ecommerce.inventory import check_stock
from ecommerce.cart import ShoppingCart
Why itโs preferred: Clear, unambiguous, and easy to search across your codebase.
Relative Imports (Dot Notation)
Reference modules relative to the current fileโs location using leading dots:
- Single Dot (
.): Current package folder. - Double Dot (
..): Parent directory.
# Inside ecommerce/checkout.py:
from .inventory import check_stock # Same folder!
from .cart import ShoppingCart # Same folder!
4. Modern Professional Project Layout: The src/ Layout
When organizing a professional Python repository, where should your files live?
The modern industry standard (recommended by Python packaging authorities and tools like Poetry, Hatch, and Flit) is the src/ layout:
my_awesome_project/
โโโ pyproject.toml # Project metadata, dependencies, build config
โโโ README.md # Human-readable documentation
โโโ .gitignore # Git exclusions (.venv, __pycache__)
โ
โโโ src/ # APPLICATION SOURCE CODE
โ โโโ my_package/ # The actual importable package
โ โโโ __init__.py # Package root
โ โโโ core.py # Main business logic
โ โโโ utils.py # Helper utilities
โ โโโ subpackage/ # Nested sub-package
โ โโโ __init__.py
โ โโโ helpers.py
โ
โโโ tests/ # AUTOMATED TEST SUITE
โโโ __init__.py
โโโ test_core.py # Unit tests for core.py
โโโ test_utils.py # Unit tests for utils.py
Why Use the src/ Layout instead of a Flat Layout?
In older โflatโ layouts, the my_package/ folder sat directly in the root folder next to tests.
This caused a notorious testing bug: when you ran tests, Python would accidentally import your unbuilt, local source files directly from the current working directory, hiding packaging bugs and installation errors!
The src/ layout guarantees that tests run against the truly installed package, preventing deployment surprises!
Summary & Whatโs Next
- A package is a directory containing modules and an
__init__.pyfile. - Use
__init__.pyto bundle and expose a clean public API for package consumers. - Use
__all__to explicitly define allowed exports. - Prefer absolute imports for clarity; use relative dots (
.) for intra-package references. - Structure projects using the modern
src/layout standard.
Now that you know how to build packages, how do you install third-party libraries (like FastAPI, Requests, or Pytest) without polluting your computerโs system Python? Learn the art of isolation in virtual-environments.