ensure-import


Nameensure-import JSON
Version 0.3.6 PyPI version JSON
download
home_pagehttps://github.com/waketzheng/ensure_import
SummaryAuto install third part packages by pip into virtual environment when import error.
upload_time2024-04-25 01:58:13
maintainerNone
docs_urlNone
authorWaket Zheng
requires_python<4.0,>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ensure_import

[![LatestVersionInPypi](https://img.shields.io/pypi/v/ensure_import.svg?style=for-the-badge)](https://pypi.python.org/pypi/ensure_import)
[![Linter: Ruff](https://img.shields.io/badge/Linter-Ruff-brightgreen?style=flat-square)](https://github.com/astral-sh/ruff)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=for-the-badge)](https://github.com/pre-commit/pre-commit)

Auto install third part packages by pip into virtual environment when import error.

## Install
```bash
pip install ensure_import
```

## Usage
- Simple case that package name is module name
```py
from ensure_import import EnsureImport as _EI

while _ei := _EI():
    with _ei:
        import uvicorn
        from fastapi import FastAPI
```
- Package name is difference from module name
```py
while _ei := _EI():
    with _ei(dotenv='python-dotenv', odbc='pyodbc'):
        import numpy as np
        import uvicorn
        import odbc  # who's package name is `pyodbc`
        from fastapi import FastAPI
        # package name of dotenv is `python-dotenv`
        from dotenv import load_dotenv
```
- Supply module path
```py
while _ei := _EI('..'):
    with _ei:
        import gunicorn
        import uvicorn
```
This is equal to:
```py
try:
    import gunicorn
    import uvicorn
except ImportError:
    import sys
    sys.path.append('..')

    import gunicorn
    import uvicorn
```
- Support `__file__`

```py
while _ei := _EI(__file__):
    with _ei:
        import local_module

# is equal to:
try:
    import local_module
except ImportError:
    import sys
    from pathlib import Path
    dirpath: str = Path(__file__).parent.as_posix()
    if dirpath not in sys.path:
        sys.path.append(dirpath)

    import local_module
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/waketzheng/ensure_import",
    "name": "ensure-import",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Waket Zheng",
    "author_email": "waketzheng@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/dd/7b/a3b0711d3ad9306205565326a46b9bbb22c81f6697d14e32f684adb2571a/ensure_import-0.3.6.tar.gz",
    "platform": null,
    "description": "# ensure_import\n\n[![LatestVersionInPypi](https://img.shields.io/pypi/v/ensure_import.svg?style=for-the-badge)](https://pypi.python.org/pypi/ensure_import)\n[![Linter: Ruff](https://img.shields.io/badge/Linter-Ruff-brightgreen?style=flat-square)](https://github.com/astral-sh/ruff)\n[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=for-the-badge)](https://github.com/pre-commit/pre-commit)\n\nAuto install third part packages by pip into virtual environment when import error.\n\n## Install\n```bash\npip install ensure_import\n```\n\n## Usage\n- Simple case that package name is module name\n```py\nfrom ensure_import import EnsureImport as _EI\n\nwhile _ei := _EI():\n    with _ei:\n        import uvicorn\n        from fastapi import FastAPI\n```\n- Package name is difference from module name\n```py\nwhile _ei := _EI():\n    with _ei(dotenv='python-dotenv', odbc='pyodbc'):\n        import numpy as np\n        import uvicorn\n        import odbc  # who's package name is `pyodbc`\n        from fastapi import FastAPI\n        # package name of dotenv is `python-dotenv`\n        from dotenv import load_dotenv\n```\n- Supply module path\n```py\nwhile _ei := _EI('..'):\n    with _ei:\n        import gunicorn\n        import uvicorn\n```\nThis is equal to:\n```py\ntry:\n    import gunicorn\n    import uvicorn\nexcept ImportError:\n    import sys\n    sys.path.append('..')\n\n    import gunicorn\n    import uvicorn\n```\n- Support `__file__`\n\n```py\nwhile _ei := _EI(__file__):\n    with _ei:\n        import local_module\n\n# is equal to:\ntry:\n    import local_module\nexcept ImportError:\n    import sys\n    from pathlib import Path\n    dirpath: str = Path(__file__).parent.as_posix()\n    if dirpath not in sys.path:\n        sys.path.append(dirpath)\n\n    import local_module\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Auto install third part packages by pip into virtual environment when import error.",
    "version": "0.3.6",
    "project_urls": {
        "Homepage": "https://github.com/waketzheng/ensure_import"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "38ffb90052875b51ac3c2f88f73034639d86cf1737207a8693fef2d8eb59f9be",
                "md5": "265eb55486bd0ead3c4f5073154477b1",
                "sha256": "4b343c286bd84cb35ddac1763a2aa5166709546768240d631038e072793fdf37"
            },
            "downloads": -1,
            "filename": "ensure_import-0.3.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "265eb55486bd0ead3c4f5073154477b1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 6075,
            "upload_time": "2024-04-25T01:58:11",
            "upload_time_iso_8601": "2024-04-25T01:58:11.672034Z",
            "url": "https://files.pythonhosted.org/packages/38/ff/b90052875b51ac3c2f88f73034639d86cf1737207a8693fef2d8eb59f9be/ensure_import-0.3.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dd7ba3b0711d3ad9306205565326a46b9bbb22c81f6697d14e32f684adb2571a",
                "md5": "ea09a7f5f3132a15a10cbda9c7eb4d0d",
                "sha256": "7f1901fc8bd4fbe205aef50b4273796e4acc52eb24a217c73777881d380540c6"
            },
            "downloads": -1,
            "filename": "ensure_import-0.3.6.tar.gz",
            "has_sig": false,
            "md5_digest": "ea09a7f5f3132a15a10cbda9c7eb4d0d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 5170,
            "upload_time": "2024-04-25T01:58:13",
            "upload_time_iso_8601": "2024-04-25T01:58:13.375067Z",
            "url": "https://files.pythonhosted.org/packages/dd/7b/a3b0711d3ad9306205565326a46b9bbb22c81f6697d14e32f684adb2571a/ensure_import-0.3.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-25 01:58:13",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "waketzheng",
    "github_project": "ensure_import",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "ensure-import"
}
        
Elapsed time: 1.44559s