Sub topic 17
The uv pip interface for existing workflows
This chapter assumes you know what pip and virtual environments do, that you have created a project with uv init, and that you know what pyproject.toml and uv.lock are. It covers uv pip, the part of uv that mimics pip, pip-tools, and venv. You will use it when a project does not have a uv-managed pyproject.toml, or when a script or CI job already calls pip.
Two ways to use uv
uv has two interfaces. The project interface (uv add, uv lock, uv sync, uv run) manages a pyproject.toml and a uv.lock for you. The pip interface (uv pip ...) works on an environment and does not touch your project metadata.
| Task | Project interface | pip interface |
|---|---|---|
| Add a package | `uv add httpx` edits `pyproject.toml` and `uv.lock` | `uv pip install httpx` changes only the environment |
| Reproduce an environment | `uv sync --locked` | `uv pip sync requirements.txt` |
| Pin dependencies | `uv lock` writes `uv.lock` | `uv pip compile` writes `requirements.txt` |
| Create an environment | Automatic, in `.venv` | `uv venv` |
The pip interface is faster than pip because uv resolves and installs in parallel and caches downloads. The commands look familiar, but they are not identical to pip's. The differences are covered below.
Creating an environment
uv venv creates a virtual environment, by default in .venv. It does not install pip into it unless you ask for that with --seed.
uv venv
uv venv --python 3.12
uv venv --seedThe --python option asks uv to find that version, and uv downloads it if it is missing. You can activate the environment the usual way, with source .venv/bin/activate on Linux and macOS or .venv\Scripts\activate on Windows.
You do not have to activate it. uv pip looks for an environment in this order:
- The active virtual environment, from
VIRTUAL_ENV. - A
.venvdirectory in the current directory or a parent directory. - Failing both, it reports an error instead of changing the system Python.
That last point is a deliberate difference from pip, which installs into whatever interpreter it finds. To target a system interpreter on purpose, for example inside a container, pass --system.
Installing packages
The install commands mirror pip:
uv pip install httpx
uv pip install "httpx>=0.27" typer
uv pip install -r requirements.txt
uv pip install -e .
uv pip install --group dev
uv pip uninstall httpx
uv pip list
uv pip freeze
uv pip show httpxuv pip install -e . installs the current project in editable mode, so changes to the source take effect without reinstalling. uv pip install --group dev installs a dependency group from pyproject.toml. The --group option needs a reasonably recent uv, so check uv --version if it is rejected.
Two behaviours to remember:
uv pip installis additive. It does not remove packages that are no longer needed. Removing a requirement fromrequirements.txtand running install again leaves the old package in place.- uv does not install packages into your project's lockfile. Nothing you do with
uv pipchangesuv.lock.
Compiling requirements
uv pip compile takes loose requirements and resolves them into a fully pinned file. This replaces pip-tools' pip-compile. Write the direct dependencies in requirements.in:
httpx>=0.27
typer>=0.12Then compile:
uv pip compile requirements.in -o requirements.txtThe output lists every package, including transitive ones, with exact versions and comments showing which package required each. You can also compile straight from pyproject.toml:
uv pip compile pyproject.toml -o requirements.txt
uv pip compile pyproject.toml --group dev -o requirements-dev.txtUseful options:
--upgradeignores the versions in the existing output file and picks the newest allowed ones.--upgrade-package httpxupgrades only one package.--generate-hashesadds a hash for each file, so a later install can verify what it downloads.--python-version 3.12resolves for a target Python version other than the one running uv.--universalproduces one file that works across platforms, using environment markers where versions differ.
By default, compile resolves for the current platform. A file compiled on macOS may not suit a Linux server. Use --universal, or compile on the target platform.
Syncing an environment exactly
uv pip sync makes the environment match a compiled file. Unlike install, it removes anything not listed.
uv pip sync requirements.txtThis is the pip interface counterpart to uv sync --locked. Use sync for deployments and CI, and install for quick experiments. Note the naming: uv pip sync reads a requirements file, while uv sync reads uv.lock. They are different commands that share a verb.
Differences from pip
uv aims to be a drop-in replacement for common cases, but a few behaviours differ, and they cause most surprises during migration:
- Index handling. With several indexes, pip takes the best version across all of them. uv by default takes a package from the first index that has it, which protects against dependency confusion attacks. Change this with
--index-strategy. - Pre-releases. uv does not pick pre-release versions unless you ask for them with
--prerelease allow, or a requirement names one explicitly. - Bytecode. uv does not compile
.pycfiles at install time unless you pass--compile-bytecode. First imports may be slightly slower. - No environment by default. As noted, pip falls back to the system interpreter and uv does not.
- Unsupported options. Some rarely used pip options are missing. Check
uv pip install --helpbefore assuming one exists.
Using the pip interface in CI and Docker
A common step in an existing pipeline is replacing pip install -r requirements.txt with the uv version, which changes little but runs faster:
uv venv
uv pip sync requirements.txt
uv pip install -e . --no-depsHere --no-deps stops the editable install from resolving again, because the compiled file already holds everything. In a container with no virtual environment, add --system to each uv pip command.
When to move to the project interface
The pip interface suits teams that keep requirements.txt as their source of truth, or that need a gradual change. It has costs. You maintain requirements.in, the compiled file, and pyproject.toml separately, and nothing keeps them in step. The project interface records one set of dependencies and a lockfile that includes every platform. If you are starting new work, prefer uv add and uv sync. The next chapter covers moving an existing requirements.txt project over.
Exercise
A teammate removes typer from requirements.in, recompiles requirements.txt, and runs uv pip install -r requirements.txt on their existing environment. typer is still importable. Why, and which command gives an environment that matches the file exactly?
Show answer
uv pip install only adds or updates packages. It never removes packages that are missing from the file. uv pip sync requirements.txt makes the environment match the file, so it uninstalls typer (and any other package not listed). Recreating the environment with uv venv and installing again would also work.
Exercise
You compile requirements.txt on a macOS laptop, and the Linux CI job fails to install it because a platform-specific package is missing. What option helps when compiling, and what does it change?
Show answer
Use uv pip compile --universal. It resolves one file for all platforms and adds environment markers, so each platform installs the versions that apply to it. Without it, uv resolves only for the current platform.