Skip to main content

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

Project description

ensure_import

LatestVersionInPypi Linter: Ruff pre-commit

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

Install

pip install ensure_import

Usage

  • Simple case that package name is module name
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
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
while _ei := _EI('..'):
    with _ei:
        import gunicorn
        import uvicorn

This is equal to:

try:
    import gunicorn
    import uvicorn
except ImportError:
    import sys
    sys.path.append('..')

    import gunicorn
    import uvicorn
  • Support __file__
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

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

ensure_import-0.3.6.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

ensure_import-0.3.6-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file ensure_import-0.3.6.tar.gz.

File metadata

  • Download URL: ensure_import-0.3.6.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.8 Darwin/20.6.0

File hashes

Hashes for ensure_import-0.3.6.tar.gz
Algorithm Hash digest
SHA256 7f1901fc8bd4fbe205aef50b4273796e4acc52eb24a217c73777881d380540c6
MD5 ea09a7f5f3132a15a10cbda9c7eb4d0d
BLAKE2b-256 dd7ba3b0711d3ad9306205565326a46b9bbb22c81f6697d14e32f684adb2571a

See more details on using hashes here.

File details

Details for the file ensure_import-0.3.6-py3-none-any.whl.

File metadata

  • Download URL: ensure_import-0.3.6-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.8 Darwin/20.6.0

File hashes

Hashes for ensure_import-0.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 4b343c286bd84cb35ddac1763a2aa5166709546768240d631038e072793fdf37
MD5 265eb55486bd0ead3c4f5073154477b1
BLAKE2b-256 38ffb90052875b51ac3c2f88f73034639d86cf1737207a8693fef2d8eb59f9be

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