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
- Use with ipython
uvx --python=.venv/bin/python --with=ensure-import ipython
In IPython:
# Append '.venv/lib/python3.x/site-packages' to sys.path
# So that no need to install ipython into virtual environment
In [1]: from ensure_import import *;EnsureImport.activate(verbose=True);EnsureImport.show(verbose=True)
Metadata
Release files for ensure-import 0.6.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ensure_import-0.6.8.tar.gz | 10.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ensure_import-0.6.8-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.4 kB
Release files / ensure_import-0.6.8.tar.gz
| Download URL | ensure_import-0.6.8.tar.gz |
|---|---|
| Size | 10.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1fe95399309b73d27b7354b40f97dddae9c9364827e7d87d39f21017b5524990
|
|
BLAKE2b-256 checksum How to use checksums |
317c18b6f58555a0fd009f9702191c26e5a3b202b7abf90caa9c40be390fa8b5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / ensure_import-0.6.8-py3-none-any.whl
| Download URL | ensure_import-0.6.8-py3-none-any.whl |
|---|---|
| Size | 7.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bb0cf80aa00a1dfa4a2c6d9baeab9e6c63214eadbc7734498abcc7131a426094
|
|
BLAKE2b-256 checksum How to use checksums |
27fa3190392c32debc3a230dcb8729475507b186e209fa2ce37d7c9f65a1cd80
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|