microsoft/autogen · error · ValueError
Could not load module from path: {module_path}
Error message
Could not load module from path: {module_path} What it means
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.
Source
Thrown at python/packages/agbench/src/agbench/load_module.py:11
import importlib.util
import os
import sys
from types import ModuleType
def load_module(module_path: str) -> ModuleType:
module_name = os.path.basename(module_path).replace(".py", "")
spec = importlib.util.spec_from_file_location(module_name, module_path)
if spec is None:
raise ValueError(f"Could not load module from path: {module_path}")
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
assert spec.loader is not None
spec.loader.exec_module(module)
return module
View on GitHub (pinned to 027ecf0a37)
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.
Example fix
# before
module = load_module(config_path) # config_path is scenario.json
# after
if not module_path.endswith(".py"):
raise ValueError(f"load_module requires a .py file, got: {module_path}")
module = load_module(module_path) Defensive patterns
Strategy: validation
Validate before calling
import os
if not module_path.endswith(".py") or not os.path.isfile(module_path):
raise ValueError(f"load_module needs an existing .py file, got: {module_path}") Type guard
def is_loadable_module(path: str) -> bool:
return isinstance(path, str) and path.endswith(".py") and os.path.isfile(path) Try / catch
try:
module = load_module(path)
except (ValueError, ImportError) as e:
logger.error("failed to load module %s: %s", path, e)
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Could not create spec
- Could not create loader
- Failed to get login URL
- Authentication failed
- Unauthorized
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/0011d1375ed2c9f9.
Report an issue: GitHub.