Skip to content

Import utils

INSTALL_MAPPING = {'huggingface_hub': 'huggingface-hub', 'pinecone': 'pinecone-client', 'opensearchpy': 'opensearch-py'} module-attribute

import_optional_dependency(name)

Import an optional dependency.

If a dependency is missing, an ImportError with a nice message will be raised.

Parameters:

Name Type Description Default
name str

The module name.

required

Returns: The imported module, when found. None is returned when the package is not found and errors is False.

Source code in griptape/griptape/utils/import_utils.py
def import_optional_dependency(name: str) -> Optional[ModuleType]:
    """Import an optional dependency.

    If a dependency is missing, an ImportError with a nice message will be raised.

    Args:
        name: The module name.
    Returns:
        The imported module, when found.
        None is returned when the package is not found and `errors` is False.
    """

    package_name = INSTALL_MAPPING.get(name)
    install_name = package_name if package_name is not None else name

    msg = f"Missing optional dependency: '{install_name}'. " f"Use poetry or pip to install '{install_name}'."
    try:
        module = import_module(name)
    except ImportError:
        raise ImportError(msg)

    return module