Skip to main content

optional_requirements

Manage optional requirements and their versions at runtime.

Examples

Managing requirements specified in a file

If you have a requirements file in your package (it must be included when installing the package), you can it to manage optional dependencies.

The find_requirements_file function will start at the given path and search for a file matching the given name, climbing up the directory tree until it finds a match or it goes out of a Python package (i.e., it no longer sees an __init__.py file.).

from pathlib import Path

from optional_requirements import (
    Requirements,
    parse_requirements_file,
    find_requirements_file,
    IncompatibleRequirementError,
    MissingRequirementError
)

r = Requirements(requirements=parse_requirements_file(
    find_requirements_file(
        start=Path(__file__),
        name="optional-requirements.txt",
    )
))

try:
    pd = r.try_import(name="pandas", reason="This is required to read a CSV file.")
except IncompatibleRequirementError as e:
    # The wrong version is installed.
    print(e.current_version)
    print(e.required_version)
    pass
except MissingRequirementError as e:
    # The requirement is not installed.
    print(e.required_version)
    pass
else:
    # Do something with the imported dependency.
    df = pd.DataFrame()
    pass

Managing requirements as a group in code

from optional_requirements import Requirements
from packaging.requirements import Requirement

r = Requirements(requirements=[Requirement("pandas==2.*"), Requirement("numpy<2")])
r.try_import(name="pandas", reason="This is required to read a CSV file.")

Managing requirements one by one in code

from optional_requirements import try_import_requirement
from packaging.requirements import Requirement

try_import_requirement(requirement=Requirement("numpy<2"), reason="Required for quick maths.")

Metadata

Release files for optional-requirements 0.0.2

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-requirements 0.0.2
File Size Uploaded
optional_requirements-0.0.2.tar.gz 9.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for optional-requirements 0.0.2
File Interpreter ABI Platform
optional_requirements-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 17.8 kB

Release files / optional_requirements-0.0.2.tar.gz

Download URL optional_requirements-0.0.2.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2ff1e6347d892a42b16092b0d70b2fa1e793e966c07a07cd3ecad4d099a5b0c0
BLAKE2b-256 checksum
How to use checksums
a7c469593d27d68715503c64db154d7c39c56e51b122373ecea2f6448395cd77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.5

Release files / optional_requirements-0.0.2-py3-none-any.whl

Download URL optional_requirements-0.0.2-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
af35ed5cb4deecf55502cb0709532f501a86043fb1612968446fbd0c5063ab84
BLAKE2b-256 checksum
How to use checksums
6e25a963f71ac4af71dc56d12bc90ed6346181462a54397123940343ecdefb77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

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