It is Friday afternoon. A teammate pings you: "The script works on my laptop, why does it crash on yours?" You compare versions, and the story unfolds the way it always does. Different Python, a library one minor version apart, a package that was installed globally two years ago and forgotten. Nobody did anything wrong. The project simply never wrote down what it needed.
This is the problem Python's packaging tools try to solve, and for years the answer was a pile of separate tools: pip, venv, pip-tools, pyenv, virtualenv. A newer tool called uv folds most of that pile into one fast command. This guide walks through the everyday workflow, why it works, and where to be careful.
The real problem: an undocumented environment
A Python script is only half the program. The other half is the environment around it: the interpreter version and every installed package. If that half lives only in your head, or in a folder you built by typing pip install a dozen times, it cannot be reproduced.
Two ideas fix this. First, isolation: each project gets its own environment, so one project's upgrades cannot break another. Second, declaration plus locking: you declare what you directly depend on, and a tool records the exact versions it resolved, including dependencies of dependencies.
A project you cannot rebuild from scratch is a project you do not fully own.
The classic way to get there is python -m venv .venv, then pip install, then pip freeze > requirements.txt. It works, but the freeze file mixes your direct choices with everything pulled in transitively, and nothing ties it to a Python version. Updating means hand-editing and hoping.
What uv is, in plain terms
According to [Astral's documentation](https://docs.astral.sh/uv/guides/projects/), uv is a package and project manager for Python, written in Rust. It reads a standard pyproject.toml, creates a .venv for you, resolves dependencies, and writes a cross-platform lockfile named uv.lock. It can also install and switch Python versions itself.
The practical result is that the "create environment, install, freeze" ritual becomes a few commands, and installs are usually much faster than with plain pip. Speed matters less than it sounds on day one, but it adds up in CI pipelines that rebuild environments on every push.
A five-minute walkthrough
Install uv following the official instructions for your OS (on Linux, the standalone installer or your package manager both work). Then start a project:
uv init weather-report
cd weather-report
uv add requestsuv init creates a pyproject.toml and a starter script. uv add requests does three things at once: records requests under dependencies, resolves compatible versions, and installs them into .venv. It also creates or updates uv.lock.
Your pyproject.toml will look roughly like this (version numbers will differ):
[project]
name = "weather-report"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"requests>=2.32",
]Now write a small script that uses it:
# main.py
import requests
def fetch_status(url: str) -> int:
response = requests.get(url, timeout=10)
return response.status_code
if __name__ == "__main__":
print(fetch_status("https://example.com"))Run it with:
uv run main.pyuv run checks that the lockfile and environment are in sync, fixes them if needed, then runs your command inside .venv. You never have to remember to "activate" anything. That small convenience removes a surprisingly common source of "I installed it but it says not found" confusion.
Direct dependencies versus the lockfile
Here is the part newcomers often miss. pyproject.toml says what you asked for (loose ranges like requests>=2.32). uv.lock says what you got (exact versions and hashes for the whole tree). Commit both to version control.
When a teammate clones the repository, one command rebuilds the same environment:
uv syncIn CI or on a server, you can be stricter. The uv docs describe flags such as --locked, which fails if the lockfile is out of date, and --frozen, which installs from the lockfile without checking. Using --locked in CI is a cheap way to catch "someone changed dependencies but forgot to commit the lock".
| File | Who edits it | Purpose |
|---|---|---|
pyproject.toml | You (or uv add) | What the project needs |
uv.lock | uv only | Exact versions, reproducible |
.venv/ | Nobody | Disposable, never commit it |
Treat .venv as disposable. If it ever feels haunted, delete it and run uv sync. That is the whole point of declaring things properly.
Development tools without polluting production
Not every package belongs in production. Test runners and linters are for developers. uv supports dependency groups for this:
uv add --dev pytest ruff
uv run pytestThe --dev flag puts them in a development group, so a production install can skip them. Keeping groups tidy also makes container images smaller and reduces what you have to patch when a security advisory lands.
To remove something you no longer use, run uv remove requests. The manifest, lockfile and environment all update together, which beats the old habit of uninstalling a package and forgetting to edit requirements.txt.
Where to be careful
A tool this convenient still has edges. A few worth knowing:
- Existing projects: if you already have a
requirements.txt, you can migrate gradually. uv offers a pip-compatible interface (uv pip install -r requirements.txt) so you do not have to switch everything in one afternoon. - Team agreement: a lockfile only helps if everyone uses the workflow. Decide once, write it in the README, and enforce it in CI.
- Python versions:
requires-pythonis a promise to other developers. Set it deliberately instead of accepting whatever your laptop happens to run. - Moving target: tooling in this area changes quickly. Check the official docs for current flags before copying commands into automation, as the details here are accurate as of writing.
None of these are reasons to avoid uv. They are reasons to adopt it with intention rather than by accident.
Summary
Python environments go wrong when they live in someone's memory instead of in the repository. A declared dependency list plus a lockfile turns "works on my machine" into "works on any machine that runs one command". uv makes that workflow short: uv init, uv add, uv run, uv sync, and commit both pyproject.toml and uv.lock.
Try it on a small side project this weekend. Once you have rebuilt an environment from scratch in ten seconds, the next Friday afternoon ping will feel a lot calmer.
Comments 0