2015-06-20 60 views
0

我想在IPython中进行文件分析,生成一些分析统计输出,然后将它传递给一些Python分析GUI工具,如KCachegrind。这是我的代码试图做到这一点。所有代码都在IPython中执行。如何在IPython中运行shell命令? (Python分析GUI工具)

# example function for profiling 
def factorial(n): 
    if n == 0: 
     return 1.0 
    else: 
     return float(n) * factorial(n-1) 

def taylor_sin(n): 
    res = [] 
    for i in range(n): 
     if i % 2 == 1: 
      res.append((-1)**((i-1)/2)/float(factorial(i))) 
     else: 
      res.append(0.0) 
    return res 

# generate cProfile output (the stats, not the text) 
%prun -D prof.out x = taylor_sin(500) 

# do conversion using pyprof2calltree 
# to do it in IPython, add prefix '!' to code to make it Shell-command code 
!pyprof2calltree -i prof.out -o prof.calltree 

现在IPython的打印错误消息:

!pyprof2calltree -i prof.out -o prof.calltree 
/bin/sh: 1: pyprof2calltree: not found 

的就是这句话,我没有添加pyprof2calltree环境路径或类似的东西?如何解决它?

我可以在纯shell命令中完美运行它。但是我不喜欢在IPython和终端之间频繁切换,我只想在IPython中做所有的事情。我知道添加一个前缀!会使代码像shell命令一样运行,但为什么它会将错误提示给我,如上所示?

[email protected]:~/Dropbox/Coding/Python$ pyprof2calltree -i prof.out -o prof.calltree 
writing converted data to: prof.calltree 
[email protected]:~/Dropbox/Coding/Python$ 

IPython与Anaconda py3.4一起安装;操作系统Ubuntu 14.04;通过PIP

回答

1

安装pyprof2calltreeipython例如在pyprof2calltree documentation

>>> from pyprof2calltree import convert, visualize 
>>> visualize('prof.out') 
>>> convert('prof.out', 'prof.calltree') 

或者:

>>> results = %prun -r x = taylor_sin(500) 
>>> visualize(results) 
>>> convert(results, 'prof.calltree') 

您也可以尝试:

>>> %run -m pyprof2calltree -i prof.out -o prof.calltree 
+0

谢谢!!!!它现在有效。 –