Skip to main content

Manage optional requirements and their versions at runtime

Project description

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.")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

optional_requirements-0.0.2.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

optional_requirements-0.0.2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file optional_requirements-0.0.2.tar.gz.

File metadata

  • Download URL: optional_requirements-0.0.2.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for optional_requirements-0.0.2.tar.gz
Algorithm Hash digest
SHA256 2ff1e6347d892a42b16092b0d70b2fa1e793e966c07a07cd3ecad4d099a5b0c0
MD5 e48d9e99f5a7c77c8ae4226c2fdd44a2
BLAKE2b-256 a7c469593d27d68715503c64db154d7c39c56e51b122373ecea2f6448395cd77

See more details on using hashes here.

File details

Details for the file optional_requirements-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for optional_requirements-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 af35ed5cb4deecf55502cb0709532f501a86043fb1612968446fbd0c5063ab84
MD5 87d510f8ae6ca10dcd3096ea53f8afe3
BLAKE2b-256 6e25a963f71ac4af71dc56d12bc90ed6346181462a54397123940343ecdefb77

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page