在 python 中构建调用图,包括模块和函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13963321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Build a Call graph in python including modules and functions?
提问by JohnnyDH
I have a bunch of scripts to perform a task. And I really need to know the call graph of the project because it is very confusing. I am not able to execute the code because it needs extra HW and SW to do so. However, I need to understand the logic behind it. So, I need to know if there is a tool (which do not require any python file execution) that can build a call graph using the modules instead of the trace or python parser. I have such tools for C but not for python.
Thank you.
我有一堆脚本来执行任务。而且我真的需要知道项目的调用图,因为它很混乱。我无法执行代码,因为它需要额外的硬件和软件才能执行。但是,我需要了解其背后的逻辑。所以,我需要知道是否有一个工具(不需要任何 python 文件执行)可以使用模块而不是跟踪或 python 解析器来构建调用图。我有这样的 C 工具,但没有用于 python。
谢谢你。
回答by Wilduck
In short, no such tool exists. Python is far too dynamic of a language to be able to generate a call graph without executing the code.
简而言之,不存在这样的工具。Python 是一种过于动态的语言,无法在不执行代码的情况下生成调用图。
Here's some code which clearly demonstrates some of the very dynamic features of python:
下面的代码清楚地展示了 python 的一些非常动态的特性:
class my_obj(object):
def __init__(self, item):
self.item = item
def item_to_power(self, power):
return self.item ** power
def strange_power_call(obj):
to_call = "item_to_power"
return getattr(obj, to_call)(4)
a = eval("my" + "_obj" + "(12)")
b = strange_power_call(a)
Note that we're using evalto create an instance of my_objand also using getattrto call one of its methods. These are both methods that would make it extremely difficult to create a static call graph for python. Additionally, there are all sorts of difficult to analyze ways of importing modules.
请注意,我们使用eval来创建实例my_obj并使用getattr调用其方法之一。这两种方法都会使为 python 创建静态调用图变得极其困难。此外,还有各种难以分析的导入模块的方式。
I think your best bet is going to be to sit down with the code base and a pad of paper, and start taking notes by hand. This will have the dual benefit of making you more familiar with the code base, and will not be easily tricked by difficult to parse scenarios.
我认为你最好的选择是坐下来处理代码库和一张纸,然后开始手工做笔记。这将具有让您更加熟悉代码库的双重好处,并且不会轻易被难以解析的场景所欺骗。
回答by vkontori
You might want to check out pycallgraph:
您可能想查看 pycallgraph:
Also in this link a more manual approach is described:
同样在此链接中,还描述了一种更手动的方法:
generating-call-graphs-for-understanding-and-refactoring-python-code
回答by David Fraser
The best tool I've found is called pyan, and was originally writtenby Edmund Horner, improved by him, and then given colorizationand other features by Juha Jeronen. That version has useful commandline options:
我发现的最好的工具叫做pyan,最初由Edmund Horner编写,由他改进,然后由Juha Jeronen赋予着色和其他功能。该版本具有有用的命令行选项:
Usage: pyan.py FILENAME... [--dot|--tgf]
Analyse one or more Python source files and generate an approximate call graph
of the modules, classes and functions within them.
Options:
-h, --help show this help message and exit
--dot output in GraphViz dot format
--tgf output in Trivial Graph Format
-v, --verbose verbose output
-d, --defines add edges for 'defines' relationships [default]
-n, --no-defines do not add edges for 'defines' relationships
-u, --uses add edges for 'uses' relationships [default]
-N, --no-uses do not add edges for 'uses' relationships
-c, --colored color nodes according to namespace [dot only]
-g, --grouped group nodes (create subgraphs) according to namespace
[dot only]
-e, --nested-groups create nested groups (subgraphs) for nested namespaces
(implies -g) [dot only]
Here's the result of running pyan.py --dot -c -e pyan.py | fdp -Tpng:
这是运行的结果pyan.py --dot -c -e pyan.py | fdp -Tpng:
Edmund Horner's original code is now best found in his github repository, and somebody has also made a repository with both versions, from where you can download Juha Jeronen's version. I've made a clean version combining their contributions into my own repository just for pyan, since both repositories have lots of other software.
Edmund Horner 的原始代码现在最好在他的 github 存储库中找到,并且有人还制作了两个版本的存储库,您可以从那里下载 Juha Jeronen 的版本。我制作了一个干净的版本,将他们的贡献合并到我自己的存储库中,仅用于 pyan,因为两个存储库都有很多其他软件。
回答by codeslord
SourceTrail will help you here. https://www.sourcetrail.com/
SourceTrail 将在这里为您提供帮助。https://www.sourcetrail.com/
Sourcetrail is a free and open-source cross-platform source explorer that helps you get productive on unfamiliar source code. Supports C, C++, Java and Python
Sourcetrail 是一个免费的开源跨平台源代码浏览器,可帮助您在不熟悉的源代码上提高工作效率。支持 C、C++、Java 和 Python
https://github.com/CoatiSoftware/Sourcetrail
https://github.com/CoatiSoftware/Sourcetrail
Here is a link to the documentation
这是文档的链接
https://www.sourcetrail.com/documentation/
https://www.sourcetrail.com/documentation/
Please note that Python support is relatively new, so please don't expect it to work perfectly yet.
请注意,Python 支持相对较新,因此请不要指望它可以完美运行。


