Home / Function/ guard_import() — langchain Function Reference

guard_import() — langchain Function Reference

Architecture documentation for the guard_import() function in utils.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  9f9eefb6_e0ed_94d4_e160_b6c1dc6d07dc["guard_import()"]
  b77fd012_b825_e350_c8f5_a8f1b44997d9["utils.py"]
  9f9eefb6_e0ed_94d4_e160_b6c1dc6d07dc -->|defined in| b77fd012_b825_e350_c8f5_a8f1b44997d9
  style 9f9eefb6_e0ed_94d4_e160_b6c1dc6d07dc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/utils/utils.py lines 116–143

def guard_import(
    module_name: str, *, pip_name: str | None = None, package: str | None = None
) -> Any:
    """Dynamically import a module.

    Raise an exception if the module is not installed.

    Args:
        module_name: The name of the module to import.
        pip_name: The name of the module to install with pip.
        package: The package to import the module from.

    Returns:
        The imported module.

    Raises:
        ImportError: If the module is not installed.
    """
    try:
        module = importlib.import_module(module_name, package)
    except (ImportError, ModuleNotFoundError) as e:
        pip_name = pip_name or module_name.split(".", maxsplit=1)[0].replace("_", "-")
        msg = (
            f"Could not import {module_name} python package. "
            f"Please install it with `pip install {pip_name}`."
        )
        raise ImportError(msg) from e
    return module

Domain

Subdomains

Frequently Asked Questions

What does guard_import() do?
guard_import() is a function in the langchain codebase, defined in libs/core/langchain_core/utils/utils.py.
Where is guard_import() defined?
guard_import() is defined in libs/core/langchain_core/utils/utils.py at line 116.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free