# What are Optional Dependencies and Dependency Groups?

## Optional Dependencies: End-User Features

Optional dependencies, defined in [pyproject.toml](https://pydevtools.com/handbook/reference/pyproject.toml.md) under `[project.optional-dependencies]` (part of the [PEP 621](https://pydevtools.com/handbook/explanation/what-is-pep-621-compatibility.md) standard), are designed to:

* Package optional features that some users may want
* Allow users to install specific feature sets using the [`pip`](https://pydevtools.com/handbook/reference/pip.md) `install package[extra]` syntax
* Get published to PyPI along with the package

For example:

```toml
[project.optional-dependencies]
aws = [
    "boto3>=1.26.0",
    "s3fs>=2023.1.0",
]
visualization = [
    "matplotlib>=3.7.0",
    "seaborn>=0.12.0",
]
```

This allows users to choose the features they need:

```bash
pip install mypackage[aws]  # Only AWS-related features
pip install mypackage[visualization]  # Only visualization features
```

## Dependency Groups: Developer Tools

Dependency groups, defined under `[dependency-groups]`, organize development tools such as [`pytest`](https://pydevtools.com/handbook/reference/pytest.md), [`Ruff`](https://pydevtools.com/handbook/reference/ruff.md), and [`mypy`](https://pydevtools.com/handbook/reference/mypy.md):

* Managing development-time dependencies
* Organizing tools needed for different development tasks
* Keeping the published package lean

A typical example might look like:

```toml
[dependency-groups]
test = [
    "pytest>=7.0.0",
    "coverage>=7.0.0",
]
lint = [
    "ruff>=0.1.0",
    "mypy>=1.0.0",
]
```

These dependencies stay local to development and never get published to PyPI.

## Choose between groups and extras

Ask yourself: will a *user* of the package need this dependency, or only a *developer* working on the package? If the answer is "user," put it in `[project.optional-dependencies]`. If the answer is "developer," put it in `[dependency-groups]`.

Each optional dependency you expose becomes part of your public API, so keep the set small and stable.

## Further Reading

* [What is PEP 621?](https://pydevtools.com/handbook/explanation/what-is-pep-621-compatibility.md) (storing project metadata in pyproject.toml)
* [What is PEP 735?](https://pydevtools.com/handbook/explanation/what-is-pep-735.md) (dependency groups in pyproject.toml)
* [Understanding Dependency Groups in uv](https://pydevtools.com/handbook/explanation/understanding-dependency-groups-in-uv.md)
* [Python Packaging User Guide - Declaring Dependencies](https://packaging.python.org/en/latest/tutorials/managing-dependencies/)
