| name | python-package-management |
| description | Manages Python projects using UV package manager exclusively. Use when users need to create Python projects, manage dependencies, handle virtual environments, or work with Python packages. This skill enforces UV workflows for all Python package operations including installing packages, creating projects, managing dependencies, running scripts, and building distributions. NOT when working with existing pip/poetry projects that user explicitly wants to keep unchanged.
|
Python Package Management with UV
This skill provides expert guidance for managing Python projects using UV, the ultra-fast Rust-based package manager. UV replaces pip, pip-tools, virtualenv, poetry, pyenv, and more with a single, blazing-fast tool.
Core Principles
- UV-First Approach: Always use UV for Python package management
- Automatic Environment Management: UV handles virtual environments automatically
- Lock Files for Reproducibility: UV maintains
uv.lock for deterministic builds
- Project-Based Workflow: All Python work happens within UV-managed projects
Project Initialization
Creating New Projects
UV supports different project types based on use case:
Standard Application (default)
uv init myapp
cd myapp
Creates: Flat structure with main.py, no build system. For web apps, scripts, non-distributed applications.
Packaged Application
uv init --package myapp
cd myapp
Creates: src/ layout with build system and console script entry points. For CLI tools distributed via PyPI.
Library
uv init --lib mylib
cd mylib
Creates: src/ layout with build system, py.typed marker. For reusable libraries published to PyPI.
Project Type Selection Guide:
- Web server/internal script → Standard app (
uv init)
- CLI tool for distribution → Packaged app (
uv init --package)
- Reusable library for PyPI → Library (
uv init --lib)
Project Structure
After initialization, projects contain:
myproject/
├── .venv/ # Created on first uv run/sync
├── .python-version # Python version pin
├── .gitignore # Pre-configured for Python
├── pyproject.toml # Project metadata & dependencies
├── uv.lock # Generated on first uv run/sync
├── README.md
└── main.py or src/ # Code location based on type
Dependency Management
Adding Dependencies
Production dependencies:
uv add requests
uv add "fastapi>=0.100.0"
uv add git+https://github.com/user/repo.git
Development dependencies:
uv add --dev pytest ruff mypy
uv add --dev "black>=24.0"
Optional dependency groups:
uv add --group docs sphinx sphinx-rtd-theme
uv add --group lint ruff
From requirements.txt:
uv add -r requirements.txt
Removing Dependencies
uv remove requests
uv remove --dev pytest
uv remove --group docs sphinx
Note: uv remove automatically removes transitive dependencies that are no longer needed.
Managing Dependencies
Lock dependencies (update uv.lock from pyproject.toml):
uv lock
Sync environment (install from uv.lock):
uv sync
uv sync --no-dev
uv sync --group docs
uv sync --all-groups
Upgrade specific package:
uv lock --upgrade-package requests
Upgrade all packages:
uv lock --upgrade
Running Code
Execute Scripts/Commands
UV automatically creates/syncs environment before running:
uv run python script.py
uv run python -m module_name
uv run pytest
uv run myapp
With temporary dependencies:
uv run --with requests python script.py
uv run --with "numpy>=1.20" python analysis.py
Skip auto-sync (when environment is known up-to-date):
uv run --no-sync python script.py
Interactive Python
uv run python
uv run ipython
Python Version Management
Install Python Versions
uv python install 3.12
uv python install 3.11 3.12 3.13
uv python list
Pin Project Python Version
uv python pin 3.12
uv python pin 3.12.5
Use Specific Python Version
uv venv --python 3.11
uv run --python 3.12 python script.py
Virtual Environment Operations
While UV manages environments automatically, manual operations are available:
Create virtual environment:
uv venv
uv venv --python 3.12
Manual activation (rarely needed with uv run):
source .venv/bin/activate
.venv\Scripts\activate
Why UV's auto-management is preferred:
uv run automatically uses project environment
- No manual activation needed
- Guarantees environment is synced with lockfile
- Prevents "works on my machine" issues
Tool Management
UV provides uvx (alias for uv tool run) for running CLI tools without installing them in the project:
Run tool once:
uvx ruff check .
uvx black --check .
uvx pytest
Install tool globally:
uv tool install ruff
uv tool install black
List installed tools:
uv tool list
Update tool:
uv tool upgrade ruff
Building and Publishing
Build Distributions
uv build
uv build --wheel
uv build --sdist
Publishing
For packaged libraries:
uv publish
uv publish --token <token>
Migration from Other Tools
From pip + virtualenv
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
uv init .
uv add -r requirements.txt
uv run python script.py
From Poetry
uv sync
uv run pytest
From requirements.txt
uv init .
uv add -r requirements.txt
Common Workflows
New Project Setup
uv init --package myproject
cd myproject
uv add fastapi uvicorn
uv add --dev pytest ruff
uv run python -m myproject
uv run pytest
Daily Development
uv add new-package
uv run python main.py
uv run pytest
uvx ruff format .
uvx mypy src/
Collaboration Workflow
git clone <repo-url>
cd project
uv sync
uv run python main.py
uv add new-dependency
git add pyproject.toml uv.lock
git commit -m "Add new dependency"
CI/CD Setup
uv sync --frozen
uv run pytest
uv build
Key Flags and Options
Lock Behavior
--locked: Fail if lockfile needs update (CI/CD)
--frozen: Use lockfile without checking if current (fastest)
--upgrade: Upgrade all packages to latest compatible
--upgrade-package <pkg>: Upgrade specific package
Sync Behavior
--no-dev: Exclude development dependencies
--all-groups: Include all optional groups
--group <name>: Include specific group
--no-group <name>: Exclude specific group
--exact: Remove packages not in lockfile
Run Behavior
--with <pkg>: Add temporary dependency
--no-sync: Skip environment sync check
--python <version>: Use specific Python version
--no-project: Run in isolated environment
Troubleshooting
Environment Issues
Environment out of sync:
rm -rf .venv
uv sync
Lockfile conflicts after merge:
uv lock
uv sync
Python version mismatch:
uv python install 3.12
uv python pin 3.12
uv sync
Dependency Conflicts
Check for conflicts:
uv tree
Force resolution update:
uv lock --upgrade
Performance Tips
Use global cache (automatic):
- UV caches packages globally at
~/.cache/uv/
- Deduplicates across projects
- Dramatically speeds up repeated installs
Leverage frozen lockfile in production:
uv sync --frozen
Best Practices
- Always commit
uv.lock: Ensures reproducible builds across team
- Use
uv run over manual activation: Guarantees correct environment
- Pin Python version: Add
.python-version to repository
- Use dependency groups: Separate dev/test/docs dependencies
- Let UV manage everything: Don't mix pip/poetry commands
- Leverage
uvx for tools: Keep project dependencies clean
- Use
--frozen in CI: Fail fast on lockfile issues
Quick Reference
uv init --package myapp
uv add requests
uv run python main.py
uv sync
uv lock
uv add --dev pytest
uv run pytest
uvx ruff format .
uv python install 3.12
uv python pin 3.12
uvx ruff check .
uv tool install ruff
uv build
uv publish
When NOT to Use This Skill
- User explicitly wants to keep existing pip/poetry workflow
- Working in non-Python projects
- User is debugging UV itself
- Legacy Python 2 projects (UV requires Python 3.8+)