安装pycallgraph的小坑

第一坑:pip install pycallgraph失败

为了画函数和类的调用图,搜索到pycallgraph 是现成的好轮子,但是安装这个库时遇到问题:

使用 `pip install pycallgraph`失败。反复查询,耽误了时间,这看到这篇文章就解决了。

问题:

使用 `pip install pycallgraph`,但安装失败

(venv_person) PS D:\dev> pip install pycallgraph                
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pycallgraph
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/ca/2e/fafa56316bc2c5fbfbda898f964137c8b5ef33a876cb1
f35a54ff6afbd60/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.

解决:

使用 `pip install pycallgraph2`,安装成功

(venv_person) PS D:\onedark_dev> pip install pycallgraph2
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pycallgraph2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d5/7d/636c4f8359f82c5b7dc022e59f67b1f3aa7a1875fa5e9a
c00e5119daf4bd/pycallgraph2-1.1.3-py2.py3-none-any.whl (30 kB)
Installing collected packages: pycallgraph2
Successfully installed pycallgraph2-1.1.3

第二坑:graphviz添加环境变量后电脑需要重启

在pycharm中运行demo, 报错dot 命令找不到,但是我按照教程安装了graphviz,添加了环境变量,在cmd中使用‘dot -V'是可以运行的,但是pycharm就是死活不承认'dot‘的存在,毙了狗了。

解决:  

重启电脑,即可运行。示例代码:

from pycallgraph2 import PyCallGraph
from pycallgraph2.output import GraphvizOutput
from pycallgraph2 import Config
from pycallgraph2 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()

结果:

安装pycallgraph的小坑_第1张图片

你可能感兴趣的:(python,开发语言)