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 shpowershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"brew install uvuv versionNote: Check if these commands produce different results
uv help
uv --helpAdd 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 projectCreates a project that can contain other projects
uv init --workspace my-workspaceDefault 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-coreuv init --lib example-libuv init --build-backend maturin example-extuv init --build-backend scikit-build-core example-extuv 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-specificInstall core package first, then add optional features
uv add pandas
uv add pandas --optional plot exceluv add --group group_name package_nameuv remove scikit-learnuv export -o requirements.txt
uv export -o requirements.txt --no-hashes # without hashesAligns environment with project dependencies
uv syncCreates a lockfile (automatic on project creation)
uv lockuv treeuv run python -c "import example"uv run hello.pyuv run bash scripts/foo.shuv run --directory example-pkg example-pkguv python list --only-installedUpdate .python-version file, then sync environment:
uv syncInstall dependencies:
uv pip install -e .sudo chown -R $USER ~/.local/share/uvTemporarily run tools like black, flake8, pytest, mypy, etc.
Full command:
uv tool run black hello.pyShort alias (uvx):
uvx black hello.py
uvx ruff check .
uvx pytestuv tool install black ruff mypy pytestuv tool uninstalluv tool listAdd tool executables to PATH
uv tool update-shelluv builduv publishShare 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-workspaceTo create a workspace project:
uv init --workspace my-workspaceUV supports pip commands as drop-in replacements
uv pip install -r requirements.txt
uv pip list
uv pip freezeNot UV-specific; requires tree command
tree example-appuv cache clean # Clear cache
uv cache prune # Remove outdated cache
uv cache dir # Show cache directoryuv tool dir # Tool directory
uv python dir # Python versions directoryuv self update