Skip to main content

Auto install third part packages by pip 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.5.0.tar.gz (4.9 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.5.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ensure_import-0.5.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.9 Darwin/20.6.0

File hashes

Hashes for ensure_import-0.5.0.tar.gz
Algorithm Hash digest
SHA256 4e180f3c78dcc2659958050b377395b5ec48fb7c42a0b815da445b10e19a4bf8
MD5 13a5600043e891606d36d54a63860825
BLAKE2b-256 46b172f41abc92dcc346d51a0da65a76ad47d71ae943ca6e4cb40e0e19590efb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ensure_import-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.9 Darwin/20.6.0

File hashes

Hashes for ensure_import-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1d095289607b4c0eef302c4a3fa84c847df09992f59dc53d1eb35bfa4075b0d
MD5 6d11752a1c1f77d5c945a2a40575a996
BLAKE2b-256 0fc6a311e66e51e5bd2a9a5efe9a2f37b4f90162f1aaf8245bb7ea3b32feaa27

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