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)

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)

Source distribution for ensure-import 0.6.8
File Size Uploaded
ensure_import-0.6.8.tar.gz 10.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ensure-import 0.6.8
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

This release

0.6.8 This release

2 release files

0.6.7

2 release files

0.6.6

2 release files

0.6.5

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page