uv-package-manager
by @wu-uk
Master the uv package manager for fast Python dependency management, virtual environments, and modern Python project workflows. Use when setting up Python pr...
clawhub install fix-build-agentops-uv-package-managerπ About This Skill
name: uv-package-manager description: Master the uv package manager for fast Python dependency management, virtual environments, and modern Python project workflows. Use when setting up Python projects, managing dependencies, or optimizing Python development workflows with uv.
UV Package Manager
Comprehensive guide to using uv, an extremely fast Python package installer and resolver written in Rust, for modern Python project management and dependency workflows.
When to Use This Skill
Core Concepts
1. What is uv?
2. Key Features
3. UV vs Traditional Tools
Installation
Quick Install
# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | shWindows (PowerShell)
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"Using pip (if you already have Python)
pip install uvUsing Homebrew (macOS)
brew install uvUsing cargo (if you have Rust)
cargo install --git https://github.com/astral-sh/uv uv
Verify Installation
uv --version
uv 0.x.x
Quick Start
Create a New Project
# Create new project with virtual environment
uv init my-project
cd my-projectOr create in current directory
uv init .Initialize creates:
- .python-version (Python version)
- pyproject.toml (project config)
- README.md
- .gitignore
Install Dependencies
# Install packages (creates venv if needed)
uv add requests pandasInstall dev dependencies
uv add --dev pytest black ruffInstall from requirements.txt
uv pip install -r requirements.txtInstall from pyproject.toml
uv sync
Virtual Environment Management
Pattern 1: Creating Virtual Environments
# Create virtual environment with uv
uv venvCreate with specific Python version
uv venv --python 3.12Create with custom name
uv venv my-envCreate with system site packages
uv venv --system-site-packagesSpecify location
uv venv /path/to/venv
Pattern 2: Activating Virtual Environments
# Linux/macOS
source .venv/bin/activateWindows (Command Prompt)
.venv\Scripts\activate.batWindows (PowerShell)
.venv\Scripts\Activate.ps1Or use uv run (no activation needed)
uv run python script.py
uv run pytest
Pattern 3: Using uv run
# Run Python script (auto-activates venv)
uv run python app.pyRun installed CLI tool
uv run black .
uv run pytestRun with specific Python version
uv run --python 3.11 python script.pyPass arguments
uv run python script.py --arg value
Package Management
Pattern 4: Adding Dependencies
# Add package (adds to pyproject.toml)
uv add requestsAdd with version constraint
uv add "django>=4.0,<5.0"Add multiple packages
uv add numpy pandas matplotlibAdd dev dependency
uv add --dev pytest pytest-covAdd optional dependency group
uv add --optional docs sphinxAdd from git
uv add git+https://github.com/user/repo.gitAdd from git with specific ref
uv add git+https://github.com/user/repo.git@v1.0.0Add from local path
uv add ./local-packageAdd editable local package
uv add -e ./local-package
Pattern 5: Removing Dependencies
# Remove package
uv remove requestsRemove dev dependency
uv remove --dev pytestRemove multiple packages
uv remove numpy pandas matplotlib
Pattern 6: Upgrading Dependencies
# Upgrade specific package
uv add --upgrade requestsUpgrade all packages
uv sync --upgradeUpgrade package to latest
uv add --upgrade requestsShow what would be upgraded
uv tree --outdated
Pattern 7: Locking Dependencies
# Generate uv.lock file
uv lockUpdate lock file
uv lock --upgradeLock without installing
uv lock --no-installLock specific package
uv lock --upgrade-package requests
Python Version Management
Pattern 8: Installing Python Versions
# Install Python version
uv python install 3.12Install multiple versions
uv python install 3.11 3.12 3.13Install latest version
uv python installList installed versions
uv python listFind available versions
uv python list --all-versions
Pattern 9: Setting Python Version
# Set Python version for project
uv python pin 3.12This creates/updates .python-version file
Use specific Python version for command
uv --python 3.11 run python script.pyCreate venv with specific version
uv venv --python 3.12
Project Configuration
Pattern 10: pyproject.toml with uv
[project]
name = "my-project"
version = "0.1.0"
description = "My awesome project"
readme = "README.md"
requires-python = ">=3.8"
dependencies = [
"requests>=2.31.0",
"pydantic>=2.0.0",
"click>=8.1.0",
][project.optional-dependencies]
dev = [
"pytest>=7.4.0",
"pytest-cov>=4.1.0",
"black>=23.0.0",
"ruff>=0.1.0",
"mypy>=1.5.0",
]
docs = [
"sphinx>=7.0.0",
"sphinx-rtd-theme>=1.3.0",
]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.uv]
dev-dependencies = [
# Additional dev dependencies managed by uv
]
[tool.uv.sources]
Custom package sources
my-package = { git = "https://github.com/user/repo.git" }
Pattern 11: Using uv with Existing Projects
# Migrate from requirements.txt
uv add -r requirements.txtMigrate from poetry
Already have pyproject.toml, just use:
uv syncExport to requirements.txt
uv pip freeze > requirements.txtExport with hashes
uv pip freeze --require-hashes > requirements.txt
Advanced Workflows
Pattern 12: Monorepo Support
# Project structure
monorepo/
packages/
package-a/
pyproject.toml
package-b/
pyproject.toml
pyproject.toml (root)
Root pyproject.toml
[tool.uv.workspace]
members = ["packages/*"]Install all workspace packages
uv syncAdd workspace dependency
uv add --path ./packages/package-a
Pattern 13: CI/CD Integration
# .github/workflows/test.yml
name: Testson: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v2
with:
enable-cache: true
- name: Set up Python
run: uv python install 3.12
- name: Install dependencies
run: uv sync --all-extras --dev
- name: Run tests
run: uv run pytest
- name: Run linting
run: |
uv run ruff check .
uv run black --check .
Pattern 14: Docker Integration
# Dockerfile
FROM python:3.12-slimInstall uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uvSet working directory
WORKDIR /appCopy dependency files
COPY pyproject.toml uv.lock ./Install dependencies
RUN uv sync --frozen --no-devCopy application code
COPY . .Run application
CMD ["uv", "run", "python", "app.py"]
Optimized multi-stage build:
# Multi-stage Dockerfile
FROM python:3.12-slim AS builderInstall uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uvWORKDIR /app
Install dependencies to venv
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev --no-editableRuntime stage
FROM python:3.12-slimWORKDIR /app
Copy venv from builder
COPY --from=builder /app/.venv .venv
COPY . .Use venv
ENV PATH="/app/.venv/bin:$PATH"CMD ["python", "app.py"]
Pattern 15: Lockfile Workflows
# Create lockfile (uv.lock)
uv lockInstall from lockfile (exact versions)
uv sync --frozenUpdate lockfile without installing
uv lock --no-installUpgrade specific package in lock
uv lock --upgrade-package requestsCheck if lockfile is up to date
uv lock --checkExport lockfile to requirements.txt
uv export --format requirements-txt > requirements.txtExport with hashes for security
uv export --format requirements-txt --hash > requirements.txt
Performance Optimization
Pattern 16: Using Global Cache
# UV automatically uses global cache at:
Linux: ~/.cache/uv
macOS: ~/Library/Caches/uv
Windows: %LOCALAPPDATA%\uv\cache
Clear cache
uv cache cleanCheck cache size
uv cache dir
Pattern 17: Parallel Installation
# UV installs packages in parallel by defaultControl parallelism
uv pip install --jobs 4 package1 package2No parallel (sequential)
uv pip install --jobs 1 package
Pattern 18: Offline Mode
# Install from cache only (no network)
uv pip install --offline packageSync from lockfile offline
uv sync --frozen --offline
Comparison with Other Tools
uv vs pip
# pip
python -m venv .venv
source .venv/bin/activate
pip install requests pandas numpy
~30 seconds
uv
uv venv
uv add requests pandas numpy
~2 seconds (10-15x faster)
uv vs poetry
# poetry
poetry init
poetry add requests pandas
poetry install
~20 seconds
uv
uv init
uv add requests pandas
uv sync
~3 seconds (6-7x faster)
uv vs pip-tools
# pip-tools
pip-compile requirements.in
pip-sync requirements.txt
~15 seconds
uv
uv lock
uv sync --frozen
~2 seconds (7-8x faster)
Common Workflows
Pattern 19: Starting a New Project
# Complete workflow
uv init my-project
cd my-projectSet Python version
uv python pin 3.12Add dependencies
uv add fastapi uvicorn pydanticAdd dev dependencies
uv add --dev pytest black ruff mypyCreate structure
mkdir -p src/my_project testsRun tests
uv run pytestFormat code
uv run black .
uv run ruff check .
Pattern 20: Maintaining Existing Project
# Clone repository
git clone https://github.com/user/project.git
cd projectInstall dependencies (creates venv automatically)
uv syncInstall with dev dependencies
uv sync --all-extrasUpdate dependencies
uv lock --upgradeRun application
uv run python app.pyRun tests
uv run pytestAdd new dependency
uv add new-packageCommit updated files
git add pyproject.toml uv.lock
git commit -m "Add new-package dependency"
Tool Integration
Pattern 21: Pre-commit Hooks
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: uv-lock
name: uv lock
entry: uv lock
language: system
pass_filenames: false - id: ruff
name: ruff
entry: uv run ruff check --fix
language: system
types: [python]
- id: black
name: black
entry: uv run black
language: system
types: [python]
Pattern 22: VS Code Integration
// .vscode/settings.json
{
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
"python.terminal.activateEnvironment": true,
"python.testing.pytestEnabled": true,
"python.testing.pytestArgs": ["-v"],
"python.linting.enabled": true,
"python.formatting.provider": "black",
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
}
}
Troubleshooting
Common Issues
# Issue: uv not found
Solution: Add to PATH or reinstall
echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> ~/.bashrcIssue: Wrong Python version
Solution: Pin version explicitly
uv python pin 3.12
uv venv --python 3.12Issue: Dependency conflict
Solution: Check resolution
uv lock --verboseIssue: Cache issues
Solution: Clear cache
uv cache cleanIssue: Lockfile out of sync
Solution: Regenerate
uv lock --upgrade
Best Practices
Project Setup
1. Always use lockfiles for reproducibility 2. Pin Python version with .python-version 3. Separate dev dependencies from production 4. Use uv run instead of activating venv 5. Commit uv.lock to version control 6. Use --frozen in CI for consistent builds 7. Leverage global cache for speed 8. Use workspace for monorepos 9. Export requirements.txt for compatibility 10. Keep uv updated for latest features
Performance Tips
# Use frozen installs in CI
uv sync --frozenUse offline mode when possible
uv sync --offlineParallel operations (automatic)
uv does this by default
Reuse cache across environments
uv shares cache globally
Use lockfiles to skip resolution
uv sync --frozen # skips resolution
Migration Guide
From pip + requirements.txt
# Before
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtAfter
uv venv
uv pip install -r requirements.txt
Or better:
uv init
uv add -r requirements.txt
From Poetry
# Before
poetry install
poetry add requestsAfter
uv sync
uv add requestsKeep existing pyproject.toml
uv reads [project] and [tool.poetry] sections
From pip-tools
# Before
pip-compile requirements.in
pip-sync requirements.txtAfter
uv lock
uv sync --frozen
Command Reference
Essential Commands
# Project management
uv init [PATH] # Initialize project
uv add PACKAGE # Add dependency
uv remove PACKAGE # Remove dependency
uv sync # Install dependencies
uv lock # Create/update lockfileVirtual environments
uv venv [PATH] # Create venv
uv run COMMAND # Run in venvPython management
uv python install VERSION # Install Python
uv python list # List installed Pythons
uv python pin VERSION # Pin Python versionPackage installation (pip-compatible)
uv pip install PACKAGE # Install package
uv pip uninstall PACKAGE # Uninstall package
uv pip freeze # List installed
uv pip list # List packagesUtility
uv cache clean # Clear cache
uv cache dir # Show cache location
uv --version # Show version
Resources
Best Practices Summary
1. Use uv for all new projects - Start with uv init
2. Commit lockfiles - Ensure reproducible builds
3. Pin Python versions - Use .python-version
4. Use uv run - Avoid manual venv activation
5. Leverage caching - Let uv manage global cache
6. Use --frozen in CI - Exact reproduction
7. Keep uv updated - Fast-moving project
8. Use workspaces - For monorepo projects
9. Export for compatibility - Generate requirements.txt when needed
10. Read the docs - uv is feature-rich and evolving
π‘ Examples
Create a New Project
# Create new project with virtual environment
uv init my-project
cd my-projectOr create in current directory
uv init .Initialize creates:
- .python-version (Python version)
- pyproject.toml (project config)
- README.md
- .gitignore
Install Dependencies
# Install packages (creates venv if needed)
uv add requests pandasInstall dev dependencies
uv add --dev pytest black ruffInstall from requirements.txt
uv pip install -r requirements.txtInstall from pyproject.toml
uv sync
π Tips & Best Practices
Project Setup
1. Always use lockfiles for reproducibility 2. Pin Python version with .python-version 3. Separate dev dependencies from production 4. Use uv run instead of activating venv 5. Commit uv.lock to version control 6. Use --frozen in CI for consistent builds 7. Leverage global cache for speed 8. Use workspace for monorepos 9. Export requirements.txt for compatibility 10. Keep uv updated for latest features
Performance Tips
# Use frozen installs in CI
uv sync --frozenUse offline mode when possible
uv sync --offlineParallel operations (automatic)
uv does this by default
Reuse cache across environments
uv shares cache globally
Use lockfiles to skip resolution
uv sync --frozen # skips resolution