2015-06-20 89 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

謝謝!!!!它現在有效。 –