python生成函数关系调用图

2,701 阅读1分钟

python生成函数关系调用图

1.下载并安装graphviz,并配置环境变量

下载地址](graphviz.gitlab.io/_pages/Down…

2.安装pycallgraph库

3.使用

代码实例:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/6/29 22:47
# @Author  : ywb
# @Site    : 
# @File    : ywb.py
# @Software: PyCharm


from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
from pycallgraph import Config
from pycallgraph import GlobbingFilter


def before():
    print('开始调用图')


def generate(num):
    return [i**2 for i in range(num)]


def check_is_even(num):
    if num % 2 ==0:
        return True
    return False


def main():
    num_list = generate(8)
    for i in num_list:
        result = check_is_even(i)
        if result:
            print(result)


if __name__ == '__main__':
    config = Config()
    graphviz = GraphvizOutput()
    config.trace_filter = GlobbingFilter(include=['before'])
    graphviz.output_file = 'graph.png'
    with PyCallGraph(output=graphviz, config=config):
        main()

运行:python ywb.py

生成图如下:

graph.png