安装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()

结果:

  • 13
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值