<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
[![Wheel](https://img.shields.io/pypi/wheel/requires.svg)](https://img.shields.io/pypi/wheel/requires.svg)
[![Version](https://img.shields.io/pypi/v/requires.svg)](https://img.shields.io/pypi/v/requires.svg)
[![py_versions](https://img.shields.io/pypi/pyversions/requires.svg)](https://img.shields.io/pypi/pyversions/requires.svg)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.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": "https://github.com/dynamic-graphics-inc/dgpy-libs/tree/main/libs/requires",
"name": "requires",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "",
"keywords": "requires,dgpy,import,funkify,decorator",
"author": "jesse",
"author_email": "jesse@dgi.com",
"download_url": "https://files.pythonhosted.org/packages/25/a0/38f21ca428e081a6564605f581cb51b7dd3e657047cc6273b6874635757c/requires-0.10.5.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[![Wheel](https://img.shields.io/pypi/wheel/requires.svg)](https://img.shields.io/pypi/wheel/requires.svg)\n[![Version](https://img.shields.io/pypi/v/requires.svg)](https://img.shields.io/pypi/v/requires.svg)\n[![py_versions](https://img.shields.io/pypi/pyversions/requires.svg)](https://img.shields.io/pypi/pyversions/requires.svg)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](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": "MIT",
"summary": "Runtime imports and dependency utils",
"version": "0.10.5",
"split_keywords": [
"requires",
"dgpy",
"import",
"funkify",
"decorator"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "bb2120a984c49a3ed98c920c9f4892ef5b525677281b929c8e0826b8dc3ea168",
"md5": "1bd001ccec75b3846300dfd404b2ae30",
"sha256": "498cc3a5ad0c2d9f149362ab0cd3e7176a0036658872e50d40f765d09af81ef3"
},
"downloads": -1,
"filename": "requires-0.10.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1bd001ccec75b3846300dfd404b2ae30",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 8819,
"upload_time": "2023-04-21T21:13:54",
"upload_time_iso_8601": "2023-04-21T21:13:54.624450Z",
"url": "https://files.pythonhosted.org/packages/bb/21/20a984c49a3ed98c920c9f4892ef5b525677281b929c8e0826b8dc3ea168/requires-0.10.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "25a038f21ca428e081a6564605f581cb51b7dd3e657047cc6273b6874635757c",
"md5": "56b0dd4548be25a5895b995216b22faa",
"sha256": "de4ed110b9d199327ea2e91fa60c00e1878cb180d75620589b11e6d519e7b5a0"
},
"downloads": -1,
"filename": "requires-0.10.5.tar.gz",
"has_sig": false,
"md5_digest": "56b0dd4548be25a5895b995216b22faa",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<4.0",
"size": 8739,
"upload_time": "2023-04-21T21:13:57",
"upload_time_iso_8601": "2023-04-21T21:13:57.093586Z",
"url": "https://files.pythonhosted.org/packages/25/a0/38f21ca428e081a6564605f581cb51b7dd3e657047cc6273b6874635757c/requires-0.10.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-21 21:13:57",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "requires"
}