{"record":{"id":"39e003fb409e733f","repo":"huggingface/smolagents","slug":"module-expression-module-has-no-attribute-alias","errorCode":null,"errorMessage":"Module {expression.module} has no attribute {alias.name}","messagePattern":"Module (.+?) has no attribute (.+?)","errorType":"exception","errorClass":"InterpreterError","httpStatus":null,"severity":"error","filePath":"src/smolagents/local_python_executor.py","lineNumber":1337,"sourceCode":"        return None\n    elif isinstance(expression, ast.ImportFrom):\n        if check_import_authorized(expression.module, authorized_imports):\n            raw_module = __import__(expression.module, fromlist=[alias.name for alias in expression.names])\n            module = get_safe_module(raw_module, authorized_imports)\n            if expression.names[0].name == \"*\":  # Handle \"from module import *\"\n                if hasattr(module, \"__all__\"):  # If module has __all__, import only those names\n                    for name in module.__all__:\n                        state[name] = getattr(module, name)\n                else:  # If no __all__, import all public names (those not starting with '_')\n                    for name in dir(module):\n                        if not name.startswith(\"_\"):\n                            state[name] = getattr(module, name)\n            else:  # regular from imports\n                for alias in expression.names:\n                    if hasattr(module, alias.name):\n                        state[alias.asname or alias.name] = getattr(module, alias.name)\n                    else:\n                        raise InterpreterError(f\"Module {expression.module} has no attribute {alias.name}\")\n        else:\n            raise InterpreterError(\n                f\"Import from {expression.module} is not allowed. Authorized imports are: {str(authorized_imports)}\"\n            )\n        return None\n\n\ndef evaluate_generatorexp(\n    genexp: ast.GeneratorExp,\n    state: dict[str, Any],\n    static_tools: dict[str, Callable],\n    custom_tools: dict[str, Callable],\n    authorized_imports: list[str],\n) -> Generator[Any]:\n    def generator():\n        for gen in genexp.generators:\n            iter_value = evaluate_ast(gen.iter, state, static_tools, custom_tools, authorized_imports)\n            for value in iter_value:","sourceCodeStart":1319,"sourceCodeEnd":1355,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/local_python_executor.py#L1319-L1355","documentation":"A `from module import name` succeeded in importing the module, but the module has no attribute with that name — the interpreter's equivalent of ImportError: cannot import name.","triggerScenarios":"from math import pie (typo), from collections import OrderedDict in a version where it moved, from json import loads_extra.","commonSituations":"Agent hallucinates a function/class name that doesn't exist in the module; version differences where a name was removed or moved; typo in the imported name.","solutions":["Check the module's actual attributes (dir(module) outside sandbox) and fix the name","For hallucinated names, guide the model with better tool/prompt descriptions or few-shot examples","If the name exists in another module, import from the correct one"],"exampleFix":"# before\nfrom math import pie\n# after\nfrom math import pi","handlingStrategy":"validation","validationCode":"import importlib\nmod = importlib.import_module('math')\nassert hasattr(mod, name_to_import), f'{name_to_import!r} missing in math'","typeGuard":null,"tryCatchPattern":"try:\n    evaluate_python(code, ...)\nexcept InterpreterError as e:\n    if 'has no attribute' in str(e):\n        # check dir(module), fix the imported name, retry","preventionTips":["Verify names exist in the target module before importing","Guard against model-hallucinated APIs with few-shot examples"],"tags":["python-executor","import","attribute-error","smolagents"],"backgroundTag":"cannot-import-name-from-module","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}