requires
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?
Metadata
Release files for requires 0.12.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| requires-0.12.1.tar.gz | 8.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| requires-0.12.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.8 kB
Release files / requires-0.12.1.tar.gz
| Download URL | requires-0.12.1.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e05a3bfbeadbf14429ce21fd2111dc36c0aca060cbe549bb6f5f010ac0bf9bb2
|
|
BLAKE2b-256 checksum How to use checksums |
9669d04c7f4b7cf7d17086dfcc711f9fe7ec7bfaafdf0011ae4a7605ce6672b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.1
|
Release files / requires-0.12.1-py3-none-any.whl
| Download URL | requires-0.12.1-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7c8734a810e362bd4fe77d112355e8f6f069b67376dbeeab6b2731f56367068c
|
|
BLAKE2b-256 checksum How to use checksums |
0a6249d256dbad857672a4b1baad7bf43c3e4e863bb720c395a9b55aaca390f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.1
|
Security for requires 0.12.1
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.