facebookresearch/detectron2 · error · ImportError
Relative import of directories is not allowed within config
Error message
Relative import of directories is not allowed within config files. Within a config file, relative import can only import other config files.
What it means
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.
Source
Thrown at detectron2/config/lazy.py:125
e.g. you can import file without having __init__
2. do not cache modules globally; modifications of module states has no side effect
3. support other storage system through PathManager, so config files can be in the cloud
4. imported dict are turned into omegaconf.DictConfig automatically
"""
old_import = builtins.__import__
def find_relative_file(original_file, relative_import_path, level):
# NOTE: "from . import x" is not handled. Because then it's unclear
# if such import should produce `x` as a python module or DictConfig.
# This can be discussed further if needed.
relative_import_err = """
Relative import of directories is not allowed within config files.
Within a config file, relative import can only import other config files.
""".replace(
"\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."
)View on GitHub (pinned to a2f4a8771a)
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
Example fix
# before (inside config) from . import # after from .base_config import model
Defensive patterns
Strategy: validation
Validate before calling
import re
src = open(cfg_path).read()
for m in re.finditer(r"^\s*from\s+(\.+)([\w.]*)\s+import", src, re.M):
assert m.group(2), "relative import with empty module path in config" Try / catch
try:
cfg = LazyConfig.load(path)
except ImportError as e:
if "Relative import of directories" in str(e):
print("rewrite relative imports in", path)
raise Prevention
- Use explicit module names in config relative imports
- Prefer absolute imports for non-config code
- Keep config files flat and simple
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Cannot import from {cur_file_no_suffix}.
- Config file {filename} has syntax error!
- Cannot import name {relative_import_path} from {original_fil
- Config file {filename} has to be a python or yaml file.
- target of LazyCall must be a callable or defines a callable!
AI-assisted analysis of facebookresearch/detectron2@a2f4a8771a (2026-08-27).
Data as JSON: /api/errors/0a587827668be2f4.
Report an issue: GitHub.