uv is a next-generation tool for managing Python dependencies. Written in Rust by Astral, it is 10–100× faster than pip and replaces several tools at once: pip, venv, pyenv, and pip-tools.
Why Another Package Manager
Standard pip works, but it’s slow and imprecise. Installing Django takes 30–60 seconds — uv does it in 1–2 seconds. But speed isn’t the main point.
The main point is reproducibility. pip install without a lockfile can install different versions on different machines. uv generates uv.lock — a deterministic file with exact versions of all packages, including transitive dependencies. On any machine, uv sync produces an identical environment.
Installation
On Mac/Linux:
curl -LsSf https://astral.sh/uv/install.sh | sh
On Windows (PowerShell):
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
After installation, restart your terminal:
uv --version # uv 0.5.x
Creating a Project
uv init my-project # creates a directory with pyproject.toml and .venv
cd my-project
What gets created automatically:
- pyproject.toml — project description and dependencies
- .python-version — pinned Python version
- hello.py — starter file
- .venv/ — virtual environment
Adding Dependencies
uv add anthropic # add a dependency
uv add httpx rich environs # add several at once
uv add pytest --dev # dev dependency (not in prod)
uv remove requests # remove a dependency
Dependencies are automatically written to pyproject.toml and locked in uv.lock.
Running Code
uv run python main.py # run in the virtual environment
uv run pytest # run tests
uv run ruff check . # run any tool
uv run activates the correct environment automatically — no need to run source .venv/bin/activate.
Lockfile and Reproducibility
Commit uv.lock to Git. It guarantees that all team members and CI use identical versions.
uv sync # install exactly from the lockfile
uv sync --dev # including dev dependencies
Comparison with pip + venv
| Task | pip + venv | uv |
|---|---|---|
| Create an environment | python -m venv .venv + activate |
uv init |
| Add a package | pip install X + write to requirements.txt |
uv add X |
| Reproduce an environment | pip install -r requirements.txt (imprecise) |
uv sync (exact, from lockfile) |
| Speed | slow | 10–100× faster |
| Python version management | separate tool (pyenv) | built in |
pyproject.toml
The standard project description format (PEP 517/518). uv creates and updates it automatically:
[project]
name = "my-project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"anthropic>=0.40.0",
"rich>=13.0.0",
"environs>=11.0.0",
]
[tool.uv]
dev-dependencies = [
"pytest>=8.0.0",
"ruff>=0.8.0",
]
Tip: .python-version
The .python-version file pins the Python version for the project. uv reads it and downloads the required version automatically if needed — no pyenv required.
3.12
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!