Modern Python Packaging with pyproject.toml
Intermediatev1.0.0
Set up Python projects with pyproject.toml — dependency management with uv or poetry, virtual environments, building packages, and publishing to PyPI following PEP 621/517.
Content
Overview
pyproject.toml is the modern standard for Python project configuration (PEP 621). It replaces setup.py, setup.cfg, requirements.txt, and MANIFEST.in with a single, declarative configuration file.
Why This Matters
- -Single file — replaces setup.py + setup.cfg + requirements.txt
- -Standard — PEP 621 is the official Python packaging standard
- -Tool support — uv, poetry, pip, hatch all support pyproject.toml
- -Reproducible — lock files ensure consistent environments
Step 1: Project Structure
Step 2: pyproject.toml Configuration
Step 3: Dependency Management with uv
Step 4: Virtual Environments
Step 5: Build and Publish
Best Practices
- -Use pyproject.toml for ALL project configuration (no setup.py)
- -Pin Python version with
.python-versionfile - -Use
src/layout to prevent import confusion - -Use uv for dependency management (10-100x faster than pip)
- -Always use virtual environments — never install globally
- -Generate lock files for reproducible builds
Common Mistakes
- -Using requirements.txt as the primary dependency specification
- -Not using a virtual environment (pollutes global Python)
- -Mixing setup.py and pyproject.toml
- -Not pinning dependency versions for applications (use lock files)
FAQ
Discussion
Loading comments...