如何查看Python函数调用图 Ubuntu

来源:互联网 发布:python bs4 下载 编辑:程序博客网 时间:2024/05/17 03:24

说明:有时候想看看Python的函数调用图,此时  pycallgraph  就显示出他的用途了。

  • 安装 pycallgraph
       pip install pycallgraph
  • 安装 graphviz,使用dot -v 验证安装,记得将 /usr/bin/ 设置到 PATH路径
           pip install graphviz       dot -v

这个会安装一个dot 库,不然会报如下错误

 'The command "{}" is required to be in your path.'.format(cmd))pycallgraph.exceptions.PyCallGraphException: The command "dot" is required to be in your path. 

实例:

【downloadmusic.py】

import urllib2import threadingdef download(url, path):    data = urllib2.urlopen(url).read()    open(path, 'wb').write(data)    print "success!" 


【testpycall.py】

from pycallgraph import PyCallGraphfrom pycallgraph import Configfrom pycallgraph.output import GraphvizOutputfrom downloadmusic import *graphviz = GraphvizOutput(output_file=r'trace_detail.png')with PyCallGraph(output=graphviz):    download('http://www.baidu.com/img/bd_logo1.png', r'baidu.png')


终端执行  python  testpycall.py,就会在当前路径生成 trace_detail.png 图,打开截取部分如下:


参考文献

pycallgraph  window使用

Python call graph官网

pycallgraph with pycharm does not work



原创粉丝点击