{"record":{"id":"4e76ae4eacf725a7","repo":"facebookresearch/detectron2","slug":"config-file-filename-has-syntax-error","errorCode":null,"errorMessage":"Config file {filename} has syntax error!","messagePattern":"Config file (.+?) has syntax error!","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"detectron2/config/lazy.py","lineNumber":81,"sourceCode":"    Apply func recursively to all DictConfig in cfg.\n    \"\"\"\n    if isinstance(cfg, DictConfig):\n        func(cfg)\n        for v in cfg.values():\n            _visit_dict_config(v, func)\n    elif isinstance(cfg, ListConfig):\n        for v in cfg:\n            _visit_dict_config(v, func)\n\n\ndef _validate_py_syntax(filename):\n    # see also https://github.com/open-mmlab/mmcv/blob/master/mmcv/utils/config.py\n    with PathManager.open(filename, \"r\") as f:\n        content = f.read()\n    try:\n        ast.parse(content)\n    except SyntaxError as e:\n        raise SyntaxError(f\"Config file {filename} has syntax error!\") from e\n\n\ndef _cast_to_config(obj):\n    # if given a dict, return DictConfig instead\n    if isinstance(obj, dict):\n        return DictConfig(obj, flags={\"allow_objects\": True})\n    return obj\n\n\n_CFG_PACKAGE_NAME = \"detectron2._cfg_loader\"\n\"\"\"\nA namespace to put all imported config into.\n\"\"\"\n\n\ndef _random_package_name(filename):\n    # generate a random package name when loading config files\n    return _CFG_PACKAGE_NAME + str(uuid.uuid4())[:4] + \".\" + os.path.basename(filename)","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/facebookresearch/detectron2/blob/a2f4a8771ab77e8411c26b27f24f9489a28a2453/detectron2/config/lazy.py#L63-L99","documentation":"Before executing a Python config file, detectron2 parses it with ast.parse to give a friendlier error. A SyntaxError from the file (bad indentation, unclosed bracket, Python-2 style print) is re-raised as this message with the original error chained.","triggerScenarios":"Calling fvcore/detectron2 LazyConfig.load('my_cfg.py') where the file fails to compile: unterminated string, missing colon after if, invalid syntax for the running Python version.","commonSituations":"Editing configs by hand or via sed and breaking syntax; configs written for a newer Python version (e.g. match statements under 3.10); generated configs with truncated lines.","solutions":["Run python -m py_compile my_cfg.py to see the exact line/column of the syntax error","Fix the reported syntax error in the config file","If using new syntax, upgrade Python or rewrite the construct compatibly"],"exampleFix":"# before (my_cfg.py)\nmodel = dict(target=\"...\"  # missing closing paren\n# after\nmodel = dict(target=\"...\")","handlingStrategy":"validation","validationCode":"import ast\nsrc = open(cfg_path).read()\ntry:\n    ast.parse(src)\nexcept SyntaxError as e:\n    raise SystemExit(f\"fix config first: {e}\")","typeGuard":null,"tryCatchPattern":"try:\n    cfg = LazyConfig.load(path)\nexcept SyntaxError as e:\n    if \"has syntax error\" in str(e) and e.__cause__:\n        print(\"line\", e.__cause__.lineno, \"col\", e.__cause__.offset)\n    raise","preventionTips":["Run python -m py_compile on configs in CI","Validate configs after code-generation steps","Match config syntax to the training environment's Python version"],"tags":["config-file","syntax-error","lazy-config","python"],"backgroundTag":"config-file-syntax-error","analyzedSha":"a2f4a8771ab77e8411c26b27f24f9489a28a2453","analyzedAt":"2026-08-27T12:08:21.260Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}