TExcelTAD
Home › Python Packaging: pip, venv, Poetry & uv › Dependency Groups & Extras

Dependency Groups & Extras

Not every dependency is needed everywhere. pytest is needed by developers and CI but not by users; a documentation generator only by the docs job; rich maybe only by people who want the fancy CLI output. Python has two separate mechanisms for this, and mixing them up is a common mistake:

Dependency groups Extras (optional dependencies)
For the project's developers: tests, linting, docs the package's users: optional features
Published in the wheel's metadata? no yes (Provides-Extra)
Installed by users with can't be pip install greet[fancy]
Declared in [dependency-groups] (PEP 735) or [tool.poetry.group.<name>.dependencies] [project.optional-dependencies]
Poetry command poetry add --group test pytest poetry add --optional fancy rich

Groups

bash · try it yourself
poetry new -q greet && cd greet
poetry source add -q --priority=primary local http://127.0.0.1:8080/simple/
poetry add -q attrs
poetry add --group test pytest
poetry add -q --group docs click
echo "--- pyproject.toml"
sed -n '/^dependencies/,/^]/p;/^\[dependency-groups\]/,$p' pyproject.toml
Using version ^9.1.1 for pytest

Updating dependencies
Resolving dependencies...

Package operations: 5 installs, 0 updates, 0 removals

  - Installing iniconfig (2.3.1)
  - Installing packaging (26.3)
  - Installing pluggy (1.7.0)
  - Installing pygments (2.21.0)
  - Installing pytest (9.1.1)

Writing lock file
--- pyproject.toml
dependencies = [
    "attrs (>=26.1.0,<27.0.0)"
]
[dependency-groups]
test = [
    "pytest (>=9.1.1,<10.0.0)"
]
docs = [
    "click (>=8.5.0,<9.0.0)"
]

Poetry 2.5 writes groups to the standard [dependency-groups] table (PEP 735), which uv and pip (pip install --group test) understand too. Older Poetry projects use [tool.poetry.group.test.dependencies]; both work.

All groups are locked together: there is one poetry.lock with one consistent resolution, so the pytest you test with sees the same attrs the app uses. What you choose at install time is only which parts get installed:

bash · try it yourself
poetry new -q greet && cd greet
poetry source add -q --priority=primary local http://127.0.0.1:8080/simple/
poetry add -q attrs
poetry add -q --group test pytest
poetry add -q --group docs click
echo "=== only runtime dependencies (a production image)"
poetry sync --only main 2>&1 | grep -E 'Removing|Installing'
echo "=== everything except docs (CI test job)"
poetry sync --without docs 2>&1 | grep -E 'Removing|Installing'
=== only runtime dependencies (a production image)
Installing dependencies from lock file
  - Removing click (8.5.0)
  - Removing iniconfig (2.3.1)
  - Removing packaging (26.3)
  - Removing pluggy (1.7.0)
  - Removing pygments (2.21.0)
  - Removing pytest (9.1.1)
Installing the current project: greet (0.1.0)
=== everything except docs (CI test job)
Installing dependencies from lock file
  - Installing iniconfig (2.3.1)
  - Installing packaging (26.3)
  - Installing pluggy (1.7.0)
  - Installing pygments (2.21.0)
  - Installing pytest (9.1.1)
Installing the current project: greet (0.1.0)

poetry install only adds packages; poetry sync (Poetry 2's spelling of the old install --sync) also removes whatever the selection doesn't include, so the environment matches exactly. Use sync in CI and Docker images.

Tip: Mark a group optional ([tool.poetry.group.docs] optional = true) and it's skipped unless you ask for it with --with docs. Good for heavy, rarely used groups.

Extras

Extras are part of the package your users install. poetry add --optional creates one:

bash · try it yourself
poetry new -q greet && cd greet
poetry source add -q --priority=primary local http://127.0.0.1:8080/simple/
poetry add -q attrs
poetry add -q --optional fancy "rich>=13"
sed -n '/^\[project.optional-dependencies\]/,/^fancy/p' pyproject.toml
poetry build -q -f wheel
unzip -p dist/greet-0.1.0-py3-none-any.whl 'greet-0.1.0.dist-info/METADATA' | grep -E '^(Requires-Dist|Provides-Extra)'
echo "--- installing the extra with pip"
python3 -m venv /tmp/user && /tmp/user/bin/pip install -q "dist/greet-0.1.0-py3-none-any.whl[fancy]"
/tmp/user/bin/pip list --format=freeze | grep -iE 'attrs|rich|greet'
[project.optional-dependencies]
fancy = ["rich (>=13)"]
Provides-Extra: fancy
Requires-Dist: attrs (>=26.1.0,<27.0.0)
Requires-Dist: rich (>=13) ; extra == "fancy"
--- installing the extra with pip
attrs==26.1.0
greet==0.1.0
rich==15.0.0

The wheel's metadata says rich is only required extra == "fancy". A user who runs pip install greet gets attrs only; greet[fancy] adds rich. In your code, guard the optional import:

python
try:
    from rich import print
except ImportError:          # the "fancy" extra isn't installed
    pass

Inside the project, poetry install --extras fancy (or --all-extras) installs extras into the dev environment.

Test yourself

You want pytest for your test suite. Group or extra?

  1. An extra, so users can run tests
  2. A dependency group: it's for developers and must not appear in the published metadata
  3. A main dependency
  4. [build-system] requires

What's the difference between poetry install --only main and poetry sync --only main?

  1. None
  2. sync also removes installed packages that aren't in the selection, so the environment matches exactly
  3. sync updates the lockfile
  4. install is faster

How does a user install the fancy extra of greet?

  1. pip install greet --extra fancy
  2. pip install "greet[fancy]"
  3. pip install greet-fancy
  4. poetry add greet --group fancy