Optional Dependencies and Extras, Done Carefully
Every dependency you declare is installed for every user, whether or not they touch the feature that needs it. Making some of them optional keeps the default install lean, but it also creates a second, less-tested version of your package. This page compares the mechanisms each ecosystem offers and the discipline that makes them safe.
The mechanisms are not the same thing
| Mechanism | What it does | Typical use |
|---|---|---|
Python extras ([project.optional-dependencies]) | Named groups the user opts into with pip install pkg[name]. | A CLI, a plotting backend, async support, a database driver. |
npm optionalDependencies | Installed by default, but a failure to install does not fail the whole install. | Platform-specific native binaries with a pure-JS fallback. |
npm peerDependenciesMeta with optional: true | Declares a peer the host may provide; package managers do not warn if it is absent. | Adapters for one of several frameworks the host might use. |
| Cargo features | Compile-time flags that can enable optional dependencies. | Serialisation support, alternative async runtimes. |
Note the trap in the middle row: despite the name, npm's optionalDependencies are not opt-in. If you want "install only if the user asks", in JavaScript that usually means an optional peer dependency or a separate package.
A Python example (illustrative)
[project]
name = "report-kit"
dependencies = ["jinja2>=3.1"]
[project.optional-dependencies]
pdf = ["weasyprint>=60"]
cli = ["click>=8"]
all = ["report-kit[pdf,cli]"]
The self-referencing all extra is a common convenience. Keep group names short and stable: renaming an extra breaks every requirements.txt that mentions it, so treat it like renaming a public function.
Guarding the import
Code paths that use an optional dependency must fail clearly when it is missing. Import lazily, inside the function that needs it, and raise an error that tells the user exactly what to install:
def render_pdf(html):
try:
import weasyprint
except ImportError as exc:
raise ImportError(
"PDF output needs the 'pdf' extra: pip install report-kit[pdf]"
) from exc
return weasyprint.HTML(string=html).write_pdf()
The JavaScript equivalent is a dynamic import() inside a try block. In both languages, avoid top-level imports of optional packages; a single one turns "optional" into "required, with a worse error".
Test the minimal install, not just the full one
Most CI setups install everything, which means the path most users take is the one nobody tests. Add at least two jobs to your test matrix:
- Bare install: only required dependencies. Run the core tests and a test that asserts the friendly error appears when an optional feature is called.
- Full install: every extra or optional peer present, running the whole suite.
If you maintain many extras, a job per extra catches cases where one group quietly relies on another.
Versioning considerations
- Moving a dependency from required to optional is a breaking change for anyone who relied on it being present; release it in a major version or keep a compatibility extra.
- Adding a new extra is a minor change.
- Widening or narrowing the range inside an extra deserves a changelog line, since it changes what users' resolvers can pick.
Related: how peer dependencies work, and shipping a CLI, a frequent reason to reach for an extra.