{"record":{"id":"0146343b7e580e92","repo":"FoundationAgents/MetaGPT","slug":"failed-to-import-module-init-with-error-no-mod","errorCode":null,"errorMessage":"Failed to import module __init__ with error:No module named __init__.","messagePattern":"Failed to import module __init__ with error:No module named __init__\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/repo_parser.py","lineNumber":727,"sourceCode":"            None or Parsed information from the assignment node.\n        \"\"\"\n        return [RepoParser._parse_variable(t) for t in node.targets]\n\n    async def rebuild_class_views(self, path: str | Path = None):\n        \"\"\"\n        Executes `pylint` to reconstruct the dot format class view repository file.\n\n        Args:\n            path (str | Path): The path to the target directory or file. Default is None.\n        \"\"\"\n        if not path:\n            path = self.base_directory\n        path = Path(path)\n        if not path.exists():\n            return\n        init_file = path / \"__init__.py\"\n        if not init_file.exists():\n            raise ValueError(\"Failed to import module __init__ with error:No module named __init__.\")\n        command = f\"pyreverse {str(path)} -o dot\"\n        output_dir = path / \"__dot__\"\n        output_dir.mkdir(parents=True, exist_ok=True)\n        result = subprocess.run(command, shell=True, check=True, cwd=str(output_dir))\n        if result.returncode != 0:\n            raise ValueError(f\"{result}\")\n        class_view_pathname = output_dir / \"classes.dot\"\n        class_views = await self._parse_classes(class_view_pathname)\n        relationship_views = await self._parse_class_relationships(class_view_pathname)\n        packages_pathname = output_dir / \"packages.dot\"\n        class_views, relationship_views, package_root = RepoParser._repair_namespaces(\n            class_views=class_views, relationship_views=relationship_views, path=path\n        )\n        class_view_pathname.unlink(missing_ok=True)\n        packages_pathname.unlink(missing_ok=True)\n        return class_views, relationship_views, package_root\n\n    @staticmethod","sourceCodeStart":709,"sourceCodeEnd":745,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/repo_parser.py#L709-L745","documentation":"RepoParser.generate_class_views (pyreverse-based class diagram generation) requires an __init__.py in the target directory to treat it as an importable package; if missing, this ValueError is raised mimicking an import failure. The check runs before invoking the pyreverse subprocess.","triggerScenarios":"Calling await parser.generate_class_views(path) where path has no __init__.py — e.g. a scripts folder, a data directory, or a namespace-package layout.","commonSituations":"Running class-view generation on non-package directories; pointing the parser at a flat script collection; refactoring that removed __init__.py files.","solutions":["Point generate_class_views at a directory containing __init__.py (a real Python package)","Or create an empty __init__.py in the target directory","Check path.exists() and (path/'__init__.py').exists() before calling, and skip silently if you want best-effort diagrams"],"exampleFix":"// before\nawait parser.generate_class_views(Path(\"scripts/\"))  # no __init__.py -> ValueError\n\n// after\ntarget = Path(\"mypackage/\")\nassert (target / \"__init__.py\").exists()\nawait parser.generate_class_views(target)","handlingStrategy":"validation","validationCode":"from pathlib import Path\np = Path(path)\nif not (p / \"__init__.py\").exists():\n    raise ValueError(f\"{p} is not a package; generate_class_views needs __init__.py\")","typeGuard":"from pathlib import Path\n\ndef is_python_package(path) -> bool:\n    p = Path(path)\n    return p.is_dir() and (p / \"__init__.py\").exists()","tryCatchPattern":"try:\n    await parser.generate_class_views(path)\nexcept ValueError as e:\n    if \"No module named __init__\" in str(e):\n        (Path(path) / \"__init__.py\").touch()  # or skip\n        await parser.generate_class_views(path)\n    else:\n        raise","preventionTips":["Only call generate_class_views on directories that contain __init__.py","Check (path/'__init__.py').exists() first and skip non-package dirs in bulk scans","Ensure pylint/pyreverse is installed, since the next step shells out to it"],"tags":["parser","class-diagram","pyreverse","packaging"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}