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.9

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.9
File Size Uploaded Provenance Actions
ensure_import-0.6.9.tar.gz 11.0 kB Without provenance Details for ensure_import-0.6.9.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ensure-import 0.6.9
File Interpreter ABI Platform Actions
ensure_import-0.6.9-py3-none-any.whl Python 3 none any Details for ensure_import-0.6.9-py3-none-any.whl

Total release size: 18.6 kB

Release files / ensure_import-0.6.9.tar.gz

Download URL ensure_import-0.6.9.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d84be2e88b3ec818ceecc4c70cc500eb366d02be1b2758e5cf4fb26917797203
BLAKE2b-256 checksum
How to use checksums
2986683df9d69ac6f28f1f40a4b9420af039eafc20c590af45fd838502a0f972
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / ensure_import-0.6.9-py3-none-any.whl

Download URL ensure_import-0.6.9-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
399b90131d5aaf5af6d99352895eb4ce444a3f4357e2cb9691cae06672b386f1
BLAKE2b-256 checksum
How to use checksums
2d629db101a233b544a3dbc8a8166ab724b163809e8a9aabf0fa74d3dc455710
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for ensure-import 0.6.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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