Auto install third part packages 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.6.2.tar.gz
(8.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ensure_import-0.6.2.tar.gz.
File metadata
- Download URL: ensure_import-0.6.2.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3878999fb5a3053949aa60fc60fc60b2cb38485d339842749b6891b4e11d34f7
|
|
| MD5 |
cf3122076e2c390cfd8de20ad20a617e
|
|
| BLAKE2b-256 |
b9b110b3df5be77493827a5dd2aeb3cbe035d7f7cacd52e2b44204eaa82a04d1
|
File details
Details for the file ensure_import-0.6.2-py3-none-any.whl.
File metadata
- Download URL: ensure_import-0.6.2-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae744fecbc2586fba9cd6d6b2ff639b060580cbda899163591e2d8166410a8a3
|
|
| MD5 |
466de6d40ad6acdbe73734e937d26ec5
|
|
| BLAKE2b-256 |
e83dfa6951cf5cd6c40deebd0fe11f10d03c2e03d550ee25330f0fe9405b51e9
|