Sub topic 5
Adding and removing dependencies
This chapter assumes you have a project created with uv init and know what the [project] table in pyproject.toml holds. It also assumes you know what a virtual environment is. Here you will learn what uv add and uv remove do to your files and environment, how to control version constraints, and which errors to expect.
What `uv add` does
A dependency is declared in pyproject.toml, resolved into uv.lock, and installed into the project environment. uv add performs all three steps in one command.
uv add httpxAfter this command, pyproject.toml contains a new entry:
dependencies = [
"httpx>=0.27.2",
]The version in the entry is a lower bound taken from the version uv resolved. The exact version, with hashes, is recorded in uv.lock. The environment in .venv is then updated to match.
If resolution fails, uv reports the error and leaves pyproject.toml unchanged. You never end up with a declared dependency that cannot be installed.
Controlling version constraints
By default you get a lower bound. You can give uv any constraint written in the standard dependency specifier syntax. Quote the argument so your shell does not treat < or > as redirection.
uv add "httpx>=0.27,<1"
uv add "typer==0.12.5"
uv add "httpx[http2]"
uv add "tomli; python_version < '3.11'"The four commands show a range, an exact pin, an extra, and an environment marker.
- A range allows upgrades within the limits you set.
- An exact pin (
==) blocks all upgrades, so use it sparingly. Libraries that pin exactly cause conflicts for their users. - An extra such as
[http2]installs the optional dependencies the package defines for that feature. - A marker limits a dependency to certain Python versions or platforms.
For an application, loose ranges in pyproject.toml are fine, because uv.lock already fixes the exact versions. For a library, keep ranges as wide as you have tested, because your users' resolvers must combine your constraints with everyone else's.
Removing dependencies
uv remove httpxThis deletes the entry from pyproject.toml, re-resolves, and syncs the environment. Packages that only httpx needed are dropped from the lock and uninstalled. You only name direct dependencies. You never list or remove transitive ones yourself.
To see why a package is installed, print the tree:
uv tree
uv tree --invert --package h11The second command shows which packages depend on h11. This helps when a package you do not recognize appears in the lock.
Choosing where the dependency goes
The same command can write to different tables, depending on a flag.
| Command | Table written | Typical use |
|---|---|---|
| `uv add httpx` | `[project] dependencies` | Needed at runtime |
| `uv add --dev pytest` | `[dependency-groups] dev` | Tests and local tooling |
| `uv add --group lint ruff` | `[dependency-groups] lint` | A named group |
| `uv add --optional http2 h2` | `[project.optional-dependencies]` | An extra your users can choose |
The key difference is who sees the dependency. Runtime dependencies and optional dependencies are published in the package metadata. Dependency groups are not published, so people who install your package never get them. The next chapter covers groups in detail.
Dependencies from other sources
A plain name is looked up on the default package index. When you give a git URL or a local path, uv records it under [tool.uv.sources] and keeps the name in dependencies.
uv add "git+https://github.com/encode/httpx"
uv add ./packages/core-libThe first command pins a git dependency. The second adds a local package. uv writes the source entry for you. A later chapter covers these sources in detail.
Adding many at once and importing a requirements file
You can list several packages in one command:
uv add httpx typer richIf you already have a requirements file, import it:
uv add -r requirements.txtThis adds each requirement as a direct dependency. It is a useful first step when migrating, but it also promotes every pinned transitive package in the file to a direct dependency. Review the result and remove the entries you did not choose yourself.
Upgrading
uv add does not upgrade a package that is already satisfied. To move to newer versions, ask the lock to do so:
uv lock --upgrade-package httpx
uv lock --upgrade
uv syncThe first command upgrades one package and leaves the rest of the lock alone. The second re-resolves everything to the latest allowed versions. Neither can go beyond the constraints in pyproject.toml. To exceed an upper bound, edit the constraint first or run uv add "httpx>=0.28".
Editing `pyproject.toml` by hand
The command line is a convenience. You can edit the dependencies list directly. The next uv run, uv sync, or uv lock notices that the file changed and re-resolves. If you want to avoid that, use uv sync --locked, which fails when the lock is out of date instead of updating it.
Two flags control side effects of uv add and uv remove:
--no-syncupdatespyproject.tomlanduv.lockbut leaves.venvuntouched.--frozenskips locking and usesuv.lockas it is. The lock will not match the new dependency, so use it with care.
Common errors
No solution found. Two of your dependencies need incompatible versions of a third. The message lists the chain of constraints. Loosen one of your own constraints, or check requires-python. A package that does not support your declared Python range can make resolution fail even if it works on your current interpreter.
Package not found. The name is misspelled, or the package lives on a private index. Names are matched case-insensitively and -, _, and . are treated as equivalent, so Typer and typer are the same package.
Shell redirection. uv add httpx>=0.27 without quotes makes the shell write output to a file named =0.27. Always quote constraints.
Wrong table. A tool such as pytest added without --dev ships to every user of your package. Move it with uv remove pytest followed by uv add --dev pytest.
Import works locally but fails elsewhere. You installed the package with uv pip install or plain pip into .venv, but it is not declared in pyproject.toml. Declare it with uv add.
Exercise
You run uv add pytest in a library project, and later realize that pytest should be a development-only tool. Which commands fix this, and why does the difference matter for people who install your library?
Show answer
Run uv remove pytest and then uv add --dev pytest. The first removes it from [project] dependencies, and the second adds it to the dev group under [dependency-groups]. Entries in dependencies are published in the package metadata, so every user of your library would install pytest. Dependency groups are not published.
Exercise
A teammate wrote uv add httpx>=0.27 and found a file named =0.27 in the project directory. What happened, and what is the correct command?
Show answer
The shell treated > as output redirection, so it ran uv add httpx and wrote the command's output to a file named =0.27. The correct command quotes the constraint: uv add "httpx>=0.27".