Python - 使用pycallgraph生成函數關系圖


1- pycallgraph簡介

可用於創建python函數關系圖,依賴於dot命令,需要先安裝 graphviz;
 

2- 安裝pycallgraph

安裝pycallgraph
$ pip3 install pycallgraph  --proxy=10.144.1.10:8080
Collecting pycallgraph
  Using cached https://files.pythonhosted.org/packages/ca/2e/fafa56316bc2c5fbfbda898f964137c8b5ef33a876cb1f35a54ff6afbd60/pycallgraph-1.0.1.tar.gz
Installing collected packages: pycallgraph
  Running setup.py install for pycallgraph ... done
Successfully installed pycallgraph-1.0.1
 
安裝graphviz
graphviz( http://www.graphviz.org/)是一個圖形可視化軟件(Graph Visualization Software),使用dot文件生成關系圖;

3- 運行pycallgraph

3-1 直接在命令行方式運行

$ pycallgraph graphviz -- ./test.py
默認將生成一個名為pycallgraph.png的函數關圖

3-2 使用API在命令行運行

示例-1
$ cat TempTest.py
#! python3
# -*- coding: utf-8 -*-


def testStr(key):
    print("testStr is :", key)

$ cat CallGraphTest.py
#! python3
# -*- coding: utf-8 -*-
from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput

import TempTest


def testNum(key):
    TempTest.testStr(key)
    print("testNum is :", key)


g = GraphvizOutput(output_file=r'./trace.png')
with PyCallGraph(output=g):
    TempTest.testStr("111")
    testNum(222)

$

$ py -3 CallGraphTest.py
testStr is : 111
testStr is : 222
testNum is : 222

$ ls -l
total 14
drwxr-xr-x 1 guowli 1049089    0 May 23 16:07 __pycache__/
-rwxr-xr-x 1 guowli 1049089  350 May 23 15:56 CallGraphTest.py*
-rwxr-xr-x 1 guowli 1049089   92 May 23 15:59 TempTest.py*
-rw-r--r-- 1 guowli 1049089 9141 May 23 16:07 trace.png

$

生成指定名稱的函數關系圖trace.png

 

示例-2

#! python3
# -*- coding: utf-8 -*-
from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
from pycallgraph import Config
from pycallgraph import GlobbingFilter


class Banana:

    def eat(self):
        pass


class Person:

    def __init__(self):
        self.no_bananas()

    def no_bananas(self):
        self.bananas = []

    def add_banana(self, banana):
        self.bananas.append(banana)

    def eat_bananas(self):
        [banana.eat() for banana in self.bananas]
        self.no_bananas()


def main():
    graphviz = GraphvizOutput()
    graphviz.output_file = 'basic.png'
    config = Config()
    config.max_depth = 5  # 控制最大追蹤深度

    with PyCallGraph(output=graphviz, config=config):
        person = Person()
        for a in range(10):
            person.add_banana(Banana())
        person.eat_bananas()


if __name__ == '__main__':
    main()

生成函數關系圖

4- 在Pycharm運行objgraph

需要在Pycharm中設置Graphviz環境變量,否則可能報錯:
'The command "{}" is required to be in your path.'.format(cmd))
pycallgraph.exceptions.PyCallGraphException: The command "dot" is required to be in your
path.
 
設置Pycharm環境變量步驟:
Run --》Edit Configurations...  --》Default --》Python  --》Environment field group: Environment variables --》 ...  --》+  --》
  • Name: PATH
  • Value: C:\Program Files (x86)\Graphviz2.38\bin

5- 參考信息

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM