{"record":{"id":"e9dcb7b3dfe20a37","repo":"huggingface/smolagents","slug":"import-of-alias-name-is-not-allowed-authorized","errorCode":null,"errorMessage":"Import of {alias.name} is not allowed. Authorized imports are: {str(authorized_imports)}","messagePattern":"Import of (.+?) is not allowed\\. Authorized imports are: (.+?)","errorType":"exception","errorClass":"InterpreterError","httpStatus":null,"severity":"error","filePath":"src/smolagents/local_python_executor.py","lineNumber":1316,"sourceCode":"            )\n            continue\n        # Recursively process nested modules, passing visited set\n        if isinstance(attr_value, ModuleType):\n            attr_value = get_safe_module(attr_value, authorized_imports, visited=visited)\n\n        setattr(safe_module, attr_name, attr_value)\n\n    return safe_module\n\n\ndef evaluate_import(expression, state, authorized_imports):\n    if isinstance(expression, ast.Import):\n        for alias in expression.names:\n            if check_import_authorized(alias.name, authorized_imports):\n                raw_module = import_module(alias.name)\n                state[alias.asname or alias.name] = get_safe_module(raw_module, authorized_imports)\n            else:\n                raise InterpreterError(\n                    f\"Import of {alias.name} is not allowed. Authorized imports are: {str(authorized_imports)}\"\n                )\n        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):","sourceCodeStart":1298,"sourceCodeEnd":1334,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/local_python_executor.py#L1298-L1334","documentation":"An `import X` statement referenced a module not in the executor's authorized_imports whitelist. The sandbox only imports modules explicitly permitted (default: a small safe set; '*' allows all).","triggerScenarios":"import requests inside agent code when authorized_imports=['time','math']; importing subprocess, os, socket, or any third-party package not whitelisted.","commonSituations":"Default smolagents configuration only allows a few safe modules; agent-generated code tries to pip-install/import pandas, numpy, requests; deploying with stricter authorized_imports than dev environment.","solutions":["Pass the module in authorized_imports when creating the agent: ToolCallingAgent(..., authorized_imports=['requests', 'pandas']) or use smolagents's helper for standard subsets","Replace the import with an existing tool (e.g. HTTP fetch via a provided tool instead of requests)","Use AdditionalResources/approved-imports patterns from smolagents docs to scope what agents may import"],"exampleFix":"# before\nagent = CodeAgent(tools=[], model=model)  # default imports only\n# code: import requests -> blocked\n# after\nagent = CodeAgent(tools=[], model=model, authorized_imports=['requests'])","handlingStrategy":"validation","validationCode":"from smolagents.utils import get_module_imports_allowed  # or construct list explicitly\nAUTHORIZED = ['math', 'time', 'random', 'requests']\nassert all(m in AUTHORIZED for m in required_modules), 'code needs unapproved imports'","typeGuard":null,"tryCatchPattern":"try:\n    evaluate_python(code, static_tools=base_python_tools, authorized_imports=AUTHORIZED)\nexcept InterpreterError as e:\n    if 'not allowed' in str(e) and 'Import of' in str(e):\n        # either extend authorized_imports or rewrite code to use tools","preventionTips":["Set authorized_imports explicitly per task scope","Review agent-generated imports before execution in high-stakes settings","Expose capabilities as tools instead of granting broad imports"],"tags":["python-executor","import","sandbox","security","smolagents"],"backgroundTag":"module-import-not-authorized","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}