{"record":{"id":"746626834a615663","repo":"facebookresearch/detectron2","slug":"cannot-import-from-cur-file-no-suffix","errorCode":null,"errorMessage":"Cannot import from {cur_file_no_suffix}.","messagePattern":"Cannot import from (.+?)\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"detectron2/config/lazy.py","lineNumber":138,"sourceCode":"Within a config file, relative import can only import other config files.\n\"\"\".replace(\n            \"\\n\", \" \"\n        )\n        if not len(relative_import_path):\n            raise ImportError(relative_import_err)\n\n        cur_file = os.path.dirname(original_file)\n        for _ in range(level - 1):\n            cur_file = os.path.dirname(cur_file)\n        cur_name = relative_import_path.lstrip(\".\")\n        for part in cur_name.split(\".\"):\n            cur_file = os.path.join(cur_file, part)\n        if not cur_file.endswith(\".py\"):\n            cur_file += \".py\"\n        if not PathManager.isfile(cur_file):\n            cur_file_no_suffix = cur_file[: -len(\".py\")]\n            if PathManager.isdir(cur_file_no_suffix):\n                raise ImportError(f\"Cannot import from {cur_file_no_suffix}.\" + relative_import_err)\n            else:\n                raise ImportError(\n                    f\"Cannot import name {relative_import_path} from \"\n                    f\"{original_file}: {cur_file} does not exist.\"\n                )\n        return cur_file\n\n    def new_import(name, globals=None, locals=None, fromlist=(), level=0):\n        if (\n            # Only deal with relative imports inside config files\n            level != 0\n            and globals is not None\n            and (globals.get(\"__package__\", \"\") or \"\").startswith(_CFG_PACKAGE_NAME)\n        ):\n            cur_file = find_relative_file(globals[\"__file__\"], name, level)\n            _validate_py_syntax(cur_file)\n            spec = importlib.machinery.ModuleSpec(\n                _random_package_name(cur_file), None, origin=cur_file","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/facebookresearch/detectron2/blob/a2f4a8771ab77e8411c26b27f24f9489a28a2453/detectron2/config/lazy.py#L120-L156","documentation":"The patched relative import in config files found a directory where a .py config file was expected: it built cur_file, appended .py, saw the file didn't exist, but a matching directory (cur_file minus .py) does exist. Relative imports inside configs may only target config files, not packages/directories.","triggerScenarios":"In a config .py: 'from .models import something' where models/ is a directory (Python package) rather than models.py, under the patched import used by LazyConfig.load.","commonSituations":"Structuring configs like a package with subfolders and using relative imports; mixing regular Python modules into config directories.","solutions":["Rename the target so it is a config file: use from .models_config import X with models_config.py present","If you want to import real code, use the absolute package path (from myproj.models import X), which the patched import forwards to the real __import__","Flatten config directory structure to plain .py files"],"exampleFix":"# before\nfrom .models import model  # models/ is a directory\n# after\nfrom mypkg.configs.models import model  # absolute import of real module\n# or create models.py config file","handlingStrategy":"validation","validationCode":"import os, re\nfor m in re.finditer(r\"^\\s*from\\s+(\\.+)([\\w.]*)\\s+import\", open(cfg_path).read(), re.M):\n    target = os.path.join(os.path.dirname(cfg_path), *m.group(2).split('.')) + '.py'\n    assert os.path.isfile(target), f\"{target} is not a config file (dir?)\"","typeGuard":null,"tryCatchPattern":"try:\n    cfg = LazyConfig.load(path)\nexcept ImportError as e:\n    if \"Cannot import from\" in str(e):\n        print(\"use absolute import for packages; relative only for .py configs\")\n    raise","preventionTips":["Don't organize configs as Python packages with relative imports","Use absolute imports for real modules","Name config files distinctly from package names"],"tags":["config-file","relative-import","lazy-config","directory"],"backgroundTag":"config-relative-import-error","analyzedSha":"a2f4a8771ab77e8411c26b27f24f9489a28a2453","analyzedAt":"2026-08-27T12:08:21.260Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}