facebookresearch/detectron2 · error · ImportError

Cannot import name {relative_import_path} from {original_fil

Error message

Cannot import name {relative_import_path} from {original_file}: {cur_file} does not exist.

What it means

The relative import inside a config file resolved to a path that is neither an existing .py file nor a directory — it simply does not exist. Detectron2 reports which name was imported from which config file and the missing path.

Source

Thrown at detectron2/config/lazy.py:140

            "\n", " "
        )
        if not len(relative_import_path):
            raise ImportError(relative_import_err)

        cur_file = os.path.dirname(original_file)
        for _ in range(level - 1):
            cur_file = os.path.dirname(cur_file)
        cur_name = relative_import_path.lstrip(".")
        for part in cur_name.split("."):
            cur_file = os.path.join(cur_file, part)
        if not cur_file.endswith(".py"):
            cur_file += ".py"
        if not PathManager.isfile(cur_file):
            cur_file_no_suffix = cur_file[: -len(".py")]
            if PathManager.isdir(cur_file_no_suffix):
                raise ImportError(f"Cannot import from {cur_file_no_suffix}." + relative_import_err)
            else:
                raise ImportError(
                    f"Cannot import name {relative_import_path} from "
                    f"{original_file}: {cur_file} does not exist."
                )
        return cur_file

    def new_import(name, globals=None, locals=None, fromlist=(), level=0):
        if (
            # Only deal with relative imports inside config files
            level != 0
            and globals is not None
            and (globals.get("__package__", "") or "").startswith(_CFG_PACKAGE_NAME)
        ):
            cur_file = find_relative_file(globals["__file__"], name, level)
            _validate_py_syntax(cur_file)
            spec = importlib.machinery.ModuleSpec(
                _random_package_name(cur_file), None, origin=cur_file
            )
            module = importlib.util.module_from_spec(spec)

View on GitHub (pinned to a2f4a8771a)

Solutions

  1. Check the printed cur_file path and create/rename the config file to match
  2. Fix the number of leading dots for the intended directory level
  3. Fix typos/case in the imported module name

Example fix

# before (configs/maskrcnn.py)
from .bas_cfg import model  # typo
# after
from .base_cfg import model
Defensive patterns

Strategy: validation

Validate before calling

import os, re
for m in re.finditer(r"^\s*from\s+(\.+)([\w.]*)\s+import", open(cfg_path).read(), re.M):
    d = os.path.dirname(cfg_path)
    for _ in range(len(m.group(1)) - 1): d = os.path.dirname(d)
    p = os.path.join(d, *m.group(2).split('.')) + '.py'
    assert os.path.isfile(p), f"missing config file: {p}"

Try / catch

try:
    cfg = LazyConfig.load(path)
except ImportError as e:
    if "does not exist" in str(e):
        print("create or fix path for the imported config")
    raise

Prevention

When it happens

Trigger: In a config file: 'from .common import thing' where common.py does not exist in the same directory (typo, wrong level of '../', file moved).

Common situations: Moving/renaming config files without updating importers; wrong number of leading dots ('..base' vs '.base'); case-sensitivity mismatches on case-insensitive filesystems.

Related errors


AI-assisted analysis of facebookresearch/detectron2@a2f4a8771a (2026-08-27). Data as JSON: /api/errors/c9d0021d35b1014e. Report an issue: GitHub.