Sub topic 6
Development dependencies and dependency groups
Every project has packages that the code needs to run and packages that only people working on the code need. Examples of the second kind are test runners, linters, and documentation builders. This chapter shows how pyproject.toml separates the two with dependency groups, and how uv installs, locks, and updates them.
It assumes you can already create a project with uv init, add runtime dependencies with uv add, and read a basic pyproject.toml.
Why separate development dependencies
Suppose weather-cli uses httpx and typer at runtime. You also use pytest to test it. If pytest sits in the runtime dependencies list, every user who installs your package also installs pytest. That adds download time and gives them more packages that can conflict with their own.
A dependency group is a named list of requirements that belongs to the project but is not part of its published metadata. Groups are defined by the standard [dependency-groups] table (PEP 735), so other tools can read them too.
The dev group
The command you have already seen creates the most common group:
uv add --dev pytestuv writes the requirement into a group named dev:
[dependency-groups]
dev = ["pytest>=8"]--dev is shorthand for --group dev. You can name any other group the same way:
uv add --group lint ruff
uv add --group docs mkdocsThe result is one table with several groups:
[dependency-groups]
dev = ["pytest>=8"]
lint = ["ruff>=0.6"]
docs = ["mkdocs>=1.6"]To remove a requirement, name the group again, because uv remove ruff alone looks only in the runtime dependencies and fails:
uv remove --group lint ruffWhich groups get installed
uv decides which groups to install each time you run uv sync or uv run. By default it installs the project's runtime dependencies plus the dev group. You change that with flags:
| Flag | Effect |
|---|---|
| `--no-dev` | Skip the `dev` group |
| `--group docs` | Also install the `docs` group |
| `--no-group docs` | Leave out `docs`, even if it is a default |
| `--only-group lint` | Install only `lint`, not the project or its runtime dependencies |
| `--all-groups` | Install every group |
The same flags work on uv run, which syncs the environment before it runs your command:
uv run --group docs mkdocs build
uv run --no-dev python -m weather_cliuv sync --only-group lint is useful in a CI job that only runs a linter. The job skips installing the project and every runtime dependency.
Changing the defaults
The set of groups installed by default is a uv setting, not part of the standard. It lives in [tool.uv]:
[tool.uv]
default-groups = ["dev", "lint"]With this setting, a plain uv sync installs dev and lint but not docs. You can also write default-groups = "all" to install every group by default. Remember that default-groups only changes what happens when you pass no flags. --no-group and --no-dev still take precedence.
Composing groups with include-group
A group can contain other groups. This avoids copying requirements between lists:
[dependency-groups]
test = ["pytest>=8", "pytest-cov>=5"]
lint = ["ruff>=0.6"]
dev = [
{include-group = "test"},
{include-group = "lint"},
]Installing dev now installs everything in test and lint. A CI job that only needs tests can run uv sync --only-group test, while developers get the full set from the default dev group. Including a group that does not exist is an error, so check the spelling.
Groups versus optional dependencies
pyproject.toml has a second place for extra requirements: [project.optional-dependencies], also called extras. The two look similar but have different purposes.
| Row | Dependency groups | Optional dependencies (extras) |
|---|---|---|
| Table | `[dependency-groups]` | `[project.optional-dependencies]` |
| Published in the wheel metadata | No | Yes |
| Users can install them | No | Yes, with `pip install "pkg[extra]"` |
| Typical use | Tests, linting, docs | Optional features such as `pkg[postgres]` |
| uv flag | `--group` | `--extra` |
Use a group when only contributors need the packages. Use an extra when users of your package may want the packages to unlock a feature. If you are unsure, ask whether someone who runs pip install weather-cli could ever want the package. If not, it belongs in a group.
Groups and the lockfile
uv lock resolves the runtime dependencies and all groups together and records them in one uv.lock. Two consequences follow:
- Switching groups with
--groupor--no-devnever changes the lockfile. It only changes which locked packages are installed. - A shared package gets one version everywhere. If
pytestneedspluggy>=1.5and your docs tools needpluggy<1.5, resolution fails instead of giving each group its own version.
For the second case you can declare that two groups never install together:
[tool.uv]
conflicts = [
[
{ group = "docs" },
{ group = "legacy-tests" },
],
]uv then resolves each side separately. Use this sparingly, because it makes the lockfile larger and you can no longer sync both groups at once.
To upgrade only a development tool, ask for a targeted upgrade:
uv lock --upgrade-package ruff
uv syncCommon errors
- A tool is missing after
uv sync --no-dev.--no-devremoves thedevgroup, includingpytest. Runuv syncwithout the flag, or add the group you need. uv remove pytestreports that the package is not a dependency. Add--devor--group dev.- A group in
default-groupsis not found. The name must match a key in[dependency-groups]. uv treatsDocsanddocsas the same name, but a misspelling is an error. - Old
dev-dependenciesunder[tool.uv]. Earlier uv versions stored development dependencies in[tool.uv] dev-dependencies. That key still works but is the legacy form. Move its entries into[dependency-groups] devwhen you touch the file. - Groups missing from a built wheel. This is expected.
uv builddoes not include groups, so tell consumers to use extras if they need an optional set.
Exercise
Your project has a lint group containing ruff, and a CI job that should run only ruff check . without installing the project or any of its runtime dependencies. Which command installs the right packages, and why does the lockfile stay unchanged?
Show answer
Run uv sync --only-group lint. The --only-group flag installs just the packages in lint and skips the project and its runtime dependencies. The lockfile stays unchanged because uv.lock already records every group's resolution, and the flag only selects which locked packages to install.
Exercise
You want a plain uv sync to install dev and docs, and you want dev to include everything from a test group. Write the relevant pyproject.toml tables.
Show answer
Define the groups and set the defaults: [dependency-groups] test = ["pytest>=8"] docs = ["mkdocs>=1.6"] dev = [{include-group = "test"}] [tool.uv] default-groups = ["dev", "docs"]