{"record":{"id":"449d7294e157293a","repo":"langflow-ai/langflow","slug":"could-not-import-attr-name-r-from-name-r","errorCode":null,"errorMessage":"Could not import {attr_name!r} from {__name__!r}: {e}","messagePattern":"Could not import (.+?) from (.+?): (.+?)","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"scripts/migrate/port_bundle.py","lineNumber":291,"sourceCode":"from lfx.components._importing import import_mod\n\nif TYPE_CHECKING:\n{type_checking_imports}\n\n_dynamic_imports = {dynamic_imports_dict}\n\n__all__ = {all_list}\n\n\ndef __getattr__(attr_name: str) -> Any:\n    if attr_name not in _dynamic_imports:\n        msg = f\"module {{__name__!r}} has no attribute {{attr_name!r}}\"\n        raise AttributeError(msg)\n    try:\n        result = import_mod(attr_name, _dynamic_imports[attr_name], __spec__.parent)\n    except (ModuleNotFoundError, ImportError, AttributeError) as e:\n        msg = f\"Could not import {{attr_name!r}} from {{__name__!r}}: {{e}}\"\n        raise AttributeError(msg) from e\n    globals()[attr_name] = result\n    return result\n\n\ndef __dir__() -> list[str]:\n    return list(__all__)\n'''\n\n\nBASE_INIT_TEMPLATE = '''\\\n\"\"\"Shared base infrastructure for the {bundle} bundle.\n\nHouses the mixin(s) every component in this bundle inherits from --\npre-extraction this lived at ``lfx.base.{bundle}``.  Moved into the\nbundle (not kept in lfx) because it is {bundle}-specific and only ever\nimported by the {bundle} components.\n\"\"\"\n","sourceCodeStart":273,"sourceCodeEnd":309,"githubUrl":"https://github.com/langflow-ai/langflow/blob/976ec789d2886a86de109c044d089d68e96c9a35/scripts/migrate/port_bundle.py#L273-L309","documentation":"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.","triggerScenarios":"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).","commonSituations":"Installing a bundle without its optional extras; CI slim environments; dependency version drift where an import that used to work now raises ImportError.","solutions":["Read the chained original exception (`raise ... from e` — check __cause__) to identify the missing module.","Install the missing dependency, usually via the bundle's extras: `pip install lfx-<bundle>[all]` or the named package directly.","If the underlying error is an ImportError inside the module itself (not a missing package), open the submodule and fix or report it upstream."],"exampleFix":"# before\nimport lfx_openai\nlfx_openai.OpenAIModel  # AttributeError: Could not import 'OpenAIModel' from 'lfx_openai': No module named 'openai'\n\n# after\npip install openai\nimport lfx_openai\nlfx_openai.OpenAIModel  # OK","handlingStrategy":"try-catch","validationCode":"import importlib.util\n\ndef deps_available(mod_names: list[str]) -> bool:\n    return all(importlib.util.find_spec(m) is not None for m in mod_names)","typeGuard":null,"tryCatchPattern":"try:\n    Component = lfx_bundle.SomeComponent\nexcept AttributeError as exc:\n    cause = exc.__cause__\n    if isinstance(cause, ModuleNotFoundError):\n        raise SystemExit(\n            f\"Missing dependency {cause.name!r} for this component. pip install it (or lfx-{bundle}[all]).\"\n        ) from exc\n    raise","preventionTips":["Always inspect exc.__cause__ — the wrapping AttributeError hides the real ModuleNotFoundError/ImportError.","Declare component runtime deps as package extras so `pip install lfx-<bundle>[all]` covers them.","Add an import smoke test per component to CI: import the module and touch each __all__ entry."],"tags":["import","dependencies","lazy-import","packaging"],"backgroundTag":null,"analyzedSha":"976ec789d2886a86de109c044d089d68e96c9a35","analyzedAt":"2026-08-14T18:23:12.227Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}