Skip to main content

Auto install third part packages into virtual environment when import error.

Project description

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

Project details


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.1.tar.gz (8.5 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.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ensure_import-0.6.1.tar.gz
Algorithm Hash digest
SHA256 d82d6d97f1aca52f67212bf010e177fbeabf7afa27f20e9b73da269e3b2b2b41
MD5 5035d8c41862f4870991b60f0a2bbe14
BLAKE2b-256 fd5d9e954ed5f28355f55dd5544b88a5e07ba59654a3edbbb5db0a28e16f7060

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ensure_import-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ebf1e63cbb2e44429b040fb2d42d9245cabfd7f1ece5ab1ef986915e68cf9ce2
MD5 e07d76b2338ccb125bb158264b1dffc0
BLAKE2b-256 bacf13db6af30dcf534205f22400bda0564513f2351a0ae4d308927845edd5f7

See more details on using hashes here.

Supported by

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