Skip to main content
drawing

requires

Wheel Version py_versions Ruff

Install: pip install requires

Breaking change (v0.13.0): the requires module is no longer callable. Replace import requires; @requires(...) with from requires import requires; @requires(...).

Decorate that lets you Require/Import dependencies at runtime.

Python dependency management can be mind bottlingly complex. Optional dependencies are pretty common. Why not require the dependency at run time if a function requires said dependency?

This package has come in handy in lambda-land where you only get 250mb (on aws)!


Usage:

# This will fail
def uno():
    return json.dumps({"a": 1, "b": 2})


try:
    uno()
except NameError as ne:
    print("Error:", ne)
Error: name 'json' is not defined
# This will not fail
from requires import requires


@requires("json")
def uno():
    return json.dumps({"a": 1, "b": 2})


uno()
'{"a": 1, "b": 2}'
from requires import requires


@requires("from json import dumps")
def uno():
    return dumps({"a": 1, "b": 2})


uno()
'{"a": 1, "b": 2}'
def dos():
    return dumps({"a": 1, "b": 2})


dos()
'{"a": 1, "b": 2}'
from requires import requires


@requires(_from="json", _import="dumps")
def dos():
    return dumps({"a": 1, "b": 2})


dos()
'{"a": 1, "b": 2}'
from requires import requires


@requires(_import="rapidjson", pip="python-rapidjson", conda_forge="python-rapidjson")
def tres():
    return rapidjson.dumps({"a": 1, "b": 2})


tres()  # Will err if not install with where to install instructions
'{"a":1,"b":2}'
# should error
def quatro():
    return path.join("a", "b")


try:
    quatro()
except NameError as ne:
    print("ERROR:", ne)
ERROR: name 'path' is not defined
from requires import Requirement

os_path_req = Requirement(_import="path", _from="os")


@os_path_req
def quatro():
    return path.join("a", "b")


assert isinstance(quatro(), str)

Enforcing requirements

import requires

try:
    import alibrary
except ModuleNotFoundError:
    requirement = requires.Requirement(
        _import="alibrary",
        pip=True,
        conda_forge="alibrary-conda-listing",
        details="Install details",
    )
try:
    requirement.raise_error()
except requires.RequirementError as err:
    print("ERROR:")
    print(err)
ERROR:
Module/Package(s) not found/installed; could not import: `import alibrary`
    pip install alibrary
    conda install -c conda-forge alibrary-conda-listing
    Install details

Less verbose version:

import requires

try:
    import alibrary
except ModuleNotFoundError:
    requires.Requirement(
        _import="alibrary",
        pip=True,
        conda_forge="alibrary-conda-listing",
        details="Install details",
    ).raise_error()

Future ideas?

  • Adding support for requiring particular package versions?
  • Auto install?
  • Allow non pip/conda/conda-forge locations?

Metadata

Release files for requires 0.13.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 requires 0.13.0
File Size Uploaded Provenance Actions
requires-0.13.0.tar.gz 10.1 kB Without provenance Details for requires-0.13.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for requires 0.13.0
File Interpreter ABI Platform Actions
requires-0.13.0-py3-none-any.whl Python 3 none any Details for requires-0.13.0-py3-none-any.whl

Total release size: 20.6 kB

Release files / requires-0.13.0.tar.gz

Download URL requires-0.13.0.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
530dd817f38c7aa748c3819c78b62a89cc4647dec12a4a165db4c1b52e6f3b88
BLAKE2b-256 checksum
How to use checksums
3c41bed95223fc05f9d7079a9075362597b7edac74bb64b9d9d9a3483b207673
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / requires-0.13.0-py3-none-any.whl

Download URL requires-0.13.0-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
792a2f3e6a351b538b2eda9b84479eef71eefce2bbf35ff737bbe398bc9b44d0
BLAKE2b-256 checksum
How to use checksums
bd748f7e5214b17a872313ae88dc09363ded83625f78e218f98432368f0117eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for requires 0.13.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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