{"record":{"id":"0011d1375ed2c9f9","repo":"microsoft/autogen","slug":"could-not-load-module-from-path-module-path","errorCode":null,"errorMessage":"Could not load module from path: {module_path}","messagePattern":"Could not load module from path: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/agbench/src/agbench/load_module.py","lineNumber":11,"sourceCode":"import importlib.util\nimport os\nimport sys\nfrom types import ModuleType\n\n\ndef load_module(module_path: str) -> ModuleType:\n    module_name = os.path.basename(module_path).replace(\".py\", \"\")\n    spec = importlib.util.spec_from_file_location(module_name, module_path)\n    if spec is None:\n        raise ValueError(f\"Could not load module from path: {module_path}\")\n    module = importlib.util.module_from_spec(spec)\n    sys.modules[module_name] = module\n    assert spec.loader is not None\n    spec.loader.exec_module(module)\n    return module\n","sourceCodeStart":1,"sourceCodeEnd":17,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/agbench/src/agbench/load_module.py#L1-L17","documentation":"agbench's load_module raises this ValueError when importlib.util.spec_from_file_location returns None for the given path, which Python does when the path's extension is not recognized by any importer (typically not a .py file) or the path is malformed. Note the module name is derived by stripping '.py' via replace, so any other extension (e.g. .pyw handling aside) leaves the suffix intact and spec lookup fails.","triggerScenarios":"Calling load_module() with a path that is not a .py file (e.g. a .json, .yaml, .pyc, or extensionless file), or with a path whose suffix is unrecognized by the import system.","commonSituations":"Passing a scenario config or template file instead of the Python module, generated paths with unexpected extensions, or Windows paths where the basename manipulation mangles the name.","solutions":["Check the extension of the path before calling load_module and pass only .py files.","Print/verify the exact path being passed (absolute vs relative, typos).","If you must load a non-.py source file, register a SourceFileLoader explicitly or copy the file to a .py extension first."],"exampleFix":"# before\nmodule = load_module(config_path)  # config_path is scenario.json\n\n# after\nif not module_path.endswith(\".py\"):\n    raise ValueError(f\"load_module requires a .py file, got: {module_path}\")\nmodule = load_module(module_path)","handlingStrategy":"validation","validationCode":"import os\nif not module_path.endswith(\".py\") or not os.path.isfile(module_path):\n    raise ValueError(f\"load_module needs an existing .py file, got: {module_path}\")","typeGuard":"def is_loadable_module(path: str) -> bool:\n    return isinstance(path, str) and path.endswith(\".py\") and os.path.isfile(path)","tryCatchPattern":"try:\n    module = load_module(path)\nexcept (ValueError, ImportError) as e:\n    logger.error(\"failed to load module %s: %s\", path, e)\n    raise","preventionTips":["Construct module paths with os.path.join and assert the .py suffix at build time.","Validate scenario/template file types before dispatching to load_module."],"tags":["python","importlib","dynamic-import","file-path","agbench"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}