Home / Function/ import_attr() — langchain Function Reference

import_attr() — langchain Function Reference

Architecture documentation for the import_attr() function in _import_utils.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  d7e9d742_01f6_45f8_18db_235e2e0b39c7["import_attr()"]
  a635ef7c_881f_22dd_4254_c1034294acf3["_import_utils.py"]
  d7e9d742_01f6_45f8_18db_235e2e0b39c7 -->|defined in| a635ef7c_881f_22dd_4254_c1034294acf3
  style d7e9d742_01f6_45f8_18db_235e2e0b39c7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/_import_utils.py lines 4–41

def import_attr(
    attr_name: str,
    module_name: str | None,
    package: str | None,
) -> object:
    """Import an attribute from a module located in a package.

    This utility function is used in custom `__getattr__` methods within `__init__.py`
    files to dynamically import attributes.

    Args:
        attr_name: The name of the attribute to import.
        module_name: The name of the module to import from.

            If `None`, the attribute is imported from the package itself.
        package: The name of the package where the module is located.

    Raises:
        ImportError: If the module cannot be found.
        AttributeError: If the attribute does not exist in the module or package.

    Returns:
        The imported attribute.
    """
    if module_name == "__module__" or module_name is None:
        try:
            result = import_module(f".{attr_name}", package=package)
        except ModuleNotFoundError:
            msg = f"module '{package!r}' has no attribute {attr_name!r}"
            raise AttributeError(msg) from None
    else:
        try:
            module = import_module(f".{module_name}", package=package)
        except ModuleNotFoundError as err:
            msg = f"module '{package!r}.{module_name!r}' not found ({err})"
            raise ImportError(msg) from None
        result = getattr(module, attr_name)
    return result

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free