Skip to main content

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

Project description

ensure_import

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()).trying:
    with _ei:
        import uvicorn
        from fastapi import FastAPI
  • Package name is difference from module name
while (_ei := _EI()).trying:
    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('..')).trying:
    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

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.0.tar.gz (4.4 kB view hashes)

Uploaded Source

Built Distribution

ensure_import-0.3.0-py3-none-any.whl (5.3 kB view hashes)

Uploaded Python 3

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