如何从Python源代码中获取类图?

2024-05-13 20:21:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从Client文件夹中的Python源代码中获取一个类图,其中包含pyreverse,但它需要__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()
...

Tags: 代码pyself文件夹clientnonetitleinit
2条回答

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

请参阅this SO question关于__init__.py以了解有关该文件的更深入解释

多亏了@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得到的类图:

enter image description here

相关问题 更多 >