Skip to main content

Runtime imports and dependency utils

Project description

drawing

requires

Wheel Version py_versions Code style: black

Install: pip install 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
import requires  # Module is callable! (checkout funkify for more info -- `pip install funkify`)

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

uno()
'{"a": 1, "b": 2}'
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}'
import requires

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

dos()
'{"a": 1, "b": 2}'
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?

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

requires-0.10.3.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

requires-0.10.3-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file requires-0.10.3.tar.gz.

File metadata

  • Download URL: requires-0.10.3.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.7 Windows/10

File hashes

Hashes for requires-0.10.3.tar.gz
Algorithm Hash digest
SHA256 ebac72997045d281064c6619652ba77dd8aca5a8b8469de6b23a255855b1b9fd
MD5 f2da1c38cdf973eca333e6af074f3f92
BLAKE2b-256 eb2775133d368615edaf4083e15684098e01fdb397c8c6c63f0081d0be3eee65

See more details on using hashes here.

File details

Details for the file requires-0.10.3-py3-none-any.whl.

File metadata

  • Download URL: requires-0.10.3-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.7 Windows/10

File hashes

Hashes for requires-0.10.3-py3-none-any.whl
Algorithm Hash digest
SHA256 873cc3230af1733bc4649265069b336348bd5f2a2b4c92b0f791a5e4a216f57d
MD5 546070accbf68d9cf68a42b4a9d60dc3
BLAKE2b-256 c6d0df7596d88f690503101dfa4b25500bb277fea3a0e0a645cf231d18773b58

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