| name | uv |
| description | This skill should be used when working with Python projects that use uv for package and project management. Use this skill for running Python scripts and CLI tools with `uv run`, managing dependencies, creating projects, handling virtual environments, and executing commands within isolated project environments. Essential for projects with pyproject.toml files. |
uv - Python Package and Project Manager
Contents
Core Concept: uv run
ALWAYS use uv run instead of bare python in uv-managed projects.
uv run automatically:
- Creates/updates the virtual environment
- Installs project dependencies
- Executes commands in isolation
uv run script.py
uv run pytest
uv run ruff check .
uv run python -m mymodule
Quick Reference
Essential Commands
| Task | Command |
|---|
| Run script | uv run script.py |
| Run CLI tool | uv run pytest |
| Add dependency | uv add requests |
| Add dev dependency | uv add --dev pytest |
| Remove dependency | uv remove requests |
| Sync environment | uv sync |
| Update dependencies | uv lock --upgrade |
| Create project | uv init my-project |
Decision Tree
| Need to... | Use |
|---|
| Run code in project | uv run <command> |
| Add package to project | uv add <package> |
| Run tool once without installing | uvx <tool> |
| Create new project | uv init [name] |
| Update all dependencies | uv lock --upgrade |
| Sync after pulling changes | uv sync |
| Install Python version | uv python install <version> |
Common Workflows
1. Create New Project
uv init my-project
uv init --lib my-library
uv init
uv init --python 3.12
Creates: pyproject.toml, .python-version, README.md, src/
2. Add Dependencies
uv add requests
uv add 'httpx>=0.25,<0.27'
uv add --dev pytest pytest-cov
uv add --optional docs sphinx
3. Run Tests and Tools
uv run pytest
uv run pytest --cov
uv run ruff check .
uv run mypy src/
4. Update Dependencies
uv lock --upgrade
uv lock --upgrade-package numpy
uv sync
5. Build and Publish
uv build
uv publish
Dependency Management
Adding Dependencies
| Type | Command |
|---|
| Runtime | uv add requests |
| Development | uv add --dev pytest |
| Optional group | uv add --optional docs sphinx |
| With constraints | uv add 'numpy>=1.20,<2.0' |
Temporary Dependencies
Use --with for one-off operations without modifying pyproject.toml:
uv run --with httpx python -c "import httpx"
uv run --with rich --with httpx script.py
Syncing Environment
uv sync
uv sync --no-dev
uv sync --all-extras
Python Version Management
uv python install 3.12
uv python list
uv python pin 3.12
uv python find
Scripts with Inline Dependencies
Scripts can declare their own dependencies, isolated from the project:
import requests
import rich
Managing Script Dependencies
uv init --script analyze.py --python 3.12
uv add --script analyze.py pandas
uv run analyze.py
uv lock --script analyze.py
Note: Scripts with inline metadata ignore project dependencies.
Tools Without Installation
uvx ruff check .
uvx black --check .
uv tool install ruff
uv tool list
Troubleshooting
Command Not Found After uv add
Problem: Added package with CLI, but command doesn't work.
Solution: Use uv run:
uv add ruff
uv run ruff check .
Environment Out of Sync
Problem: Dependencies changed but environment hasn't updated.
Solution:
uv sync
Wrong Python Version
Problem: Project requires different Python.
Solution:
uv python install 3.12
uv python pin 3.12
Script Ignores Project Dependencies
Problem: Script with inline metadata doesn't use project packages.
Solution: This is intentional. Either:
- Remove inline metadata to use project deps
- Add needed deps to script's inline metadata
Best Practices
- Always use
uv run - Don't manually activate venvs
- Commit
uv.lock - Ensures reproducible builds
- Pin Python versions - Use
uv python pin
- Use
--with for experiments - Test deps without modifying pyproject.toml
- Use
uvx for one-off tools - Don't pollute project deps
- Let uv manage envs - Avoid manual pip installs
References
Project References
External Resources