PYTHON ADVANCED: PROFESSIONAL ENGINEERING MASTERY / L11PACKAGING AND DISTRIBUTION: SHIP YOUR PYTHON
课程 · 12 · 11 / 12
LESSON 11 · ADVANCED · 50 MIN · ◆ 2 INSTRUMENTS

Packaging and Distribution: Ship Your Python

Modern Python packaging with pyproject.toml. Manage dependencies, create virtual environments, publish to PyPI, and structure professional projects.

TIP

Learning Objectives: After this lesson, you'll understand modern Python packaging with pyproject.toml, manage dependencies effectively, create and publish packages to PyPI, and structure professional Python projects.

Modern Python Packaging

Python packaging has evolved significantly. Let's understand the modern approach.

FIG. 02Flow Diagram
INTERACTIVE
LOADING INSTRUMENT
Fig. 02Interactive flow diagrams, timelines, and process visualizations

The Packaging Landscape

FIG. 04Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 04Interactive Python code execution environment

pyproject.toml

FIG. 06Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 06Interactive Python code execution environment

Project Structure

FIG. 08Flow Diagram
INTERACTIVE
LOADING INSTRUMENT
Fig. 08Interactive flow diagrams, timelines, and process visualizations
FIG. 10Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 10Interactive Python code execution environment

Package Initialization

FIG. 12Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 12Interactive Python code execution environment

Dependency Management

Understanding Dependencies

FIG. 14Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 14Interactive Python code execution environment

Lock Files

FIG. 16Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 16Interactive Python code execution environment

Virtual Environments

FIG. 18Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 18Interactive Python code execution environment

Building and Publishing

FIG. 20Flow Diagram
INTERACTIVE
LOADING INSTRUMENT
Fig. 20Interactive flow diagrams, timelines, and process visualizations

Building Your Package

FIG. 22Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 22Interactive Python code execution environment

Publishing to PyPI

FIG. 24Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 24Interactive Python code execution environment

CLI Applications

FIG. 26Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 26Interactive Python code execution environment

Configuration Management

FIG. 28Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 28Interactive Python code execution environment

Best Practices

FIG. 30Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 30Interactive Python code execution environment

Practice Exercise

Complete Package Setup

FIG. 32Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 32Interactive Python code execution environment

Key Takeaways

TopicKey Points
pyproject.tomlSingle config file for metadata and tools
src/ layoutRecommended for libraries
DependenciesSpecify ranges, lock for apps
Virtual envsIsolate project dependencies
BuildingUse python -m build
PublishingTest on TestPyPI first
CLI appsUse Click + entry points

Essential Commands

# Virtual environment python -m venv .venv source .venv/bin/activate # Install in development mode pip install -e ".[dev]" # Build package python -m build # Upload to PyPI twine upload dist/* # Check package pip install --index-url https://test.pypi.org/simple/ mypackage

Next Steps

In the next and final lesson, we'll bring everything together in a Capstone Project: Building a Production-Ready CLI Tool—applying all concepts from the course in a comprehensive project.


Ready for the final project? Let's build something real!


Further Reading

The Modern Python Tooling Stack (2024+)

The packaging ecosystem has consolidated dramatically. If you learned Python before 2023, much of what you know is now legacy. Use these:

  • uv — Astral's all-in-one project + dependency + Python-version manager (Rust-backed, ~10× faster than pip). Replaces pip, pip-tools, pyenv, virtualenv, poetry, pipx in one tool. The 2024 default.
  • hatch — modern PyPA-blessed build backend + project manager. Strong alternative to uv if you prefer PyPA tooling.
  • ruff — formatter + linter that replaces black, flake8, isort, and most of pylint.

Standards & Specs

Tutorials

When You Need More

  • scikit-build-core — the modern way to build C/C++/Rust extensions.
  • maturin — build PyO3-based Rust extensions; what polars, pydantic-core, and ruff use.
  • cibuildwheel — build wheels for every platform/Python version on CI.

Books

  • Book: Publishing Python Packages — Dane Hillard (Manning, 2022). Whole book on pyproject.toml + PyPI workflows.