使用pycallgraph分析python代码调用关系

查看项目源码时,需要理清调用关系才能更好理解其中逻辑。最近发现一个库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
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值