| name | python |
| description | Use uv for fast Python project management, script execution, dependency handling, and tool installation. AVOID pip - always use uv commands (uv add, uv sync, uv run) instead. |
Python/uv Development Skill
You are a Python development specialist using uv, an extremely fast Python package manager and project management tool. This skill provides comprehensive workflows, best practices, and common patterns for Python development with uv.
Why uv?
uv is the modern replacement for pip, virtualenv, poetry, and pyenv combined:
- Extremely fast: 10-100x faster than pip
- All-in-one: Package management, virtual environments, Python version management
- Rust-powered: Built for speed and reliability
- Compatible: Works with existing Python projects and tools
IMPORTANT: Avoid pip
DO NOT use pip commands. Always use uv equivalents:
- WRONG:
pip install requests → RIGHT: uv add requests
- WRONG:
pip install -r requirements.txt → RIGHT: uv sync
- WRONG:
pip freeze > requirements.txt → RIGHT: uv lock (creates uv.lock)
- WRONG:
python -m pip install → RIGHT: uv run or uv add
The only exception is legacy projects that explicitly require pip compatibility, and even then prefer migrating to uv.
Core Capabilities
- Project Management: init, add, remove, sync, lock
- Running Code: run (scripts and commands)
- Python Management: python (install, list, pin versions)
- Tools: tool (install and run CLI tools)
- Environments: venv (virtual environment creation)
- pip-compatible: pip interface for legacy workflows
- Building: build, publish packages
Quick Start
Running a Python Script
The most common use case - just run your script:
uv run script.py
uv run script.py arg1 arg2
What this does:
- Automatically creates/uses virtual environment
- Installs dependencies from pyproject.toml if present
- Runs the script with the correct Python version
Running a Script with Dependencies
uv run --with requests script.py
uv run --with requests --with pandas analyze.py
uv run --with-requirements requirements.txt script.py
Running a Python Module
uv run -m module_name
uv run -m pytest tests/
uv run -m black .
One-off Script Execution (No Project)
uv run --isolated script.py
uv run --python 3.11 script.py
uv run --with httpx --with rich my_script.py
Project Management
Creating a New Project
uv init my-app
uv init my-app --app
uv init my-lib --lib
uv init my-script --script
uv init
uv init --bare
Project types:
--app: Application (not meant to be imported)
--lib: Library (meant to be published and imported)
--script: Single-file script with inline dependencies
Managing Dependencies
uv add requests
uv add "requests>=2.31.0"
uv add requests pandas numpy
uv add --dev pytest
uv add --dev black ruff mypy
uv add --extra docs sphinx
uv remove requests
uv remove --dev pytest
uv lock --upgrade
uv lock --upgrade-package requests
Syncing Environment
uv sync
uv sync --no-dev
uv sync --all-extras
uv sync --extra docs
uv sync --exact
Lockfile Management
uv lock
uv lock --upgrade
uv lock --upgrade-package requests
uv lock --offline
Python Version Management
Installing Python Versions
uv python install 3.11
uv python install 3.12.1
uv python install 3.11 3.12
uv python list
uv python list --only-installed
Finding and Pinning Python
uv python find
uv python find 3.11
uv python pin 3.11
uv python pin 3.12.1
Python Version in Projects
When you create a project, uv automatically:
- Creates
.python-version file
- Detects or installs the appropriate Python version
- Uses it for all
uv run commands in that project
Tool Management
Installing CLI Tools
uv tool install ruff
uv tool install black
uv tool install httpie
uv tool install "black==24.1.0"
uv tool run ruff check .
uv tool run black --check .
Managing Installed Tools
uv tool list
uv tool upgrade ruff
uv tool upgrade --all
uv tool uninstall ruff
uv tool dir
Common Tools to Install
uv tool install ruff
uv tool install black
uv tool install mypy
uv tool install pytest
uv tool install tox
uv tool install httpie
uv tool install rich-cli
uv tool install pipx
Virtual Environments
Creating Virtual Environments
uv venv
uv venv --python 3.11
uv venv path/to/venv
uv venv .venv-dev
Using Virtual Environments
source .venv/bin/activate
uv run python script.py
uv run pytest
Best Practice: With uv, you rarely need to manually activate environments. Just use uv run.
Legacy pip Interface (AVOID - Use Native uv Commands Instead)
WARNING: This interface exists only for legacy compatibility. Prefer native uv commands (uv add, uv sync, uv lock) over uv pip commands.
Only use uv pip when:
- Working with a legacy project that cannot be migrated yet
- Maintaining compatibility with existing CI/CD that expects pip commands
- As a temporary bridge during migration
Better alternatives:
uv pip install requests → Use uv add requests instead
uv pip install -r requirements.txt → Use uv sync instead
uv pip freeze → Use uv lock instead
uv pip install requests
uv pip install -r requirements.txt
uv pip list
uv pip show requests
uv pip freeze > requirements.txt
uv pip uninstall requests
Common Workflows
Workflow 1: New Python Project
uv init my-project --app
cd my-project
uv add requests httpx rich
uv add --dev pytest black ruff
uv run python main.py
uv run pytest
Workflow 2: Working with Existing Project
git clone <repo>
cd <repo>
uv sync
uv run python main.py
uv run pytest
Workflow 3: Quick Script with Dependencies
cat > script.py << 'EOF'
import requests
from rich import print
response = requests.get("https://api.github.com")
print(response.json())
EOF
uv run --script script.py
uv run --with requests --with rich script.py
Workflow 4: Testing and Linting
uv run pytest
uv run pytest tests/
uv run pytest -v --cov
uv run black .
uv run ruff format .
uv run ruff check .
uv run ruff check --fix .
uv run mypy .
Workflow 5: Dependency Updates
uv tree
uv lock --upgrade
uv lock --upgrade-package requests
uv sync
uv run pytest
Workflow 6: Building and Publishing
uv build
uv publish
uv publish --index https://test.pypi.org/simple/
Best Practices
1. Always Use uv run
Instead of:
source .venv/bin/activate
python script.py
Do this:
uv run script.py
Benefits:
- Automatic environment management
- Ensures dependencies are synced
- Works consistently across systems
2. Pin Python Versions
Always create .python-version file:
uv python pin 3.11
This ensures everyone on the team uses the same Python version.
3. Use Lockfiles
Commit uv.lock to version control:
- Ensures reproducible builds
- Locks transitive dependencies
- Faster installs for teammates
4. Separate Dev Dependencies
uv add --dev pytest black ruff mypy
This keeps production dependencies clean.
5. Use Scripts Section
In pyproject.toml:
[project.scripts]
my-cli = "my_package.cli:main"
[tool.uv]
dev-dependencies = [
"pytest>=7.0.0",
"black>=24.0.0",
]
Then run:
uv run my-cli
Project Structure
Typical uv Project Layout
my-project/
├── .python-version # Python version (e.g., "3.11")
├── pyproject.toml # Project config and dependencies
├── uv.lock # Lockfile (commit this!)
├── .venv/ # Virtual environment (don't commit)
├── src/
│ └── my_package/
│ ├── __init__.py
│ └── main.py
└── tests/
└── test_main.py
pyproject.toml Example
[project]
name = "my-project"
version = "0.1.0"
description = "My awesome project"
requires-python = ">=3.11"
dependencies = [
"requests>=2.31.0",
"rich>=13.0.0",
]
[project.optional-dependencies]
dev = [
"pytest>=7.0.0",
"black>=24.0.0",
"ruff>=0.1.0",
]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.uv]
dev-dependencies = [
"pytest>=7.0.0",
"black>=24.0.0",
]
Advanced Features
Running with Extras
uv run --extra docs sphinx-build
uv run --all-extras pytest
Dependency Groups
uv add --group test pytest
uv run --group test pytest
uv sync --group test
Environment Variables
uv run --env-file .env script.py
uv run --no-env-file script.py
Offline Mode
uv run --offline script.py
uv sync --offline
uv lock --offline
Custom Indexes
uv add requests --index https://my-pypi.org/simple/
uv add package --index https://index1.org/simple/ --index https://index2.org/simple/
Troubleshooting
Issue: Command not found after uv tool install
Solution: Update shell PATH
uv tool update-shell
Issue: Python version not found
Solution: Install Python with uv
uv python install 3.11
uv python pin 3.11
Issue: Dependencies not syncing
Solution: Force sync
uv sync --reinstall
uv sync --exact
Issue: Cache issues
Solution: Clear cache
uv cache clean
uv run --no-cache script.py
Issue: Lock file out of sync
Solution: Regenerate lock
uv lock --upgrade
uv sync
Performance Tips
1. Use --frozen for CI/CD
uv sync --frozen
uv run --frozen pytest
2. Leverage Cache
uv automatically caches packages. To see cache:
uv cache dir
3. Parallel Operations
uv automatically parallelizes operations. No configuration needed.
Migration from Other Tools
From pip + virtualenv
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python script.py
uv run script.py
From Poetry
uv sync && uv run python script.py
From Pipenv
uv sync && uv run python script.py
Integration with Other Tools
pytest
uv run pytest
uv run pytest --cov --cov-report=html
black/ruff
uv run black .
uv run ruff check --fix .
mypy
uv run mypy src/
jupyter
uv add --dev jupyter
uv run jupyter notebook
Quick Reference
uv run script.py
uv run --with requests script.py
uv init my-project
uv add package-name
uv add --dev pytest
uv sync
uv lock --upgrade
uv python install 3.11
uv python pin 3.11
uv tool install ruff
uv tool run black .
uv pip install package
uv pip install -r requirements.txt
uv run pytest
uv run black .
uv run ruff check .
Key Differences from pip/virtualenv
| Task | Old Way | uv Way |
|---|
| Create venv | python -m venv .venv | uv venv (or automatic) |
| Activate | source .venv/bin/activate | Not needed with uv run |
| Install deps | pip install -r requirements.txt | uv sync |
| Run script | python script.py | uv run script.py |
| Add package | pip install requests | uv add requests |
| Global tool | pip install black | uv tool install black |
Common Patterns
Pattern 1: Quick Data Analysis Script
uv run --with pandas --with matplotlib analyze.py
Pattern 2: Testing Before Commit
uv run pytest && uv run black --check . && uv run ruff check .
Pattern 3: Update All Dependencies
uv lock --upgrade && uv sync && uv run pytest
Pattern 4: Run with Specific Python
uv run --python 3.11 script.py
Pattern 5: Install and Run Tool
uv tool run ruff check .
Summary
Primary directive: Use uv run for executing Python scripts and commands.
Key advantages:
- No manual environment activation needed
- Automatic dependency management
- Extremely fast operations
- Single tool for all Python workflows
Most common commands:
uv run script.py - Run anything
uv add package - Add dependency
uv sync - Sync environment
uv python pin 3.11 - Set Python version