nautechsystems/nautilus_trader · error

Failed to import config module {config_module_name}: {e}

Error message

Failed to import config module {config_module_name}: {e}

What it means

Raised inside the config-instance factory when py.import(config_module_name) fails — the Python module that should contain the NautilusConfig subclass cannot be imported. The underlying ImportError is embedded in {e}.

Source

Thrown at crates/live/src/python/node.rs:1797

) -> anyhow::Result<Option<Bound<'py, PyAny>>> {
    if config_path.is_empty() && config.is_empty() {
        log::debug!("No config_path or empty config, using None");
        return Ok(None);
    }

    let config_parts: Vec<&str> = config_path.split(':').collect();
    if config_parts.len() != 2 {
        anyhow::bail!("config_path must be in format 'module.path:ClassName', was {config_path}");
    }
    let (config_module_name, config_class_name) = (config_parts[0], config_parts[1]);

    log::debug!(
        "Importing config class from module: {config_module_name} class: {config_class_name}"
    );

    let config_module = py
        .import(config_module_name)
        .map_err(|e| anyhow::anyhow!("Failed to import config module {config_module_name}: {e}"))?;
    let config_class = config_module
        .getattr(config_class_name)
        .map_err(|e| anyhow::anyhow!("Failed to get config class {config_class_name}: {e}"))?;

    // Convert config dict to Python dict
    let py_dict = PyDict::new(py);

    for (key, value) in config {
        let py_value = config_value_to_py(py, key, value)?;
        py_dict.set_item(key, py_value)?;
    }

    log::debug!("Created config dict: {py_dict:?}");

    // Try kwargs first, then default constructor with setattr
    let config_instance = match config_class.call((), Some(&py_dict)) {
        Ok(instance) => {
            log::debug!("Created config instance with kwargs");

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Run python -c "import <config_module_name>" in the same env to reproduce the ImportError
  2. Install the package (pip install -e .) into the venv running the node
  3. Set PYTHONPATH (or launch from the package root) so the module resolves
  4. Fix any import-time exception inside the module shown in the embedded traceback

Example fix

// before
PYTHONPATH= ./node run --config-path myproj.cfg:StrategyConfig  # myproj not found
// after
pip install -e . && ./node run --config-path myproj.cfg:StrategyConfig
Defensive patterns

Strategy: validation

Validate before calling

import importlib
importlib.import_module(config_module_name)  # reproduce ImportError before node start

Type guard

def module_importable(name: str) -> bool:
    import importlib.util
    return importlib.util.find_spec(name) is not None

Try / catch

try:
    node.add_strategy_from_config(cfg)
except Exception as e:
    if "Failed to import config module" in str(e):
        print("Fix sys.path or install the package; module:", config_module_name)
    raise

Prevention

When it happens

Trigger: Providing a config_path like "pkg.mod:MyConfig" where pkg.mod is not importable: package not installed, wrong working directory, PYTHONPATH missing the project root, or a syntax error inside the module.

Common situations: Live node launched from a directory where the strategy package isn't on sys.path; venv without the user's package installed; typo in the module part of config_path; module raising on import (dep missing).

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/d0d89c2338e6f89a. Report an issue: GitHub.