Skip to content

pixano_inference.utils.main

attempt_import(module, package=None)

Import specified module, or raise ImportError with a helpful message

Parameters:

Name Type Description Default
module str

The name of the module to import

required
package str

The package to install, None if identical to module name. Defaults to None.

None

Returns:

Type Description
ModuleType

Imported module

Source code in pixano_inference/utils/main.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def attempt_import(module: str, package: str = None) -> ModuleType:
    """Import specified module, or raise ImportError with a helpful message

    Args:
        module (str): The name of the module to import
        package (str): The package to install, None if identical to module name. Defaults to None.

    Returns:
        ModuleType: Imported module
    """

    try:
        return importlib.import_module(module)
    except ImportError as e:
        raise ImportError(
            f"Please install {module.split('.')[0]} to use this model: pip install {package or module}"
        ) from e