⚡ Section 17 — Mid-Level Masterclass

Packaging & Dependency Management

Master modern Python project packaging. Build declarative pyproject.toml specifications (PEP 621), virtual environment isolation (venv), build backends, dev dependency groups, and Semantic Versioning.

👁️ Views: 395 Page Views
💡 PEP 621 Standard Packaging

Legacy Unpinned Global Installs vs Declarative pyproject.toml

Do not install packages into global system Python or rely on unpinned loose text files. Use virtual environments (`venv`) and declarative `pyproject.toml` dependencies.

❌ Global Install & Loose requirements.txt
# ❌ Pollutes global system Python site-packages!
pip install fastapi sqlalchemy

# requirements.txt (unpinned, breaks builds)
fastapi
sqlalchemy
✅ Isolated venv & pyproject.toml (PEP 621)
# ✅ Isolated venv & explicit SemVer bounds
python3 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"

📦 Packaging & Dependency Management Components

pyproject.toml (PEP 621)

Declarative single-file project metadata, build backend configuration, and tool settings.

Virtual Environments (venv)

Isolated Python execution environment preventing version collisions between projects.

Build Backends (Hatch / Flit)

Standardized build engines generating distribution wheels (.whl) and source archives.

Semantic Versioning (SemVer)

Strict MAJOR.MINOR.PATCH versioning guaranteeing API compatibility across releases.

💻 Production Blueprint: Standard pyproject.toml Configuration

Declarative build system separating main application runtime packages from development & testing dependencies.

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "cloud-app"
version = "1.0.0"
requires-python = ">=3.11"

dependencies = [
    "fastapi>=0.110.0,<1.0.0",
    "sqlalchemy[asyncio]>=2.0.28",
    "pydantic>=2.6.0",
]

[project.optional-dependencies]
dev = [
    "pytest>=8.0.0",
    "mypy>=1.8.0",
    "ruff>=0.3.0",
]

⭐ Rate Section 17: Packaging & Dependency Management (1 to 5 Stars)

Your rating is stored directly in the database. Average: 5.0 / 5 (1 rating)

★ ★ ★ ★ ★
← Section 16: Logging & Errors 🏠 Masterclass Hub Section 18: Caching & Workers →