Lesson 2 of 59Your Python toolbox
uv for Real Projects
Run a whole project with uv, from Python versions and dependency groups to lockfiles, CI, Docker, single-file scripts and migrating from pip.
You already know the basic uv commands, uv init, uv add and uv run, from Virtual Environments in the Python course. On a real team, the questions get harder. Why did CI install a different version than your laptop? Should uv.lock be committed, and what happens when two people change it at once? How do you share a one-file script that needs httpx without a whole project? And how do you move a five-year-old repo off requirements.txt? This lesson treats uv as what it is: one tool that owns your Python version, your environment, your dependencies and your lockfile, and shows how those pieces fit together.
One tool, five old jobs
Before uv, a Python project needed a stack of tools that each did one part of the job. uv replaces all of them with one binary and one mental model.
| Job | The old way | uv |
|---|---|---|
| Install Python versions | pyenv, the python.org installer | uv python install, uv python pin |
| Create environments | python -m venv, virtualenv | Automatic (.venv), or uv venv |
| Declare and install dependencies | pip, requirements.txt | uv add, uv remove |
| Lock exact versions | pip-tools, Poetry | uv lock (automatic) |
| Run global CLI tools | pipx | uv tool install, uvx |
The important idea is that uv treats these as one system. When you run uv run pytest, uv checks that the right Python is available, that .venv exists, that uv.lock matches pyproject.toml, and that .venv matches uv.lock. It fixes whatever is out of date, then runs the command. You stop thinking about "did I activate the venv" or "did I reinstall after pulling".
pyproject.toml holds your intent: direct dependencies with loose ranges like fastapi>=0.115. You edit it, usually through uv add.
Python versions
uv can download and manage Python itself, so you never need a system-wide install to start a project.
uv python install 3.13 # download a managed CPythonuv python pin 3.13 # write .python-version in this folderuv python list # what is installed and availableTwo settings work together here. requires-python = ">=3.12" in pyproject.toml says which versions your project supports, and uv uses it when resolving: it will not pick a package version that drops support for a Python you claim to support. .python-version says which version this checkout uses for its .venv. Commit both. When a teammate runs uv sync, uv downloads 3.13 if they do not have it.
pyproject.toml anatomy
Here is a realistic FastAPI service file:
[project]
name = "todo-api"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"fastapi[standard]>=0.115",
"sqlalchemy>=2.0",
"pydantic-settings>=2.6",
"psycopg[binary]>=3.2",
]
[dependency-groups]
dev = ["pytest>=8.3", "pytest-cov>=6.0", "ruff>=0.9", "mypy>=1.14"]
docs = ["mkdocs-material>=9.5"][project] dependenciesare what the app needs to run in production.uv add httpxwrites here, with a lower bound on the version it just installed.[dependency-groups]are for development only.uv add --dev pytestwrites to thedevgroup, anduv add --group docs mkdocs-materialcreates any group you like. They are never installed for users of a published package.- Extras like
fastapi[standard]are optional feature sets a package offers.[standard]pulls in Uvicorn, thefastapiCLI and other common pieces.
uv sync installs your dependencies plus the dev group by default. uv sync --no-dev leaves dev tools out (what you want in a production image), --group docs adds another group, and --all-groups installs everything.
Removing is just as easy: uv remove httpx deletes it from pyproject.toml, re-locks, and uninstalls it along with anything only it needed. uv tree prints the full dependency tree, which answers "why is this package even installed?"
uv add --dev pytest. Where does pytest end up?uv.lock and reproducible installs
uv.lock records the exact version, source URL and hash of every package in the resolution. Three properties make it more useful than a pip freeze file:
- It is universal. uv resolves for every platform and supported Python at once. Open a lockfile made on a Mac and you will see packages like
coloramathat only Windows needs, with markers saying when they apply. One file works on macOS laptops, Windows laptops and Linux CI. - It has hashes. If a package file on the index ever changed, the install would fail instead of silently running different code.
- It is generated, never edited. Change
pyproject.toml(or runuv add), and uv updates the lock.
Commit it, always, for applications. If two branches both change dependencies and git reports a conflict in uv.lock, do not hand-merge it: take either side, then run uv lock to regenerate it from the merged pyproject.toml.
Upgrades are explicit. uv lock --upgrade-package sqlalchemy moves one package to the newest version your ranges allow. uv lock --upgrade moves everything. Do these in their own pull request, so if something breaks you know why.
Two flags matter in automation, and they are easy to mix up:
| Flag | Meaning | Use it when |
|---|---|---|
--locked | Fail if uv.lock is out of date with pyproject.toml | CI: catch someone who edited pyproject.toml but did not commit the new lock |
--frozen | Use uv.lock as it is, without checking it against pyproject.toml | Docker builds, where only the lock may be copied in yet |
Match each command to its job.
uv in CI and Docker
In GitHub Actions, the official astral-sh/setup-uv action installs uv and can cache downloaded packages between runs. After checkout, the job runs uv sync --locked, then your checks with uv run:
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v6
with:
enable-cache: true
- run: uv sync --locked
- run: uv run ruff check .
- run: uv run pytestPin the action to a release you have checked; the CI/CD lesson later builds the full workflow. In a Dockerfile (built in full in the deploying lesson) the pattern is: copy pyproject.toml and uv.lock first, run uv sync --frozen --no-dev --no-install-project so the dependency layer is cached, then copy your code and sync again. Both rely on the same promise: the lockfile is the single source of truth, so your laptop, CI and production all install identical packages.
Order the steps of a CI job for a uv project.
- Tap the steps below in the right order.
Single-file scripts with inline metadata
Not everything deserves a project. For a one-file script, say a data export or a quick API check, Python has a standard (PEP 723) for writing dependencies inside the file, in a special comment block. uv add --script writes it for you:
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "httpx>=0.28",
# "rich>=13.9",
# ]
# ///
import httpx
from rich import print
users = httpx.get("https://api.example.com/users", timeout=10).json()
print(users[:3])uv run export_users.py reads the block, builds a cached, throwaway environment with those packages, and runs the script. Send the file to a colleague and it just works for them too. You can even add a shebang line (#!/usr/bin/env -S uv run --script) and run it directly. The block is plain TOML inside comments, so any tool can read it. Here is the idea with only the standard library:
Press Run (⌘ Enter) to see what happens.uvx for one-off tools
uvx (short for uv tool run) runs a command-line tool in a temporary, cached environment without adding it to your project or installing it globally:
uvx ruff check . # lint any folder, no installuvx pycowsay "hello" # try a tool onceuvx --from httpie http GET https://example.com # package name differs from the commanduv run --with rich python # a REPL with your project plus one extra packageUse uvx for tools you run occasionally, uv tool install for tools you use every day across projects, and uv add --dev for tools the project itself depends on, like the exact Ruff version CI uses.
Migrating from pip and requirements.txt
Moving an older project is usually a ten-minute job:
uv init --bare # add a minimal pyproject.toml, nothing elseuv add -r requirements.txt # import runtime dependenciesuv add --dev -r requirements-dev.txt # import dev toolsuv run pytest # prove it still worksThen delete the old requirements files, update the README, CI and Dockerfile to use uv sync, and commit pyproject.toml and uv.lock together. One clean-up worth doing: pip freeze output lists every indirect package with an exact pin. After importing, trim pyproject.toml back to the packages your code actually imports, with ranges, and let the lockfile handle the rest. If a deploy platform still insists on a requirements file, generate it instead of maintaining it: uv export --format requirements.txt.
- dependency group
- A named list of development-only dependencies under
[dependency-groups], likedevordocs. - universal lockfile
- A lock that covers every platform and supported Python at once, with markers for platform-specific packages.
- uv sync
- Make
.venvmatchuv.lockexactly, installing missing packages and removing extras. - --locked vs --frozen
--lockedfails if the lock is stale;--frozenuses the lock as-is without checking.- inline script metadata
- A
# /// scriptcomment block (PEP 723) listing a single file's Python version and dependencies. - uvx
- Run a command-line tool once in a temporary cached environment, without installing it.