Sub topic 5
Adding and removing dependencies
This chapter assumes you know what [project] and uv.lock are, and that you have a project created with uv init. It covers how uv add and uv remove change your project, and the errors you are most likely to hit.
What `uv add` does
uv add does three things in one step:
- It edits
pyproject.toml. - It re-resolves the dependency set and updates
uv.lock. - It installs the result into the project environment.
uv add httpxAfter this, [project].dependencies contains an entry such as "httpx>=0.27.2". By default uv records the version it resolved as a lower bound. It does not pin an exact version. The exact version lives in uv.lock.
Controlling version constraints
Pass a requirement string to set your own constraint. Quote it so the shell does not treat < or > as redirection.
uv add "httpx>=0.27,<1"
uv add "typer[all]"
uv add "tomli; python_version < '3.11'"The three lines show a version range, an extra, and an environment marker. These use the same syntax as any dependency in pyproject.toml.
Removing and upgrading
uv remove httpx
uv lock --upgrade-package httpx
uv lock --upgradeuv remove deletes the entry, re-locks, and removes packages that are no longer needed from the environment. uv lock --upgrade-package httpx moves one package to the newest version your constraints allow. uv lock --upgrade does this for every package. Neither command edits pyproject.toml. If you need a newer version than your constraint allows, change the constraint first.
Where the dependency goes
The flags decide which table receives the entry.
| Command | Entry is written to |
|---|---|
| `uv add httpx` | `[project].dependencies` |
| `uv add --dev pytest` | `[dependency-groups].dev` |
| `uv add --group lint ruff` | `[dependency-groups].lint` |
| `uv add --optional plot matplotlib` | `[project.optional-dependencies].plot` |
Use plain uv add only for packages your code needs at runtime. Everything else belongs in a group.
You can also import an existing list with uv add -r requirements.txt.
Common errors
- Editing by hand and forgetting to lock. If you type a dependency into
pyproject.toml,uv.lockis stale.uv runre-locks quietly, butuv sync --lockedfails with an error. Runuv lockafter manual edits. - Unquoted constraints.
uv add httpx>=0.27makes the shell redirect output to a file named=0.27. Add quotes. - Resolution failures. A message that the requirements are unsatisfiable usually means two constraints conflict, or a package does not support your
requires-pythonrange. Runuv treeto see which package asks for what, then loosen one constraint. - Removing something still imported.
uv removecannot see your source code. Search your imports before you remove a package.
Exercise
You ran uv add pytest and now pytest appears in your runtime dependencies. What command moves it to the development group, and which two commands should you run first?
Show answer
Run uv remove pytest, then uv add --dev pytest. The first command removes the runtime entry and re-locks. The second adds pytest to [dependency-groups].dev. Editing pyproject.toml by hand would also work, but you would then have to run uv lock.