You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

pyreverse生成类图与包图异常:仅显导入无属性,包图内容不符

Troubleshooting Pyreverse Class & Package Diagram Issues

Let's walk through what's causing your problems and how to fix them step by step:

Why Your Current Output Is Wrong

1. Classes.png only shows imports, no attributes/methods

Pyreverse might not be properly parsing your classes. Common reasons include:

  • Your code isn't structured as a proper Python package (missing __init__.py in your project directory)
  • Your classes are in a standalone script instead of a module that Pyreverse can recognize
  • You're using dynamic attribute assignment (e.g., setattr() or assigning attributes outside __init__) that Pyreverse can't detect
  • The default Pyreverse settings only show public members, and your attributes/methods are prefixed with underscores (private/protected)

2. Packages.png only includes third-party code

By default, Pyreverse includes all imported packages in the package diagram. If your own code isn't showing up here, it's likely because:

  • You didn't specify your project directory correctly (using . might scan too broadly, including third-party files if they're in your working directory)
  • Your code isn't marked as a package (again, missing __init__.py)

Step-by-Step Fixes

1. Fix Your Project Structure

First, ensure your code follows a standard Python package structure:

my_project/
    __init__.py  # Can be empty, but required to mark this as a package
    core/
        __init__.py
        my_class.py  # Contains your class definitions

2. Adjust Pyreverse Command Parameters

Use these flags to get the output you want:

  • To get detailed class diagrams with attributes and methods:

    pyreverse -a 1 -m 1 -o png -p my_project my_project/
    
    • -a 1: Shows all attributes (public and private)
    • -m 1: Shows all methods (public and private)
    • -p my_project: Names your project in the diagram
    • my_project/: Targets your specific package directory instead of . (avoids scanning unrelated files)
  • To generate a package diagram that only includes your code (not third-party dependencies):

    pyreverse --ignore=site-packages -o png -p my_project my_project/
    
    • --ignore=site-packages: Excludes third-party libraries from the package diagram

3. Ensure Your Classes Are Pyreverse-Friendly

  • Stick to standard class definitions with explicit attribute assignments (preferably in __init__):
    class User:
        def __init__(self, name: str, age: int):
            self.name = name  # Explicit attribute Pyreverse can detect
            self._age = age   # Protected attribute will show with -a 1
    
        def get_age(self) -> int:
            return self._age
    
        def _validate_age(self):  # Protected method will show with -m 1
            if self._age < 0:
                raise ValueError("Age can't be negative")
    
  • Avoid dynamic attribute creation (e.g., self.new_attr = value outside __init__ or using setattr()) unless you add type hints or docstrings that Pyreverse can parse.

Getting the Combined Output You Want

If you want a single diagram that shows both package structure AND detailed class attributes/methods, Pyreverse doesn't support this natively. However, you can:

  1. Generate both the detailed classes.png and filtered packages.png as above
  2. Use a tool like Inkscape or Draw.io to combine them manually, or
  3. Switch to a tool like pydot or sphinx-apidoc (with the autodoc extension) that can generate integrated documentation with both package hierarchy and class details.

内容的提问来源于stack exchange,提问作者user2875404

火山引擎 最新活动