{"record":{"id":"0a587827668be2f4","repo":"facebookresearch/detectron2","slug":"relative-import-of-directories-is-not-allowed-with","errorCode":null,"errorMessage":"Relative import of directories is not allowed within config files. Within a config file, relative import can only import other config files.","messagePattern":"Relative import of directories is not allowed within config files\\. Within a config file, relative import can only import other config files\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"detectron2/config/lazy.py","lineNumber":125,"sourceCode":"       e.g. you can import file without having __init__\n    2. do not cache modules globally; modifications of module states has no side effect\n    3. support other storage system through PathManager, so config files can be in the cloud\n    4. imported dict are turned into omegaconf.DictConfig automatically\n    \"\"\"\n    old_import = builtins.__import__\n\n    def find_relative_file(original_file, relative_import_path, level):\n        # NOTE: \"from . import x\" is not handled. Because then it's unclear\n        # if such import should produce `x` as a python module or DictConfig.\n        # This can be discussed further if needed.\n        relative_import_err = \"\"\"\nRelative import of directories is not allowed within config files.\nWithin 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                )","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/facebookresearch/detectron2/blob/a2f4a8771ab77e8411c26b27f24f9489a28a2453/detectron2/config/lazy.py#L107-L143","documentation":"Config files execute with a patched __import__ that only supports relative imports of other .py config files. An empty relative module path in a from . import X statement (or a bare relative import of a package) triggers this ImportError explaining that directories cannot be relatively imported from configs.","triggerScenarios":"Inside a LazyConfig .py file, writing 'from . import' with nothing after it, or a relative import whose module path resolves to empty after stripping dots, e.g. malformed 'from .. import' usage.","commonSituations":"Hand-editing relative imports in configs; converting a package module into a config file while leaving package-style relative imports; IDE auto-import inserting broken statements.","solutions":["Use a concrete relative config import: from .base_config import model","Import library code absolutely (full package path) instead of relatively","Ensure the name after 'import' is non-empty and refers to a .py config file"],"exampleFix":"# before (inside config)\nfrom . import\n# after\nfrom .base_config import model","handlingStrategy":"validation","validationCode":"import re\nsrc = open(cfg_path).read()\nfor m in re.finditer(r\"^\\s*from\\s+(\\.+)([\\w.]*)\\s+import\", src, re.M):\n    assert m.group(2), \"relative import with empty module path in config\"","typeGuard":null,"tryCatchPattern":"try:\n    cfg = LazyConfig.load(path)\nexcept ImportError as e:\n    if \"Relative import of directories\" in str(e):\n        print(\"rewrite relative imports in\", path)\n    raise","preventionTips":["Use explicit module names in config relative imports","Prefer absolute imports for non-config code","Keep config files flat and simple"],"tags":["config-file","relative-import","lazy-config"],"backgroundTag":"config-relative-import-error","analyzedSha":"a2f4a8771ab77e8411c26b27f24f9489a28a2453","analyzedAt":"2026-08-27T12:08:21.260Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}