rohitg00/ai-engineering-from-scratch · error · ImportError

cannot load from

Error message

cannot load {name} from {file_path}

What it means

Peer-lesson loader guard: importlib could not build a usable module spec for the sibling lesson file at the given path (usually the file is missing because the repo layout is incomplete or the lesson was moved).

Solutions

  1. Verify the sibling lesson directories (54/55/56) exist next to this lesson
  2. Run from a complete checkout of the phase directory
  3. Check the reported path for typos or relocation
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/19-capstone-projects/57-end-to-end-research-demo/code/main.py:44 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/9647b7a9db4f7096. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/57-end-to-end-research-demo/code/main.py:44


def _add(path: str) -> None:
    if path not in sys.path:
        sys.path.insert(0, path)


_add(os.path.join(LESSON_ROOT, "54-paper-writer", "code"))
_add(os.path.join(LESSON_ROOT, "55-critic-loop", "code"))
_add(os.path.join(LESSON_ROOT, "56-iteration-scheduler", "code"))

import importlib
import importlib.util


def _load_module(name: str, file_path: str):
    spec = importlib.util.spec_from_file_location(name, file_path)
    if spec is None or spec.loader is None:
        raise ImportError(f"cannot load {name} from {file_path}")
    mod = importlib.util.module_from_spec(spec)
    sys.modules[name] = mod
    spec.loader.exec_module(mod)
    return mod


paper_writer_mod = _load_module(
    "_t_d2_paper_writer",
    os.path.join(LESSON_ROOT, "54-paper-writer", "code", "main.py"),
)
critic_loop_mod = _load_module(
    "_t_d2_critic_loop",
    os.path.join(LESSON_ROOT, "55-critic-loop", "code", "main.py"),
)
scheduler_mod = _load_module(
    "_t_d2_scheduler",
    os.path.join(LESSON_ROOT, "56-iteration-scheduler", "code", "main.py"),
)

View on GitHub (pinned to 39ea8a1c6d)