Skip to main content

ensure_import

LatestVersionInPypi pre-commit Linter: Ruff

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)

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.7.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ensure_import-0.6.7-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ensure_import-0.6.7.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ensure_import-0.6.7.tar.gz
Algorithm Hash digest
SHA256 b643efa4fe932f020089b1846a8511f1a5ff3229fe5a3bae11ba109b6dc47c2b
MD5 e71ddc20d0ce5c4bfad6bf376eb2abee
BLAKE2b-256 8889e49dd2409fb47173f4d7831d47084aade2ef0d6c50e8666f59b2746b53be

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ensure_import-0.6.7-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ensure_import-0.6.7-py3-none-any.whl
Algorithm Hash digest
SHA256 45cc16b6cbd7349c9eee61027d268ca45ab2fe6ab9a2587bf9e8bfefc24c01af
MD5 c1876f0d52bfc0704dc43f557f857c39
BLAKE2b-256 84ea7fa99bc8c678c5412a7d837578614536fc8a932e551d7139b8eb352b4066

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.6.7 This release

2 files

0.6.6

2 files

0.6.5

2 files

0.6.4

2 files

0.6.3

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.1

2 files

0.5.0

2 files

0.3.6

2 files

0.3.5

2 files

0.3.4

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page