Cheat sheet
uv commands cheat sheet for Python AI projects
Install, init, add, lock, sync, run, uvx, Python versions, scripts, PyTorch indexes and env vars, checked on uv 0.13.9.
Bookmark
Bookmarks are kept in your free account, so you can find this page again on any device.
Sign in to bookmarkOn this page
uv is Astral’s Python package and project manager: one binary that installs Python, creates the virtual environment, resolves a lockfile and runs your code. For an AI project, use the project commands (uv init, uv add, uv run) and commit uv.lock; use uvx for one-off tools and MCP servers. Every command below ran on uv 0.13.9 on 21 November 2026.
Which uv command to use
| You want to | Use | Note |
|---|---|---|
| Start a new app, agent or MCP server | uv init |
Writes pyproject.toml, .python-version, README and git repo |
| Add or remove a library | uv add, uv remove |
Updates pyproject.toml, uv.lock and .venv together |
| Run code with the project’s dependencies | uv run |
Locks and syncs first, so no activation step |
| Run a single file with its own dependencies | uv run --script or uv add --script |
Dependencies live in the file (PEP 723 block) |
| Run a CLI tool or MCP server once | uvx <tool> |
Isolated environment, cached; same as uv tool run |
| Keep a CLI tool on your PATH | uv tool install <tool> |
Upgrade later with uv tool upgrade |
Work with an old requirements.txt setup |
uv pip ... |
pip-style interface, needs a virtual environment |
Install and update uv
# macOS and Linux (standalone installer)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Windows (PowerShell)
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
# Package managers
pipx install uv
brew install uv
# Update: standalone installer only; otherwise use your package manager
uv self update
pip install --upgrade uv
# Check
uv --version
Projects
# New project in a new folder, or in the current empty folder
uv init my-agent
uv init
# Choose the Python version the project requires
uv init my-agent --python 3.12
# Flat layout with main.py and no build system
uv init my-agent --no-package
# Only a pyproject.toml
uv init my-agent --bare
# Print the project version, build sdist and wheel into dist/
uv version
uv build
Since uv 0.12, uv init creates a src/<name>/ package with a build system by default. Add --no-package if you want the older flat layout with main.py.
Dependencies
| Command | What it does |
|---|---|
uv add httpx |
Add a dependency with a lower bound, for example httpx>=0.28.1 |
uv add "httpx>=0.20" |
Add with your own version constraint |
uv add "pandas[excel]" |
Add with extras |
uv add --dev pytest |
Add to the dev group under [dependency-groups] |
uv add --group lint ruff |
Add to a named group |
uv add httpx --optional network |
Add as an optional extra named network |
uv add git+https://github.com/encode/httpx --tag 0.27.0 |
Git dependency; also --branch or --rev |
uv add -r requirements.txt |
Import every line of a requirements file |
uv remove httpx |
Remove it, and its [tool.uv.sources] entry if unused |
uv tree |
Show the dependency tree |
Lock and sync
| Command | What it does |
|---|---|
uv lock |
Create or update uv.lock |
uv lock --check |
Fail if the lockfile is out of date; change nothing |
uv lock --upgrade |
Upgrade every package within your constraints |
uv lock --upgrade-package httpx |
Upgrade one package, keep the rest locked |
uv sync |
Make .venv match the lockfile exactly (removes extra packages) |
uv sync --locked |
Error instead of updating a stale lockfile; use in CI |
uv sync --frozen |
Use the lockfile as is, without checking it |
uv sync --no-dev |
Leave out the dev group |
uv sync --extra network, --all-extras |
Include one extra, or all of them |
uv export --format requirements.txt |
Write the lockfile out as requirements.txt |
Commit uv.lock and never edit it by hand. The dev group syncs by default; extras do not.
Run code
# Run a file or command inside the project environment
uv run main.py
uv run python -c "import httpx; print(httpx.__version__)"
uv run pytest
# Add a package for this run only
uv run --with rich main.py
# Load environment variables (API keys) from a file for this run
uv run --env-file .env main.py
# Run a script without installing the current project
uv run --no-project example.py
uv run locks and syncs before every run, but syncs inexactly: it adds what is missing and leaves extra packages alone. Add --locked or --frozen to stop it touching uv.lock.
Single-file scripts
# Create a script with an inline metadata block
uv init --script example.py --python 3.12
# Declare its dependencies inside the file
uv add --script example.py 'requests<3' 'rich'
# Run it, and optionally lock it to example.py.lock
uv run example.py
uv lock --script example.py
The block uv add --script writes at the top of the file:
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "requests<3",
# "rich>=15.0.0",
# ]
# ///
To run the file directly after chmod +x, make its first line #!/usr/bin/env -S uv run --script. Inside a project, a script with this block ignores the project’s dependencies.
Tools and MCP servers
# Run a tool once (uvx is an alias of uv tool run)
uvx ruff check
uvx ruff@latest check
# The command name differs from the package name
uvx --from httpie http --version
# Run an MCP server published on PyPI
uvx mcp-server-fetch
# Install, list, upgrade and remove persistent tools
uv tool install ruff
uv tool list
uv tool upgrade --all
uv tool uninstall ruff
# Put the tool bin directory on PATH if uv warns it is missing
uv tool update-shell
In an MCP client config, uvx becomes the command: "command": "uvx", "args": ["mcp-server-fetch"]. For your own server in a project folder, the MCP quickstart uses "command": "uv" with "args": ["--directory", "/ABSOLUTE/PATH/TO/weather", "run", "weather.py"].
Python versions
| Command | What it does |
|---|---|
uv python install 3.12 |
Install a version; several at once with uv python install 3.11 3.12 |
uv python list |
Show available and installed versions |
uv python find 3.12 |
Print the path of the interpreter uv would use |
uv python pin 3.12 |
Write .python-version in the current folder |
uv python pin --global 3.12 |
Write a user-level .python-version |
uv python upgrade 3.12 |
Move to the latest supported patch release |
uv downloads a missing Python version automatically when a command needs it. Pass --no-python-downloads to stop that for one command.
pip-style interface
# Virtual environment at .venv (add --clear to replace an existing one)
uv venv
uv venv --python 3.11
source .venv/bin/activate
# Lock requirements and install them
uv pip compile requirements.in -o requirements.txt
uv pip install -r requirements.txt
uv pip sync requirements.txt
uv pip list
Unlike pip, uv pip refuses to install outside a virtual environment unless you pass --system, which the docs suggest only for CI and containers.
PyTorch indexes
# pyproject.toml: CPU-only torch and torchvision from the PyTorch index
[tool.uv.sources]
torch = [
{ index = "pytorch-cpu" },
]
torchvision = [
{ index = "pytorch-cpu" },
]
[[tool.uv.index]]
name = "pytorch-cpu"
url = "https://download.pytorch.org/whl/cpu"
explicit = true
For CUDA, the docs use the same shape with https://download.pytorch.org/whl/cu128 (also cu118, cu126, cu130) and a marker = "sys_platform == 'linux' or sys_platform == 'win32'", because PyTorch publishes no CUDA builds for macOS. With the pip interface, uv pip install torch --torch-backend=auto picks the index from your GPU driver; the flag works only under uv pip.
Environment variables
| Variable | Effect |
|---|---|
UV_PYTHON |
Python interpreter uv uses for all operations |
UV_PYTHON_DOWNLOADS |
Whether uv may download Python versions |
UV_PROJECT_ENVIRONMENT |
Path of the project’s virtual environment instead of .venv |
UV_LOCKED |
Fail if uv.lock would need to change |
UV_FROZEN |
Run without updating uv.lock |
UV_NO_SYNC |
Skip updating the environment when running commands |
UV_NO_DEV |
Leave out development dependencies |
UV_ENV_FILE |
.env files to load for uv run |
UV_DEFAULT_INDEX |
Default package index (replaces the deprecated UV_INDEX_URL) |
UV_INDEX |
Extra indexes, separated by spaces |
UV_TORCH_BACKEND |
PyTorch backend for uv pip, such as cpu, cu126 or auto |
UV_CACHE_DIR, UV_NO_CACHE |
Move the cache, or turn it off |
UV_HTTP_TIMEOUT |
HTTP read timeout in seconds (default 30) |
UV_COMPILE_BYTECODE |
Compile to bytecode after install, useful in Docker images |
UV_INSTALL_DIR, UV_NO_MODIFY_PATH |
Standalone installer: where to put uv, and leave PATH alone |
Common mistakes
| Mistake | Fix |
|---|---|
uv python pin 3.12 fails with “incompatible with the project requires-python” |
uv init set requires-python to the newest Python; use uv init --python 3.12 or lower the bound in pyproject.toml |
uv run main.py fails with “No such file or directory” in a new project |
The default layout is src/<name>/; run the entry point with uv run <name>, or create the project with --no-package |
uv venv stops because .venv already exists |
Add --clear, or use uv sync in a project |
Running pytest or mypy with uvx cannot import your code |
uvx uses an isolated environment; use uv run pytest inside the project |
| CI installs different versions from your laptop | Commit uv.lock and run uv sync --locked in CI |
uv self update refuses to run |
It works only for the standalone installer; upgrade with pip, pipx or brew instead |
Check it works
uv --version
uv lock --check
uv run python -c "import sys; print(sys.version)"
uv tree --depth 1
If the lock check passes and the Python version matches .python-version, the project is reproducible on another machine. To put uv to work, build an MCP server in Python or build an AI agent in Python, and see the Modern Python setup skill for an agent that sets projects up this way.
Check your understanding
uv commands quiz
Choosing uv commands, project layout, adding dependencies, locking and syncing, scripts, uvx for tools and MCP servers, Python pins and CI.
9 questions · pass mark 70%
Review
Checked against
Related
- Build an MCP server in Python with FastMCP
Build a small MCP server in Python with FastMCP, test its tools in MCP Inspector without a model, then connect it to Claude Code, Cursor or another MCP host.
- Build your first agent in Python
Three tools, a hard turn limit, a clear stop condition and a log of every tool call it makes.
- Modern Python setup (Trail of Bits)
Trail of Bits' skill that sets up or migrates Python projects to uv, ruff and ty, with pyproject templates, PEP 723 scripts, pre-commit hooks and Dependabot.
- Ollama commands cheat sheet
Ollama CLI commands, in-chat commands, Modelfile instructions, environment variables and API endpoints on one page, checked against the Ollama docs and source.