{"record":{"id":"f5dfdf778ec494cb","repo":"nautechsystems/nautilus_trader","slug":"failed-to-import-module-module-name-e-f5dfdf","errorCode":null,"errorMessage":"Failed to import module {module_name}: {e}","messagePattern":"Failed to import module (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/backtest/src/python/node.rs","lineNumber":481,"sourceCode":") -> PyResult<Py<PyAny>> {\n    let Some((module_name, class_name)) = component_path.split_once(':') else {\n        return Err(to_pyvalue_err(format!(\n            \"{path_field} must be in format 'module.path:ClassName'\",\n        )));\n    };\n\n    if module_name.is_empty() || class_name.is_empty() || class_name.contains(':') {\n        return Err(to_pyvalue_err(format!(\n            \"{path_field} must be in format 'module.path:ClassName'\",\n        )));\n    }\n\n    log::info!(\"Importing {component_name} from module: {module_name} class: {class_name}\");\n\n    Python::attach(|py| -> anyhow::Result<Py<PyAny>> {\n        let module = py\n            .import(module_name)\n            .map_err(|e| anyhow::anyhow!(\"Failed to import module {module_name}: {e}\"))?;\n        let class = module\n            .getattr(class_name)\n            .map_err(|e| anyhow::anyhow!(\"Failed to get class {class_name}: {e}\"))?;\n        let config_instance = create_config_instance(py, config_path, config)?;\n        let component = if let Some(config_obj) = config_instance {\n            class.call1((config_obj,))?\n        } else {\n            class.call0()?\n        };\n        Ok(component.unbind())\n    })\n    .map_err(to_pyruntime_err)\n}\n\npub(crate) fn create_config_instance<'py>(\n    py: Python<'py>,\n    config_path: &str,\n    config: &HashMap<String, serde_json::Value>,","sourceCodeStart":463,"sourceCodeEnd":499,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/backtest/src/python/node.rs#L463-L499","documentation":"create_importable_component imports a Python module by dotted path (via py.import) to instantiate an actor, strategy, or exec algorithm from its config. This error is thrown when Python's import machinery cannot import the specified module (ModuleNotFoundError, syntax error, or import-time exception).","triggerScenarios":"Adding an actor/strategy/exec algorithm from config (py_add_actor_from_config, py_add_strategy_from_config, py_add_exec_algorithm_from_config) where the config's module path is wrong, the module isn't on sys.path, or importing the module itself raises.","commonSituations":"Typo in the module string ('my_strat' vs 'my_strategy'); running the backtest script from a directory where the package isn't importable (missing PYTHONPATH or no pip install -e .); an exception at module import time (bad env var, missing third-party dependency).","solutions":["Verify the module string in the config matches the importable dotted path, e.g. 'strategies.my_strategy'.","Ensure the package is importable from the backtest entrypoint: pip install -e . or set PYTHONPATH to the project root.","Import the module manually in the same interpreter (`python -c \"import strategies.my_strategy\"`) to see the underlying import error.","Fix any exceptions raised at module import time shown in the chained {e} message."],"exampleFix":"# before\nconfig = MyStrategyConfig(module=\"my_strategy\")  # not on sys.path\n# after\n# run from repo root with strategies/ package installed:\nconfig = MyStrategyConfig(module=\"strategies.my_strategy\")","handlingStrategy":"validation","validationCode":"import importlib\ndef assert_importable(module_name: str):\n    try:\n        importlib.import_module(module_name)\n    except ImportError as e:\n        raise SystemExit(f\"Fix module path/PYTHONPATH before running backtest: {e}\")\nassert_importable(config.module)\n","typeGuard":null,"tryCatchPattern":"try:\n    node.add_actor_from_config(actor_config)\nexcept Exception as e:\n    if \"Failed to import module\" in str(e):\n        raise SystemExit(f\"Check module string and PYTHONPATH: {e}\") from e\n    raise\n","preventionTips":["Use fully-qualified dotted module paths and verify with `python -c \"import <module>\"`.","Install your strategies package with `pip install -e .` or set PYTHONPATH explicitly.","Avoid import-time side effects that can raise when the module is loaded by the backtest node."],"tags":["python","import","backtest","config"],"backgroundTag":"module-init-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}