python 生成函数图_【Python】利用graphviz和pycallgraph库自动生成Python函数调用关系图...

一、下载并安装graphviz

因为这个模块依赖Graphviz2.38这个软件,这个贝尔实验室大牛为画图提供的一个命令行工具。

下载地址:https://graphviz.gitlab.io/_pages/Download/windows/graphviz-2.38.msi

安装(路径可任意选择)

配置环境变量,在Path中添加graphviz的bin目录所在路径。

例如,我安装在F:\Program Files (x86)

那么就添加环境变量F:\Program Files (x86)\Graphviz2.38\bin

二、安装pycallgraph库

pip install pycallgraph

三、使用

注意:只有你的操作使用了某个函数,才能显示在流图中。用户没有调用的函数则不会出现在流图中。

(所以,使用的时候要考虑一下你此次调用的“测试覆盖率”,尽可能全面的调用到每一个函数)

生成的图片示例

8dac1a103c10442c80db12accde45196.jpg

代码示例 1:在普通python程序中使用

# -*- coding: utf-8 -*-

from pycallgraph import Config

from pycallgraph import PyCallGraph

from pycallgraph.output import GraphvizOutput

from werkzeug.datastructures import ImmutableMultiDict

from app.main.views import do_all_copy

def main():

# do something...

if __name__ == "__main__":

config = Config()

# 关系图中包括(include)哪些函数名。

# 如果是某一类的函数,例如类gobang,则可以直接写'gobang.*',表示以gobang.开头的所有函数。(利用正则表达式)。

# config.trace_filter = GlobbingFilter(include=[

# 'draw_chessboard',

# 'draw_chessman',

# 'draw_chessboard_with_chessman',

# 'choose_save',

# 'choose_turn',

# 'choose_mode',

# 'choose_button',

# 'save_chess',

# 'load_chess',

# 'play_chess',

# 'pop_window',

# 'tip',

# 'get_score',

# 'max_score',

# 'win',

# 'key_control'

# ])

# 该段作用是关系图中不包括(exclude)哪些函数。(正则表达式规则)

# config.trace_filter = GlobbingFilter(exclude=[

# 'pycallgraph.*',

# '*.secret_function',

# 'FileFinder.*',

# 'ModuleLockManager.*',

# 'SourceFilLoader.*'

# ])

graphviz = GraphvizOutput()

graphviz.output_file = 'graph.png'

with PyCallGraph(output=graphviz, config=config):

main()

代码示例 2:在Flask中使用

如果你开发的是web应用,同样可以使用这个库。

你只需要在你希望监测的函数中,添加如下代码:

# 添加必要的import

from pycallgraph import Config

from pycallgraph import PyCallGraph

from pycallgraph.output import GraphvizOutput

@main.route('/example_mapping/', methods=[ 'GET','POST'])

@login_required

def example_mapping():

config = Config()

graphviz = GraphvizOutput()

graphviz.output_file = 'graph.png'

with PyCallGraph(output=graphviz, config=config):

# 这里写原有的函数功能

# do something...

return render_template('example_mapping.html', jsondata=json_last, timejson=timejson)

然后访问你的web服务页面,http://127.0.0.1:5000/example_mapping/即可。经过测试,此过程可能会比以往更慢一些。

附:报错找不到dot的解决方式:

遇到错误 pycallgraph.exceptions.PyCallGraphException: The command “dot” is required to be in your path.

b1b82cea423f41e4939cad631d33c75d.jpg

解决方式

You need to find dot.exe, which for me was in C:\Program Files (x86)\Graphviz2.38\bin so I went to the following: control panel > system > advanced system settings > Environment Variables... and then in the bottom box for System Variables, find Path, select it and select edit, then select new and paste the path in. Now close and reopen cmd.exe and see simple type in ‘dot’ and hit enter. If there’s no error, the path was setup properly.

简单来说,就是你需要将C:\Program Files (x86)\Graphviz2.38\bin添加到环境变量的Path中,然后在cmd里面尝试运行dot这个命令,如果不报错,就可以正常使用了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值