Skip to main content

optional_dependencies

Construct Checks for Optional Dependencies

Installation

PyPI version PyPI platforms

pip install optional_dependencies

Documentation

High-Level API

This package allows for easy construction of checks for optional dependencies. As every Python project can have its own unique set of optional dependencies, optional_dependencies provides the enum.Enum base class OptionalDependencyEnum for enumerating the optional dependencies.

A constructed OptionalDependencyEnum enum.Enum has members that are the package names with values that are either a packaging.Version or a NOT_INSTALLED sentinel value.

As an example:

# Note that `auto` is a convenience re-export of enum.auto
from optional_dependencies import OptionalDependencyEnum, auto


class OptDeps(OptionalDependencyEnum):
    PACKAGING = auto()
    THIS_IS_NOT_INSTALLED = auto()


OptDeps.PACKAGING
# <OptDeps.PACKAGING: <Version('...')>>

OptDeps.PACKAGING.installed
# True

OptDeps.PACKAGING.version
# <Version('...')>

OptDeps.THIS_IS_NOT_INSTALLED
# <OptDeps.THIS_IS_NOT_A_PACKAGE: <InstalledState.NOT_INSTALLED: False>>

enum.auto on a OptionalDependencyEnum passes version parsing to packaging.utils.canonicalize_name then importlib.metadata.version then packaging.version.parse. If the package cannot be found then it is considered InstalledState.NOT_INSTALLED

Every member stays its own member, whatever its value. This matters because those values collide readily: any two dependencies that are both missing share the NOT_INSTALLED sentinel, and distributions released together share a version number. A plain enum.Enum folds equal-valued members into aliases of whichever was declared first, which would make OptDeps.SECOND.installed report the first package's state.

class OptDeps(OptionalDependencyEnum):
    NOT_A_PACKAGE = auto()
    ALSO_NOT_A_PACKAGE = auto()


OptDeps.ALSO_NOT_A_PACKAGE.name
# 'ALSO_NOT_A_PACKAGE'

OptDeps.NOT_A_PACKAGE is OptDeps.ALSO_NOT_A_PACKAGE
# False

InstalledState.NOT_INSTALLED is an enum.Enum member that has a truthy value of False. This can be useful for boolean checks, as packaging.Version always has a truthy value of True.

if not OptDeps.THIS_IS_NOT_INSTALLED:
    print("NOT_INSTALLED has a truthy value of False")
# NOT_INSTALLED has a truthy value of False

if OptDeps.PACKAGING:  # truthy value of `True`
    print(OptDeps.PACKAGING)
# OptDeps.PACKAGING

Low-Level API

Sometimes the high-level API is insufficient to determine whether an optional dependency is present. For example, this can sometimes happen with compiled packages, where the package appears to be installed, but something is wrong. In these cases you can customize the enum members using the low-level API.

The low-level functions are:

  • optional_dependencies.utils.is_installed(pkg_name: str, /) -> bool: a regularized form of importlib.util.find_spec.

  • optional_dependencies.utils.get_version(pkg_name: str, /) -> Version | Literal[InstalledState.NOT_INSTALLED]: for getting the packaging.Version of a package if it is installed, or returning NOT_INSTALLED otherwise.

  • optional_dependencies.utils.chain_checks(version: Version, /, *checks: bool): for chaining checks together and ensuring the returned value is still a packaging.Version or NOT_INSTALLED.

As a pseudo-code example of a package with c-compiled code that :

from optional_dependencies.utils import is_installed, get_version, chain_checks

# A subpackage needs to be checked.
chain_checks(get_version("package1"), is_installed("package1.subpackage"))
# <Version('...')>

# This package is not installed correctly
chain_checks(get_version("package2"), is_installed("package2.subpackage"))
# <InstalledState.NOT_INSTALLED: False>

The low-level API can be used with OptionalDependencyEnum

class OptDeps(OptionalDependencyEnum):
    PACKAGING = auto()
    THIS_IS_NOT_INSTALLED = chain_checks(
        get_version("package2"), is_installed("package2.subpackage")
    )

Citation

DOI

If you found this library to be useful and want to support the development and maintenance of lower-level code libraries for the scientific community, please consider citing this work.

Development

codecov Actions Status

We welcome contributions!

Release files for optional-dependencies 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for optional-dependencies 0.5.0
File Size Uploaded
optional_dependencies-0.5.0.tar.gz 80.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for optional-dependencies 0.5.0
File Interpreter ABI Platform
optional_dependencies-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 90.6 kB

Release files / optional_dependencies-0.5.0.tar.gz

Download URL optional_dependencies-0.5.0.tar.gz
Size 80.5 kB
Tags Source
SHA-256 checksum
How to use checksums
05edd224eaf1b7531a6e2ead85de2698dbc2f5edba445533dd7722f160602251
BLAKE2b-256 checksum
How to use checksums
cd8967e3cbc57a0869231b589b39f8606c98bba81b91f3b72bd56be99040aa78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 7, 2026.

Transparency log

Release files / optional_dependencies-0.5.0-py3-none-any.whl

Download URL optional_dependencies-0.5.0-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
320eb33710e5a38efc536141277c3d0da48a093aecfb15e3a7dc9491669c0821
BLAKE2b-256 checksum
How to use checksums
ba81aea272ab6faa04addb1045d54dece562a56e13200dd200d0cf71b034f7c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 7, 2026.

Transparency log
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page