Declaring and resolving extras in pyproject.toml, including self-referential groups
Each key under [project.optional-dependencies] becomes a Provides-Extra entry whose dependency array elements are gated by an environment marker, so pip only installs them when the user requests the bracket syntax. Self-referencing extras compose groups but require modern package managers.
On this page
Key idea
Extras in pyproject.toml are declared under [project.optional-dependencies] as a table of arrays. Each table key maps to a Provides-Extra metadata field, and each string in the array maps to a Requires-Dist entry. The build backend rewrites the environment marker on those Requires-Dist entries so that the dependency is only resolved when the installer receives the project-name[extra] syntax. Without that bracket request, the resolver treats the extra as absent and skips its dependencies entirely. Self-referencing extras let one group pull in other groups from the same project, for example all = ["myproject[gui, cli]"], producing a composite install from a single bracket request.
Declaring extras with optional-dependencies
The [project.optional-dependencies] table is a TOML table whose keys are extra names and whose values are arrays of dependency specifier strings. The specification states that the keys MUST be valid values for Provides-Extra, and each value in the array becomes a corresponding Requires-Dist entry for the matching Provides-Extra metadata. The optionality of these dependencies is recorded by modifying the environment marker clause on the related Requires-Dist entries to check the extra name. In other words, the build backend produces a Requires-Dist entry whose environment marker includes the extra name check, so the entry is present in the distribution metadata but only activates when the installer receives the project-name[extra] syntax.
In practice, a dependency specifier like "click>=8.0" inside the cli extra becomes a Requires-Dist line carrying an extra == "cli" marker. The resolver sees the marker and only activates the dependency when the user passes the bracket syntax during installation.
[project]
name = "myproject"
dependencies = ["requests>=2.0"]
[project.optional-dependencies]
gui = ["tkinter"]
cli = ["click>=8.0"]
all = ["myproject[gui, cli]"]
# Install only the CLI extra:
# pip install myproject[cli]
# Install both GUI and CLI via the composite extra:
# pip install myproject[all]Why extras are skipped unless requested
The specification is explicit: optional dependencies are only considered for installation if the associated extra name is requested. The mechanism is entirely in the environment marker that the build backend injects. When you run pip install myproject, the resolver evaluates every Requires-Dist entry. Entries carrying an extra == "gui" marker evaluate to false because no extra was requested, so pip skips them. When you run pip install myproject[gui], the resolver sets the extra context and those entries become active.
This is distinct from platform markers. A dependency with sys_platform == "win32" inside an extra has two conditions ANDed together: the extra must be requested AND the platform must match. If either fails, the dependency is not installed. Extras are resolved at install time, not at import time, so a missing optional dependency surfaces as an ImportError only when the code path that uses it is reached.
# At runtime, guard the import so the failure is explicit:
try:
import click
except ImportError:
raise ImportError(
"The 'cli' extra is required. "
"Install with: pip install myproject[cli]"
)Self-referencing extras to compose groups
The specification permits a dependency specifier inside an extra to reference other extras from the current project. The syntax is the bracket form applied to the project's own name: all = ["myproject[gui, cli]"]. When the user requests myproject[all], the resolver expands the self-reference, activates the gui and cli extras, and installs the union of their dependencies. This pattern lets a package author define a convenience bundle without duplicating individual dependency strings.
The specification documents self-referential extras as a supported pattern handled by package managers, noting that most modern managers including pip, uv, poetry, hatch, pdm and Pipenv support it. The spec does not explicitly describe the internal division of responsibility between the build backend and the resolver for expanding the nested bracket syntax. What the spec does establish is that the build backend writes the Requires-Dist entry with the extra marker, and that the self-reference is recognized and resolved during installation by the package manager.
# After pip install myproject[all], the resolved set includes:
# requests>=2.0 (core dependency)
# tkinter (from gui extra)
# click>=8.0 (from cli extra)
# The 'all' extra itself adds no new packages; it only triggers
# the gui and cli extras.Package-manager support and version caveats
The specification history records that self-referential extras were formally documented as a supported feature in August 2026. The current specification text separately lists pip, uv, poetry, hatch, pdm, and Pipenv as package managers that support the pattern. Older versions of these tools may not recognize the self-referencing syntax; behavior in that case depends on the tool, so consult the changelog of your package manager to confirm support. If your project targets a broad range of installer versions, avoid self-referencing extras and instead document the composite install command explicitly: pip install myproject[gui,cli].
Extra names must be valid Provides-Extra values. The specification requires that the keys under [project.optional-dependencies] conform to the Provides-Extra identifier constraints defined in the core metadata specification. For the exact character rules and normalization behavior governing valid extra names, consult the Provides-Extra section of the core metadata specification.
# Safe alternative for older tooling: document the composite request
# instead of relying on a self-referencing extra.
#
# pip install myproject[gui,cli]
#
# This works on every pip version that supports extras at all,
# because the resolver handles multiple bracket names natively.Applicability
- Does your target pip version support self-referential extras? Run pip --version; versions before the August 2026 documentation update may not resolve the nested bracket syntax.
- Do you have dependencies inside an extra that already carry platform markers? Verify that the combined marker logic matches your intent by installing on a non-matching platform and checking the resolved package list.
- Are your extra names valid Provides-Extra identifiers? The specification requires keys to conform to the Provides-Extra constraints defined in the core metadata specification; consult that specification for the exact character rules and normalization behavior.
- Does your project need to support installers older than 2026? If so, prefer explicit multi-extra bracket syntax in documentation over self-referencing extras.
Where this applies
Self-referencing extras are only guaranteed to resolve correctly on package managers that support the pattern. The specification documents support as of August 2026, but the exact minimum version per tool is not enumerated in the spec. Extras are resolved at install time; a missing optional dependency produces an ImportError only when the relevant code path executes. The spec describes the marker-modification mechanism but does not explicitly state the AND combination logic when a dependency inside an extra already carries its own environment marker; the combined behavior is the expected consequence of the mechanism rather than a directly stated rule. The example uses tkinter as a gui dependency for illustration; tkinter is a standard-library module and is not installable via pip, so a real project would reference a third-party GUI package instead.