archive2025

uv for Python projects: from pip replacement to my default tool

uv started in 2024 as a fast replacement for pip and pip-tools, and grew into a project manager. How I use it now for projects, one-file scripts, Python versions and Docker images, and the old pip habit that bites.

3 min read#dev#python#uv

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 install in a uv project. It does not touch pyproject.toml or the lock file. I once installed a package this way out of habit, and the next uv sync removed it again. In a uv project use uv add, and keep uv pip for projects with requirements files.
  • No pip in the venv. A venv made by uv has no pip inside, so a tool that calls .venv/bin/pip fails. My editor plugin did. Use uv 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 .venv and which lock file is used. My projects are small, so I have not needed them.