Astral released uv in February 2024. At first it was a fast replacement
for pip, pip-tools and venv, and I used it for exactly that, mostly to make the install step in CI
shorter. Since version 0.3 (August 2024) it also manages projects, lock files, Python versions and tools. This
month I moved my last Poetry project to uv, so now everything I write in Python uses it.
New project
uv init sensor-tool
cd sensor-tool
uv add paho-mqtt httpx
uv add --dev pytest ruff
uv run pytest
uv add updates pyproject.toml and uv.lock and installs into .venv. uv run makes sure the environment
matches the lock file before it runs the command. I do not activate the venv anymore; I put uv run in front.
For a project from Git, uv sync creates .venv with the exact versions from uv.lock. The lock file is
committed.
Python versions
uv python install 3.13
uv python pin 3.12 # writes .python-version for this project
This replaced pyenv for me. uv downloads standalone Python builds, so nothing compiles, which is nice on a Raspberry Pi.
One-file scripts
For single scripts I use inline metadata (PEP 723):
uv add --script check_broker.py paho-mqtt
uv run check_broker.py
The dependencies go into a comment block at the top of the script. I copy the file to another machine with uv installed, and it runs. I have many small scripts on different Pis, so this is the feature I use most.
Command-line tools work the same way: uvx ruff check . runs a tool once without installing it, and
uv tool install ruff puts it on the PATH.
Older projects with requirements files
Projects that still use requirements.in and pip-compile do not need to change their layout. The uv pip
commands replace the old tools one to one, and they are what I first used uv for:
| Old | uv |
|---|---|
python -m venv .venv |
uv venv |
pip-compile requirements.in |
uv pip compile requirements.in -o requirements.txt |
pip-sync requirements.txt |
uv pip sync requirements.txt |
pip install -r requirements.txt |
uv pip install -r requirements.txt |
uv pip sync installs exactly what is in the file and removes everything else, which is what you want in CI.
uv’s resolver can choose different versions than pip when several are valid, so commit the compiled
requirements.txt.
Docker
The uv image provides the binary, so the build stage copies it in:
FROM python:3.12-slim
COPY --from=ghcr.io/astral-sh/uv:0.7.13 /uv /uvx /bin/
ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy
WORKDIR /app
# dependencies first, for layer caching
COPY pyproject.toml uv.lock ./
RUN uv sync --locked --no-dev --no-install-project
COPY . .
RUN uv sync --locked --no-dev
ENV PATH="/app/.venv/bin:$PATH"
CMD ["python", "-m", "sensor_tool"]
--locked makes the build fail when uv.lock does not match pyproject.toml. I want that error in CI, not a
silent new resolve. The uv image tag is pinned, because uv releases very often.
Traps
uv pip installin a uv project. It does not touchpyproject.tomlor the lock file. I once installed a package this way out of habit, and the nextuv syncremoved it again. In a uv project useuv add, and keepuv pipfor projects with requirements files.- No pip in the venv. A venv made by uv has no
pipinside, so a tool that calls.venv/bin/pipfails. My editor plugin did. Useuv pip, or install pip into the venv if a tool really needs it. - Workspaces. Several packages in one repository work, but I have not learned the rules for which
.venvand which lock file is used. My projects are small, so I have not needed them.