UV is a Python package manager and project tool. This cheatsheet covers installation, project management, dependency handling, and more.
- Installation
- Basic Commands
- Project Initialization
- Managing Dependencies
- Running Code
- Python Version Management
- Tools Management
- Building and Publishing
- Workspaces
- UV Pip Compatibility
- Inspecting Project Structure
- Managing UV State
curl -LsSf https://astral.sh/uv/install.sh | sudo sh
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
brew install uv
uv version
Note: Check if these commands produce different results
uv help
uv --help
Add to .zshrc (for Zsh) or equivalent for Bash to enable on startup
eval "$(uv generate-shell-completion zsh)"
uv init project-name # Basic project
uv init --bare example # Minimal project
Creates a project that can contain other projects
uv init --workspace my-workspace
Default build system is Hatchling; override with --build-backend
uv init --package example-pkg
uv init --package example-pkg --build-backend hatchling/setuptools/maturin/flit-core/scikit-build-core
uv init --lib example-lib
uv init --build-backend maturin example-ext
uv init --build-backend scikit-build-core example-ext
uv add litellm langchain # Multiple packages
uv add -r requirements.txt # From file
uv add requests # Latest version
uv add requests=2.1.2 # Specific version
uv add 'requests<3.0.0' # Version bounds
uv add 'requests; sys_platform="linux"' # Platform-specific
Install core package first, then add optional features
uv add pandas
uv add pandas --optional plot excel
uv add --group group_name package_name
uv remove scikit-learn
uv export -o requirements.txt
uv export -o requirements.txt --no-hashes # without hashes
Aligns environment with project dependencies
uv sync
Creates a lockfile (automatic on project creation)
uv lock
uv tree
uv run python -c "import example"
uv run hello.py
uv run bash scripts/foo.sh
uv run --directory example-pkg example-pkg
uv python list --only-installed
Update .python-version file, then sync environment:
uv sync
Install dependencies:
uv pip install -e .
sudo chown -R $USER ~/.local/share/uv
Temporarily run tools like black, flake8, pytest, mypy, etc.
Full command:
uv tool run black hello.py
Short alias (uvx):
uvx black hello.py
uvx ruff check .
uvx pytest
uv tool install black ruff mypy pytest
uv tool uninstall
uv tool list
Add tool executables to PATH
uv tool update-shell
uv build
uv publish
Share dependencies and Python versions across projects
Sub-projects are automatically added to the workspace
To exclude a project from the workspace:
uv init another_project --no-workspace
To create a workspace project:
uv init --workspace my-workspace
UV supports pip commands as drop-in replacements
uv pip install -r requirements.txt
uv pip list
uv pip freeze
Not UV-specific; requires tree command
tree example-app
uv cache clean # Clear cache
uv cache prune # Remove outdated cache
uv cache dir # Show cache directory
uv tool dir # Tool directory
uv python dir # Python versions directory
uv self update