查看项目源码时,需要理清调用关系才能更好理解其中逻辑。最近发现一个库pycallgraph
,可以较为方便的自动生成调用关系图。
安装
安装graphviz
yum install graphviz
安装pycallgraph
[app@ip demo]$ pip install pycallgraph
Looking in indexes: http://xxxxxx/simple
Collecting pycallgraph
Using cached http://xxxxxx/packages/ca/2e/fafa56316bc2c5fbfbda898f964137c8b5ef33a876cb1f35a54ff6afbd60/pycallgraph-1.0.1.tar.gz (36 kB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [1 lines of output]
error in pycallgraph setup command: use_2to3 is invalid.
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
如果出现上述错误,需升级setuptools版本到57.5.0
pip install setuptools==57.5.0
编写代码测试一下
def a():
c()
return b()
def b():
return c()
def c():
return 1
if __name__ == "__main__":
from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
graphviz = GraphvizOutput()
graphviz.output_file = 'graph.png'
with PyCallGraph(output=graphviz):
r = a()
运行后生成一个文件graph.png