Name | requires JSON |
Version |
0.11.1
JSON |
| download |
home_page | None |
Summary | Runtime imports and dependency utils |
upload_time | 2025-07-18 00:17:22 |
maintainer | None |
docs_url | None |
author | None |
requires_python | <4.0,>=3.9 |
license | None |
keywords |
decorator
dgpy
funkify
import
requires
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
<a href="https://github.com/dynamic-graphics-inc/dgpy-libs">
<img align="right" src="https://github.com/dynamic-graphics-inc/dgpy-libs/blob/main/docs/images/dgpy_banner.svg?raw=true" alt="drawing" height="120" width="300"/>
</a>
# requires
[](https://img.shields.io/pypi/wheel/requires.svg)
[](https://img.shields.io/pypi/v/requires.svg)
[](https://img.shields.io/pypi/pyversions/requires.svg)
[](https://github.com/psf/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:
```python
# 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
```python
# 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}'
```python
import requires
@requires("from json import dumps")
def uno():
return dumps({"a": 1, "b": 2})
uno()
```
'{"a": 1, "b": 2}'
```python
def dos():
return dumps({"a": 1, "b": 2})
dos()
```
'{"a": 1, "b": 2}'
```python
import requires
@requires(_from="json", _import="dumps")
def dos():
return dumps({"a": 1, "b": 2})
dos()
```
'{"a": 1, "b": 2}'
```python
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}'
```python
# should error
def quatro():
return path.join("a", "b")
try:
quatro()
except NameError as ne:
print("ERROR:", ne)
```
ERROR: name 'path' is not defined
```python
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
```python
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:
```python
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?
Raw data
{
"_id": null,
"home_page": null,
"name": "requires",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": "decorator, dgpy, funkify, import, requires",
"author": null,
"author_email": "jesse <jesse@dgi.com>",
"download_url": "https://files.pythonhosted.org/packages/01/52/e85d571d2615f62330eaa423d23877ea3f77e7add6167a4e4ca5a8f7b532/requires-0.11.1.tar.gz",
"platform": null,
"description": "<a href=\"https://github.com/dynamic-graphics-inc/dgpy-libs\">\n<img align=\"right\" src=\"https://github.com/dynamic-graphics-inc/dgpy-libs/blob/main/docs/images/dgpy_banner.svg?raw=true\" alt=\"drawing\" height=\"120\" width=\"300\"/>\n</a>\n\n# requires\n\n[](https://img.shields.io/pypi/wheel/requires.svg)\n[](https://img.shields.io/pypi/v/requires.svg)\n[](https://img.shields.io/pypi/pyversions/requires.svg)\n[](https://github.com/psf/black)\n\n**Install:** `pip install requires`\n\nDecorate that lets you\nRequire/Import dependencies at runtime.\n\nPython 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?\n\nThis package has come in handy in lambda-land where you only get 250mb (on aws)!\n\n---\n\n## Usage:\n\n```python\n# This will fail\ndef uno():\n return json.dumps({\"a\": 1, \"b\": 2})\n\n\ntry:\n uno()\nexcept NameError as ne:\n print(\"Error:\", ne)\n```\n\n Error: name 'json' is not defined\n\n```python\n# This will not fail\nimport requires # Module is callable! (checkout funkify for more info -- `pip install funkify`)\n\n\n@requires(\"json\")\ndef uno():\n return json.dumps({\"a\": 1, \"b\": 2})\n\n\nuno()\n```\n\n '{\"a\": 1, \"b\": 2}'\n\n```python\nimport requires\n\n\n@requires(\"from json import dumps\")\ndef uno():\n return dumps({\"a\": 1, \"b\": 2})\n\n\nuno()\n```\n\n '{\"a\": 1, \"b\": 2}'\n\n```python\ndef dos():\n return dumps({\"a\": 1, \"b\": 2})\n\n\ndos()\n```\n\n '{\"a\": 1, \"b\": 2}'\n\n```python\nimport requires\n\n\n@requires(_from=\"json\", _import=\"dumps\")\ndef dos():\n return dumps({\"a\": 1, \"b\": 2})\n\n\ndos()\n```\n\n '{\"a\": 1, \"b\": 2}'\n\n```python\nimport requires\n\n\n@requires(_import=\"rapidjson\", pip=\"python-rapidjson\", conda_forge=\"python-rapidjson\")\ndef tres():\n return rapidjson.dumps({\"a\": 1, \"b\": 2})\n\n\ntres() # Will err if not install with where to install instructions\n```\n\n '{\"a\":1,\"b\":2}'\n\n```python\n# should error\ndef quatro():\n return path.join(\"a\", \"b\")\n\n\ntry:\n quatro()\nexcept NameError as ne:\n print(\"ERROR:\", ne)\n```\n\n ERROR: name 'path' is not defined\n\n```python\nfrom requires import Requirement\n\nos_path_req = Requirement(_import=\"path\", _from=\"os\")\n\n\n@os_path_req\ndef quatro():\n return path.join(\"a\", \"b\")\n\n\nassert isinstance(quatro(), str)\n```\n\n## Enforcing requirements\n\n```python\nimport requires\n\ntry:\n import alibrary\nexcept ModuleNotFoundError:\n requirement = requires.Requirement(\n _import=\"alibrary\",\n pip=True,\n conda_forge=\"alibrary-conda-listing\",\n details=\"Install details\",\n )\ntry:\n requirement.raise_error()\nexcept requires.RequirementError as err:\n print(\"ERROR:\")\n print(err)\n```\n\n ERROR:\n Module/Package(s) not found/installed; could not import: `import alibrary`\n pip install alibrary\n conda install -c conda-forge alibrary-conda-listing\n Install details\n\n## Less verbose version:\n\n```python\nimport requires\n\ntry:\n import alibrary\nexcept ModuleNotFoundError:\n requires.Requirement(\n _import='alibrary',\n pip=True,\n conda_forge='alibrary-conda-listing',\n details=\"Install details\"\n ).raise_error()\n```\n\n---\n\n## Future ideas?\n\n- Adding support for requiring particular package versions?\n- Auto install?\n- Allow non pip/conda/conda-forge locations?\n",
"bugtrack_url": null,
"license": null,
"summary": "Runtime imports and dependency utils",
"version": "0.11.1",
"project_urls": null,
"split_keywords": [
"decorator",
" dgpy",
" funkify",
" import",
" requires"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "aa35776f1cf69495cdbe883d9d509f3b89d31fc893e4f7b6cbc72b57395f49f7",
"md5": "3147c8094ccfbfa74b790572c91d5e39",
"sha256": "0cde65805284b67e30197f83d9b81e820efba36a17b1b71ea955a87185336573"
},
"downloads": -1,
"filename": "requires-0.11.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3147c8094ccfbfa74b790572c91d5e39",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 11098,
"upload_time": "2025-07-18T00:17:21",
"upload_time_iso_8601": "2025-07-18T00:17:21.262193Z",
"url": "https://files.pythonhosted.org/packages/aa/35/776f1cf69495cdbe883d9d509f3b89d31fc893e4f7b6cbc72b57395f49f7/requires-0.11.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "0152e85d571d2615f62330eaa423d23877ea3f77e7add6167a4e4ca5a8f7b532",
"md5": "a4479af745b5c063387b8cff38e519ab",
"sha256": "0a7c5631037af145d63dac61afdd1cd80de1ef25d184a59148b0f1f8ba9ae4d6"
},
"downloads": -1,
"filename": "requires-0.11.1.tar.gz",
"has_sig": false,
"md5_digest": "a4479af745b5c063387b8cff38e519ab",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 10035,
"upload_time": "2025-07-18T00:17:22",
"upload_time_iso_8601": "2025-07-18T00:17:22.948283Z",
"url": "https://files.pythonhosted.org/packages/01/52/e85d571d2615f62330eaa423d23877ea3f77e7add6167a4e4ca5a8f7b532/requires-0.11.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-18 00:17:22",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "requires"
}