langflow-ai/langflow · error · AttributeError

Could not import {attr_name!r} from {__name__!r}: {e}

Error message

Could not import {attr_name!r} from {__name__!r}: {e}

What it means

The companion case in the same generated lazy __init__ template: here the requested attribute IS listed in _dynamic_imports, but actually importing its target module fails. The generated __getattr__ catches ModuleNotFoundError, ImportError and AttributeError from the deferred import and re-raises them as AttributeError('Could not import <attr> from <module>: <original error>'), chaining the original exception. So the root cause is always in the '{e}' suffix — typically a transitive dependency of the component module being missing.

Source

Thrown at scripts/migrate/port_bundle.py:291

from lfx.components._importing import import_mod

if TYPE_CHECKING:
{type_checking_imports}

_dynamic_imports = {dynamic_imports_dict}

__all__ = {all_list}


def __getattr__(attr_name: str) -> Any:
    if attr_name not in _dynamic_imports:
        msg = f"module {{__name__!r}} has no attribute {{attr_name!r}}"
        raise AttributeError(msg)
    try:
        result = import_mod(attr_name, _dynamic_imports[attr_name], __spec__.parent)
    except (ModuleNotFoundError, ImportError, AttributeError) as e:
        msg = f"Could not import {{attr_name!r}} from {{__name__!r}}: {{e}}"
        raise AttributeError(msg) from e
    globals()[attr_name] = result
    return result


def __dir__() -> list[str]:
    return list(__all__)
'''


BASE_INIT_TEMPLATE = '''\
"""Shared base infrastructure for the {bundle} bundle.

Houses the mixin(s) every component in this bundle inherits from --
pre-extraction this lived at ``lfx.base.{bundle}``.  Moved into the
bundle (not kept in lfx) because it is {bundle}-specific and only ever
imported by the {bundle} components.
"""

View on GitHub (pinned to 976ec789d2)

Solutions

  1. Read the chained original exception (`raise ... from e` — check __cause__) to identify the missing module.
  2. Install the missing dependency, usually via the bundle's extras: `pip install lfx-<bundle>[all]` or the named package directly.
  3. If the underlying error is an ImportError inside the module itself (not a missing package), open the submodule and fix or report it upstream.

Example fix

# before
import lfx_openai
lfx_openai.OpenAIModel  # AttributeError: Could not import 'OpenAIModel' from 'lfx_openai': No module named 'openai'

# after
pip install openai
import lfx_openai
lfx_openai.OpenAIModel  # OK
Defensive patterns

Strategy: try-catch

Validate before calling

import importlib.util

def deps_available(mod_names: list[str]) -> bool:
    return all(importlib.util.find_spec(m) is not None for m in mod_names)

Try / catch

try:
    Component = lfx_bundle.SomeComponent
except AttributeError as exc:
    cause = exc.__cause__
    if isinstance(cause, ModuleNotFoundError):
        raise SystemExit(
            f"Missing dependency {cause.name!r} for this component. pip install it (or lfx-{bundle}[all])."
        ) from exc
    raise

Prevention

When it happens

Trigger: Accessing a valid exported attribute of a generated lfx_<bundle> package in an environment lacking that component's runtime dependency — e.g. `lfx_<bundle>.SomeComponent` where the component module does `import openai` / `from langchain_... import ...` and that package is not installed. Also fires on genuine ImportErrors inside the module (bad relative import, syntax-level name problem).

Common situations: Installing a bundle without its optional extras; CI slim environments; dependency version drift where an import that used to work now raises ImportError.

Related errors


AI-assisted analysis of langflow-ai/langflow@976ec789d2 (2026-08-14). Data as JSON: /api/errors/449d7294e157293a. Report an issue: GitHub.