36. Packaging & Distribution
Master modern Python packaging & library distribution: understand pyproject.toml (PEP 621), package directory layouts (src-layout), Semantic Versioning (SemVer), building Wheel (.whl) artifacts, local editable installs (pip install -e .), and publishing to PyPI with twine.
🎓 Junior Python Curriculum — Select Module
pyproject.toml & PEP 621
The modern declarative configuration file replacing legacy `setup.py`. Defines build system requirements, project metadata, authors, dependencies, and entry-point CLI scripts.
- `[build-system]`: Specifies build backend (`setuptools`, `hatchling`).
- `[project]`: Standard metadata fields (version, dependencies).
- `[project.scripts]`: Register CLI terminal commands.
Semantic Versioning (SemVer)
Standardized 3-part versioning format (`MAJOR.MINOR.PATCH`) to clearly signal breaking changes, backwards-compatible additions, and patch bug fixes.
- MAJOR (`1.0.0` → `2.0.0`): Incompatible API breaking changes.
- MINOR (`1.0.0` → `1.1.0`): Backwards-compatible new features.
- PATCH (`1.0.0` → `1.0.1`): Backwards-compatible bug fixes.
Wheels & PyPI Upload
Build distribution packages into pre-compiled binary Wheel (`.whl`) files and source archives (`.tar.gz`) for publication to the Python Package Index (PyPI).
- `python -m build`: Generate `.whl` and `.tar.gz` in `./dist/`.
- `pip install -e .`: Install package in local editable mode.
- `twine upload dist/*`: Securely publish release artifacts to PyPI.
pyproject.toml, SemVer, Building & PyPI Publishing
📄 pyproject.toml & PEP 621
Single source of truth for build backend (`setuptools`, `hatchling`, `flit`), project metadata, and dependencies.
🏷️ Semantic Versioning
Follow `MAJOR.MINOR.PATCH` rules to communicate breaking changes, new features, and backwards-compatible fixes.
🛠️ Building & PyPI Upload
Build binary Wheels (`.whl`) & source archives (`.tar.gz`) via `python -m build` and publish with `twine upload`.
=== EXECUTION RESULT ===
[build-system]
requires = ["setuptools>=61.0.0", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "teachcloud-sdk"
version = "1.4.2"
description = "TeachCloud Enterprise Python SDK"
readme = "README.md"
requires-python = ">=3.10"
license = { text = "MIT" }
authors = [
{ name = "TeachCloud Engineering", email = "dev@teachcloud.dev" }
]
dependencies = [
"requests>=2.31.0",
"pydantic>=2.5.0"
]
36. Packaging & Distribution Tooling Comparison
📊 Modern Python Packaging Ecosystem
| Tool | Category | Primary Role | PEP Compliance |
|---|---|---|---|
| setuptools | Build Backend | Standard Python build system backend | PEP 517 / PEP 621 |
| hatchling / flit | Modern Build Backend | Lightweight PEP 621 compliant backends | PEP 517 / PEP 621 |
| build | CLI Builder | Generates `.whl` and `.tar.gz` packages | PEP 517 Frontend |
| twine | PyPI Uploader | Securely uploads artifacts to PyPI via HTTPS | PyPI API Specification |