Sub topic 2
Where uv fits: one tool for projects, Python versions, and tools
This chapter assumes you can write basic Python, work in a terminal, and import packages. It also assumes you know roughly what pip and virtual environments do. The goal is to place uv among the tools you already use, so that later chapters on pyproject.toml, lockfiles, and workspaces have a map to hang on.
The problem uv addresses
A typical Python workflow uses several separate tools:
- A version manager such as pyenv to install interpreters.
python -m venvor virtualenv to create environments.- pip to install packages.
- pip-tools, Poetry, or PDM to resolve and lock dependencies.
- pipx to install command-line tools in isolation.
- build and twine to create and upload distributions.
Each tool has its own configuration, its own cache, and its own failure modes. Keeping them consistent is manual work. For example, the interpreter pyenv selects must match the one the virtual environment was built from, and the environment must match what the lockfile assumed.
uv is a single command-line program, written in Rust by Astral, that covers all of these jobs. It reads the standard pyproject.toml file and keeps a lockfile, uv.lock, next to it.
What uv replaces
| Job | Traditional tool | uv command |
|---|---|---|
| Install an interpreter | pyenv | `uv python install` |
| Create an environment | `python -m venv` | `uv venv`, or automatic via `uv run` and `uv sync` |
| Install packages ad hoc | pip | `uv pip install` |
| Declare and lock project dependencies | Poetry, pip-tools | `uv add`, `uv lock`, `uv sync` |
| Run a command in the environment | `source .venv/bin/activate` | `uv run` |
| Run a tool in isolation | pipx | `uvx`, `uv tool install` |
| Build distributions | build | `uv build` |
| Upload to an index | twine | `uv publish` |
The uv pip interface is a fast, mostly compatible reimplementation of pip and pip-tools commands. It exists so existing workflows can adopt uv without changing how they think about projects. The project commands (uv add, uv sync, uv run) are the newer, higher-level workflow. A later chapter covers uv pip in detail.
The three areas uv manages
Projects
A project is a directory with a pyproject.toml. uv treats that file as the source of truth for what the project needs. When you run uv add httpx, uv edits pyproject.toml, resolves a compatible set of versions, writes them to uv.lock, and installs them into a project environment in .venv.
You normally never activate that environment. uv run makes sure the environment exists and matches the lockfile, then runs your command inside it.
Python versions
uv can find interpreters already on your machine and can download its own. It uses prebuilt builds from the python-build-standalone project, so no compilation happens. If your project's requires-python needs a version you do not have, uv fetches one on demand. You can also install one explicitly:
uv python list
uv python install 3.12
uv python pin 3.12uv python pin writes a .python-version file. The requires-python field in pyproject.toml states which versions your code supports, while .python-version states which one you develop with. Both are covered in a later chapter.
Tools and scripts
Some Python programs are tools you run, not libraries you import: ruff, black, httpie, cookiecutter. uvx ruff check . runs a tool in a temporary, cached environment without installing it into your project. uv tool install ruff installs it persistently, with its own isolated environment, and puts its executable on your PATH. Single-file scripts can declare their own dependencies inline, and uv run script.py handles the environment.
Why it is fast
uv's speed comes from a few concrete design choices:
- It is a compiled Rust program, with no Python startup cost and no dependency on a working Python to run.
- It uses a global cache. A package downloaded once is reused by every environment.
- It links files from the cache into environments (hard links or copy-on-write where the filesystem supports it) instead of copying them each time.
- It downloads and installs in parallel, and its resolver reads package metadata efficiently.
The practical result is that creating a fresh environment for a project with dozens of dependencies often takes seconds, and a warm cache makes it faster. This changes how you work. Throwaway environments become cheap, so there is less temptation to reuse a polluted one.
A first look at the workflow
This sequence touches all three areas. Run it in an empty directory:
uv init demo
cd demo
uv python pin 3.12
uv add httpx
uv run python -c "import httpx; print(httpx.__version__)"
uvx ruff --versionWhat happens at each step:
uv init democreates a directory with apyproject.tomland a starter file.uv python pin 3.12records the interpreter version, downloading it if needed.uv add httpxadds the dependency, creates.venv, and writesuv.lock.uv runexecutes inside.venv. The import works because uv installed httpx there.uvx ruff --versionruns ruff from a separate cached environment. Ruff is not a dependency ofdemo.
Common errors and misconceptions
Using uv pip install and expecting pyproject.toml to change. The uv pip commands install into an environment but do not edit your project metadata or lockfile. If you want the dependency recorded, use uv add. Mixing the two leaves an environment that uv sync may later change, because uv sync makes the environment match the lockfile and can remove packages the lockfile does not list.
Editing .venv by hand. Treat .venv as generated output. Do not commit it, and do not rely on packages you installed manually. The source of truth is pyproject.toml plus uv.lock.
Running uv add outside a project. If there is no pyproject.toml in the current directory or a parent, uv reports that it cannot find a project. Run uv init first, or change into the project directory.
Confusing uvx with uv run. Use uvx for a tool that is independent of your project, such as a formatter you want at a fixed version. Use uv run for anything that must import your project's code or dependencies, such as your tests. A tool launched with uvx cannot see your project's installed packages.
Not committing uv.lock. For an application or any project where teammates and CI should get identical versions, commit the lockfile. Without it, each machine resolves independently and can end up with different versions.
Expecting uv to replace your build backend. uv manages environments and dependencies, and uv build invokes the backend declared in [build-system]. The backend, such as hatchling, still decides how your package is built. This is covered in the build chapter.
Decision criteria
- Starting a new application or library: use the project workflow (
uv init,uv add,uv run). - Keeping an existing
requirements.txtpipeline for now: useuv pipas a faster drop-in, and migrate later. - Needing a command-line tool once: use
uvx. - Needing a tool available every day on your
PATH: useuv tool install. - Writing a one-off script: use inline metadata and
uv run script.py.
Exercise
You run uv pip install requests in a uv project, and your script works. A teammate clones the repository and gets ModuleNotFoundError: No module named 'requests'. What went wrong, and which command should you have used?
Show answer
uv pip install installed requests into your local environment but did not record it in pyproject.toml or uv.lock. Your teammate's environment is built from those files, so it lacks the package. You should have run uv add requests, which records the dependency, updates the lockfile, and installs it.
Exercise
You want to check your code with ruff at a pinned version, but ruff is not something your project imports. Should you use uv run ruff or uvx ruff? Which would you choose for pytest, and why?
Show answer
Use uvx ruff (or add ruff as a development dependency), because ruff does not need your project's packages. For pytest, use uv run pytest, because tests import your project's code and dependencies, and only the project environment has them.