Python Package Management¶
Note
This guide covers the evolution from traditional tools to modern next-generation solutions, helping you choose the right tool for your workflow.
Key Areas¶
- Package version Management
- Python version Management
- Environment management
- Package building
- Package publishing
Concepts¶
-
pyproject.toml: Standard configuration file defined in PEP 518 for Python projects- Replaces traditional
setup.py,setup.cfg, andrequirements.txt - Used by modern build tools like Poetry, PDM, Hatch
- Contains project metadata, dependencies, build requirements, dev dependencies
-
Example structure:
- Replaces traditional
-
Pipfile: TOML-based dependency specification file used by pipenv- Replaces
requirements.txtwith a more structured format - Contains
[packages]section for runtime dependencies and[dev-packages]for development dependencies - Supports version constraints and source specifications
- Paired with
Pipfile.lockfor reproducible builds -
Example:
- Replaces
-
wheelvssdist:- Wheel (.whl): Pre-built distribution format that doesn't require compilation during installation
- Faster installation, especially for packages with C extensions
- Platform-specific (may need different wheels for different OSs/architectures)
- Defined in PEP 427
- Source Distribution (sdist): Contains source code and build instructions
- More portable but may require compilation during installation
- Contains
setup.pyorpyproject.tomlplus source files
- Wheel (.whl): Pre-built distribution format that doesn't require compilation during installation
-
Dependency Resolution:
- Pinned requirements: Exact versions (
package==1.2.3) - Compatible release: Allows patch updates (
package~=1.2.0means ≥1.2.0, <1.3.0) - Version ranges: Flexible constraints (
package>=1.0,<2.0) - Lock files:
poetry.lock,Pipfile.lock, etc. - ensure reproducible installations
- Pinned requirements: Exact versions (
-
Editable installs (
pip install -e .):- Installs package in "development mode"
- Changes to source code immediately reflected without reinstalling
- Essential for library development workflows
-
Virtual environments:
- Isolated Python environments to avoid dependency conflicts
- Each project can have its own dependencies without affecting system Python
- Tools:
venv(standard library),virtualenv,conda,poetry, etc.
-
Package Metadata:
- Entry points: Define console scripts and plugin hooks
- Classifiers: Standardized tags for categorizing packages on PyPI
- Package discovery: How to find modules to include (e.g.,
find_packages()) - Python version compatibility: Specify supported Python versions
Tools¶
Traditional¶
-
pip: Standard package installer for Python- Installs packages from PyPI or local files
- Basic commands:
pip install,pip uninstall,pip list,pip freeze - Supports requirements files:
pip install -r requirements.txt - Can install from git:
pip install git+https://github.com/user/repo.git - Limitations: No dependency resolution, no virtual environment management
-
venv: Built-in virtual environment tool (Python 3.3+)- Creates isolated Python environments:
python -m venv myenv - Activate with:
source myenv/bin/activate(Unix) ormyenv\Scripts\activate(Windows) - Deactivate with:
deactivate - Lightweight and part of standard library
- Creates isolated Python environments:
-
virtualenv: Third-party virtual environment tool (predecessor to venv)- More feature-rich than venv (can copy system packages, etc.)
- Can create environments with different Python versions
- Still useful for older Python versions or advanced use cases
-
setuptools: Traditional packaging library- Provides
setup.pyfor package configuration - Includes
easy_install(deprecated) andpkg_resources - Used with
python setup.py installorpip install . - Being replaced by modern standards but still widely used
- Provides
-
wheel: Binary package format for faster installations- Build wheels with:
python -m pip wheel .orpython setup.py bdist_wheel - Platform-specific (.whl files)
- Much faster than installing from source, especially for packages with C extensions
- Build wheels with:
Conda-like¶
-
conda- Cross-platform package manager and environment manager
- Manages both Python and non-Python dependencies
- Uses channels (defaults, conda-forge, etc.) for package distribution
- Creates isolated environments with
conda create -n myenv python=3.9 miniconda: Minimal installer for conda (includes conda + Python + essential packages)
-
mambaC++ reimplementation ofconda- Faster dependency resolution and installation
- Drop-in replacement for conda commands
micromamba: Lightweight, standalone version of mamba (no base environment)
-
pixiConda-like (Compatibility docs), Rust-based- Fast, modern alternative to conda
- Uses
pixi.tomlfor project configuration - Supports conda, PyPI, and local package sources
- Built-in task runner and environment management
Environment¶
-
pip-tools: Enhanced pip workflow with lock filespip-compilegeneratesrequirements.txtfromrequirements.in- Ensures consistent, reproducible installations across environments
- Supports nested requirements and version constraints
-
pipenv: Combines pip + virtualenv functionality- Generates
Pipfile(declarative) andPipfile.lock(exact versions) - Automatically manages virtual environments
- Security features: checks for known vulnerabilities
- Generates
-
hatch: Modern project management tool- Environment management with isolated workspaces
- Build system with support for multiple formats
- Publishing to PyPI with metadata validation
- Plugin system for extensibility
-
rye: Rust-based tool for Python project management- Fast dependency resolution and installation
- Built-in Python version management
- Project scaffolding and environment isolation
-
tox: Testing automation across multiple environments- Runs tests in isolated virtual environments
- Supports multiple Python versions and dependency sets
- Integrates with CI/CD pipelines for comprehensive testing
Next-gen¶
-
poetry: Comprehensive dependency management and packaging- Advanced dependency resolver with proper conflict resolution
- Built-in virtual environment management
- Declarative configuration with
pyproject.toml - Lock file (
poetry.lock) for reproducible builds - Publishing workflow with version management
- Popular in production environments
-
pdm: Modern package and dependency manager- Uses PEP 582 for local package installation (like
node_modules) - Fast dependency resolver written in Rust
- Supports both PEP 517/518 and legacy
setup.py - Flexible plugin system
- Cross-platform with good Windows support
- Uses PEP 582 for local package installation (like
-
uv: Ultra-fast Python package installer and resolver- Rust-based implementation for speed (10-100x faster than pip)
- Drop-in replacement for pip, venv, and poetry workflows
- Global dependency cache for instant installs
- Built-in virtual environment management
- Supports all standard Python packaging formats
Recommended Workflow (2024+)¶
For consistency and modern Python development, we recommend this single toolchain:
Core Tools¶
uv: Ultra-fast package installer and virtual environment managerpyproject.toml: Standard project configurationhatch: Project management, building, and publishingruff: Fast Python linter and formatterpytest: Testing framework
Step-by-Step¶
1. Project Setup:
2. Configure pyproject.toml:
3. Development Workflow:
4. Building and Publishing:
5. Version Management:
Migration from Existing Environments¶
From pip/virtualenv projects:
-
Backup your current environment:
-
Create new uv project:
-
Migrate dependencies:
-
Update your pyproject.toml with the configuration shown above
-
Test and remove old files:
From Poetry projects:
-
Export current dependencies:
-
Initialize new uv project:
-
Import dependencies:
-
Copy source code and tests from your Poetry project
-
Update pyproject.toml with Poetry's project metadata
-
Test the migration:
From Conda environments:
-
Export conda environment:
-
For Python-only projects:
-
For mixed Python/non-Python projects:
- Keep using conda for non-Python dependencies
- Use uv within conda environment for Python packages:
-
Test the setup:
General Migration Tips:
- Start with a fresh directory for the new project
- Test thoroughly before deleting old environments
- Update CI/CD pipelines to use new commands
- Inform team members about the migration
- Consider using
uv runinstead of activating environments in scripts
This consistent workflow eliminates tool choice fatigue while providing modern, fast, and reliable Python package management.