【问题标题】:How to get Class diagram from Python source code?如何从 Python 源代码中获取类图?
【发布时间】:2020-10-16 17:28:12
【问题描述】:

我尝试使用pyreverseClient 文件夹中的Python 源代码获取类图,但它需要__init__.py

(venv) C:\Users\User\Desktop\project> pyreverse Client
parsing Client\__init__.py...
Failed to import module Client\__init__.py with error:
No module named Client\__init__.py.

我没有找到任何解决方案。有没有办法获取图表?

更新: Client文件夹中有很多文件:

Client.py
GUI.py
script.py
...

这是Client.py 代码:

class Client:
    def __init__(self):
        self.socket = None
        self.listen_socket = None
        self.buff_dict = {}
        self.message_list_dict = {}
        self.lock = threading.Lock()
        self.target = None
        self.listen_flag = True
...

这是GUI.py 代码:

class Window(object):
    def __init__(self, title, font, client):
        self.title = title
        self.font = font
        self.client = client
        self.root = tk.Tk()
        self.root.title(title)
        self.build_window()
        
class LoginWindow(Window):
    def __init__(self, client, font):
        super(LoginWindow, self).__init__('Login', font, client)
        self.build_window()
...

【问题讨论】:

  • 你能把Client的代码也贴出来吗?
  • @Anwarvic 我应该在Client文件夹中发布所有代码或文件的所有名称?
  • @huy,那么你需要在这个文件夹中创建__init__.py文件
  • @huy,没什么...只是一个空文件
  • @huy 只需使用选项-o png

标签: python uml class-diagram pyreverse


【解决方案1】:

感谢@Anwarvic 和@bruno,我想出了解决方案。

首先,在Client文件夹中创建一个空的__init__.py文件:

(venv) C:\Users\User\Desktop\project\Client> type NUL >  __init__.py

然后到我要获取类图的Client文件夹的父文件夹:

(venv) C:\Users\User\Desktop\project> pyreverse Client -o png

但是我收到了这个错误:

The output format 'png' is currently not available.
Please install 'Graphviz' to have other output formats than 'dot' or 'vcg'.

经过一番调查,我发现了这个solution。然后我可以运行pyreverse 而不会出现任何错误。

这是我使用pyreverse得到的类图:

【讨论】:

    【解决方案2】:

    您在包含Client.py 的文件夹中似乎没有__init__.py。您应该能够只创建文件而不在其中添加任何内容,因为它的主要目的是表明该文件夹是一个包。

    请参阅this SO question 关于__init__.py 以获取有关该文件的更深入说明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 2010-09-30
      • 2011-09-09
      • 2012-12-25
      相关资源
      最近更新 更多