import_utils

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

Import an optional dependency.

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

Parameters

NameTypeDescriptionDefault
namestrThe module name.
required

Returns

TypeDescription
ModuleTypeThe imported module, when found.
Source Code in griptape/utils/import_utils.py
def import_optional_dependency(name: str) -> 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.
    """
    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"Please install the appropriate extra: https://docs.griptape.ai/stable/griptape-framework/#extras."
    )
    try:
        module = import_module(name)
    except ImportError as exc:
        raise ImportError(msg) from exc

    return module

is_dependency_installed(name)

Check if an optional dependency is available.

Parameters

NameTypeDescriptionDefault
namestrThe module name.
required

Returns

TypeDescription
boolTrue if the dependency is available.
boolFalse if the dependency is not available.
Source Code in griptape/utils/import_utils.py
def is_dependency_installed(name: str) -> bool:
    """Check if an optional dependency is available.

    Args:
        name: The module name.

    Returns:
        True if the dependency is available.
        False if the dependency is not available.
    """
    try:
        import_optional_dependency(name)
    except ImportError:
        return False

    return True

Could this page be better? Report a problem or suggest an addition!