[英]How to run shell-commands in IPython? (Python Profiling GUI tools)
I am trying to do file profiling in IPython, generate some profiling stats output and then pass it to some Python profiling GUI tools like KCachegrind. 我正在尝试在IPython中进行文件分析,生成一些分析统计数据输出,然后将其传递给某些Python分析GUI工具,例如KCachegrind。 Here is my code trying to do that.
这是我的代码试图做到这一点。 All codes are executed in IPython.
所有代码都在IPython中执行。
# 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
And now IPython prints an error message: 现在,IPython打印一条错误消息:
!pyprof2calltree -i prof.out -o prof.calltree
/bin/sh: 1: pyprof2calltree: not found
Is this saying that I haven't add pyprof2calltree
to environment path or something like that? 这是否表示我还没有将
pyprof2calltree
添加到环境路径或类似的东西? How to solve it? 怎么解决呢?
I can run it perfectly in pure shell command. 我可以在纯shell命令中完美地运行它。 But I don't like to switch frequently between IPython and a terminal, and I want to do all stuff just in IPython.
但是我不喜欢在IPython和终端之间频繁切换,我只想在IPython中做所有事情。 I understand adding a prefix
!
我知道添加前缀
!
would make codes run like in shell command, but why it raise the error to me as shown above? 会使代码像在shell命令中一样运行,但是为什么如上所述会向我引发错误?
Jian@Home-PC:~/Dropbox/Coding/Python$ pyprof2calltree -i prof.out -o prof.calltree
writing converted data to: prof.calltree
Jian@Home-PC:~/Dropbox/Coding/Python$
IPython is installed with Anaconda py3.4; IPython与Anaconda py3.4一起安装; Operating System Ubuntu 14.04;
操作系统Ubuntu 14.04;
pyprof2calltree
installed via pip pyprof2calltree
通过pip安装
From the ipython
example in the pyprof2calltree documentation : 从pyprof2calltree文档中的
ipython
示例中:
>>> from pyprof2calltree import convert, visualize
>>> visualize('prof.out')
>>> convert('prof.out', 'prof.calltree')
Or: 要么:
>>> results = %prun -r x = taylor_sin(500)
>>> visualize(results)
>>> convert(results, 'prof.calltree')
You could also try: 您也可以尝试:
>>> %run -m pyprof2calltree -i prof.out -o prof.calltree
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.