langchain-ai/langchain · error · ImportError

module '{package!r}.{module_name!r}' not found ({err})

Error message

module '{package!r}.{module_name!r}' not found ({err})

What it means

Raised by the same import utility when an explicit `module_name` is requested (`import_module(f'.{module_name}', package=package)`) and that submodule does not exist. Unlike the attribute case this raises ImportError, chained from the original ModuleNotFoundError, telling you exactly which dotted submodule under `package` failed to import.

Source

Thrown at libs/core/langchain_core/_import_utils.py:39

    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

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Verify the submodule path exists in your installed version: `uv run python -c "import langchain_core; print(langchain_core.__version__)"` and inspect the package directory.
  2. If the module moved, update the import to the current path from the version's docs/changelog.
  3. If the failure is a transitive missing dependency, install it (`uv add <dep>`) — the chained `err` in the message names the module Python couldn't find.
  4. Align versions with the lockfile (`uv sync --all-groups`) when mixing manually installed packages.

Example fix

# before (module renamed in v0.4)
from langchain_core.agents import create_agent  # ImportError: module ... not found

# after
from langchain.agents import create_agent
Defensive patterns

Strategy: try-catch

Validate before calling

import importlib.util

def module_available(dotted: str) -> bool:
    return importlib.util.find_spec(dotted) is not None

assert module_available("langchain_core.messages")

Try / catch

try:
    thing = import_attr("langchain_core.x", module_name="y", attr_name="Thing")
except ImportError as e:
    # chained ModuleNotFoundError names the missing module/dependency
    log.warning("optional feature unavailable: %s", e)
    thing = None  # degrade gracefully for optional integrations

Prevention

When it happens

Trigger: A lazy `__getattr__` maps a public name to `(module_name, attr_name)` — e.g. `('rag', 'create_react_agent')` — and the call requests a name whose backing module path is absent, or importing that module itself fails because one of its internal imports targets a missing optional dependency (ModuleNotFoundError propagates from any depth of the failed import).

Common situations: Version skew: code written against a newer langchain-core where a module was added, or a refactor that moved `foo.bar` to `foo._bar`. Also occurs when a lazily imported integration module has an undeclared third-party dependency that isn't installed, making the submodule unimportable even though the file exists.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/df0cc05760bf0c9b. Report an issue: GitHub.