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():
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
Release history Release notifications | RSS feed
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.5.tar.gz
(5.1 kB
view hashes)
Built Distribution
Close
Hashes for ensure_import-0.3.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | faa81940c0113175f308080eda9546c52707a98a5c392d4e52dad60ab7364722 |
|
MD5 | 2a725bacd695f3f914c08c2a1a5fb502 |
|
BLAKE2b-256 | 95f903525675d2e12f7122355d1773a6d6ca81069ef3f5d05aeeb20fda3c0b7d |