Sub topic 5
Declaring dependencies and optional extras
This chapter assumes you have a working pyproject.toml with a [project] table, as built in the earlier chapters, and that you can create a virtual environment and run pip install. Here you will declare what your library needs at runtime and what only some users need.
Runtime dependencies
The dependencies key in [project] lists the packages pip installs whenever someone installs your library. Each entry is a requirement string: a package name plus an optional version constraint.
[project]
name = "greeter"
version = "0.1.0"
dependencies = [
"rich>=13",
"tomli>=2; python_version < '3.11'",
]The second entry uses an environment marker. The text after the semicolon limits the requirement to certain environments, here Python older than 3.11. Python 3.11 and later include tomllib in the standard library, so newer interpreters skip the install.
Choosing version constraints
A library is installed alongside other packages, so tight pins cause conflicts. If your library requires rich==13.7.0 and another library requires rich==13.8.0, pip cannot satisfy both.
| Constraint | Meaning | Typical use |
|---|---|---|
| `rich>=13` | Version 13 or newer | Default choice: state the oldest version you tested |
| `rich>=13,<15` | A range | You know a later major version breaks you |
| `rich~=13.4` | At least 13.4, below 14 | Compatible-release shorthand |
| `rich==13.7.0` | Exactly one version | Rarely right for a library |
Exact pins belong in applications, where you control the whole environment. For a library, set a lower bound you have tested and add an upper bound only when you know of a real incompatibility.
Optional extras
Some features need packages that most users do not want. Extras let users opt in. Declare them under [project.optional-dependencies], where each key is an extra's name.
[project.optional-dependencies]
test = ["pytest>=8"]
docs = ["sphinx>=7"]
color = ["rich>=13"]Users request an extra in square brackets:
pip install "greeter[test]"
pip install "greeter[test,docs]"For local development, combine an editable install with an extra. This installs your package from the source tree, so edits take effect without reinstalling:
python -m pip install -e ".[test]"Quote the argument. Shells such as zsh treat square brackets as glob patterns and fail with "no matches found".
flowchart TDKeeping extras honest
Core code must not import an extra's package at module level. If import rich sits at the top of greeter/__init__.py but rich is only in an extra, a plain install fails with ModuleNotFoundError. Import optional packages inside the function that uses them, and raise a clear error:
def colored(message: str) -> str:
try:
from rich.markup import escape
except ImportError as exc:
raise ImportError(
"Install the color extra: pip install 'greeter[color]'"
) from exc
return f"[green]{escape(message)}[/green]"Common errors
- Test tools in
dependencies. Every user then installs pytest. Put it in an extra. - Missing dependency. The code works on your machine because the package is installed there. Testing in a clean environment, covered in a later chapter, catches this.
- Mismatched names. The install name (
pip install pillow) can differ from the import name (import PIL). Declare the install name.
Exercise
Your library needs tomli only on Python versions below 3.11, and pytest only for running tests. Write the dependencies and optional-dependencies entries.
Show answer
dependencies = ["tomli>=2; python_version < '3.11'"] and, under [project.optional-dependencies], test = ["pytest"]. The marker skips tomli on newer Pythons, and the extra keeps pytest out of ordinary installs.