Skip to content

imports

Optional dependency detection, lazy imports, and availability flags.

Functions:

  • package_available –

    Check if a package is available in your environment.

  • module_available –

    Check if a module path is available in your environment.

  • check_requirement –

    Check if a package with specified version is available.

  • optional_import –

    Import a module lazily and return a boolean indicating availability.

package_available cached

package_available(package_name: str) -> bool

Check if a package is available in your environment.

Parameters:

  • package_name (str) –

    Name of the package to check (e.g. os)

Examples:

>>> package_available('os')
True
>>> package_available('bla')
False

module_available cached

module_available(module_path: str) -> bool

Check if a module path is available in your environment.

Parameters:

  • module_path (str) –

    Path to the module to check (e.g. os.bla)

Examples:

>>> module_available('os')
True
>>> module_available('os.bla')
False
>>> module_available('bla.bla')
False

check_requirement cached

check_requirement(requirement: str) -> bool

Check if a package with specified version is available.

Parameters:

  • requirement (str) –

    Package with optional version requirement (e.g., "torch>=1.10.0")

Returns:

  • bool –

    Boolean indicating if the requirement is satisfied

Examples:

>>> check_requirement("torch>=1.10.0")
True
>>> check_requirement("torch<1.10.0")
False

optional_import cached

optional_import(
    module_path: str,
    name: str = "",
    requirement: str = "",
    url: Optional[str] = None,
) -> Tuple[Any, bool]

Import a module lazily and return a boolean indicating availability.

When the top-level package is installed and no requirement is given, the returned object is a lightweight proxy that defers the actual import to first use (attribute access, call, isinstance check, or subclassing), so module-scope calls do not load heavy dependencies at import time. With a requirement, the module is imported eagerly to check its version. When the dependency is missing, the returned proxy raises an informative ImportError on any use.

The (module, available) return contract and the raising placeholder for missing dependencies are modeled on MONAI's optional_import (https://github.com/Project-MONAI/MONAI/blob/1.4.0/monai/utils/module.py#L283).

Parameters:

  • module_path (str) –

    Path to the module to import (e.g. torch)

  • name (str, default: '' ) –

    Name of the class, function or attribute to import from the module (e.g. from torch import nn, here module="torch", name="nn")

  • requirement (str, default: '' ) –

    Requirement for the module (e.g., ">=1.10.0")

  • url (Optional[str], default: None ) –

    URL of the module documentation, if any. Will be used in case of an import error.

Returns:

  • Tuple[Any, bool] –

    Imported module (or proxy) and boolean indicating availability

Examples:

>>> torch, IS_TORCH_AVAILABLE = optional_import("torch", requirement=">=2.5.0")
>>> IS_TORCH_AVAILABLE
True
>>> pkg, IS_PKG_AVAILABLE = optional_import("missing_package")
>>> IS_PKG_AVAILABLE
False
>>> pkg.some_function()  # doctest: +SKIP
ImportError: Optional module 'missing_package' does not meet the requirement missing_package.