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

could not build spec for

Error message

could not build spec for {sibling}

What it means

_load_sibling builds an importlib ModuleSpec from the sibling file path; if spec_from_file_location returns None the file cannot be turned into an importable module (unreadable, wrong type, or filesystem anomaly), so the loader aborts before execution rather than returning a half-initialized module.

Solutions

  1. Check the sibling file is a readable .py file with valid contents
  2. Re-run the producing lesson to regenerate the module cleanly
  3. Fall back to executing the sibling with runpy if spec-based import keeps failing
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/19-capstone-projects/75-end-to-end-eval-runner/code/main.py:31 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/8044695ff6dda638. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/75-end-to-end-eval-runner/code/main.py:31

import json
import os
import random
import sys
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from dataclasses import dataclass, field
from typing import Callable, Sequence


def _load_sibling(lesson_dir: str, module_name: str = "main"):
    here = os.path.dirname(os.path.abspath(__file__))
    sibling = os.path.normpath(os.path.join(here, "..", "..", lesson_dir, "code", "main.py"))
    if not os.path.isfile(sibling):
        raise ImportError(f"sibling module not found: {sibling}")
    mod_name = f"_sibling_{lesson_dir.replace('-', '_')}"
    spec = importlib.util.spec_from_file_location(mod_name, sibling)
    if spec is None or spec.loader is None:
        raise ImportError(f"could not build spec for {sibling}")
    module = importlib.util.module_from_spec(spec)
    sys.modules[mod_name] = module
    spec.loader.exec_module(module)
    return module


spec_mod = _load_sibling("70-task-spec-format")
metrics_mod = _load_sibling("71-classical-metrics")
exec_mod = _load_sibling("72-code-exec-metric")
calib_mod = _load_sibling("73-perplexity-calibration")
board_mod = _load_sibling("74-leaderboard-aggregation")


TaskSpec = spec_mod.TaskSpec


@dataclass
class Generation:

View on GitHub (pinned to 39ea8a1c6d)