如何在IPython中运行shell命令? (Python分析GUI工具)

时间:2015-06-20 08:42:33

标签: python shell profiling ipython ipython-magic

我正在尝试在IPython中进行文件分析,生成一些分析统计信息输出,然后将其传递给一些Python分析GUI工具,如KCachegrind。这是我的代码试图这样做。所有代码都在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

现在IPython会输出一条错误消息:

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

这是说我还没有pyprof2calltree添加到环境路径或类似的东西?怎么解决?

我可以在纯shell命令中完美运行它。但我不想经常在IPython和终端之间切换,我想在IPython中做所有的事情。我知道添加前缀!会使代码像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与Anaconda py3.4一起安装;操作系统Ubuntu 14.04;通过pip

安装pyprof2calltree

1 个答案:

答案 0 :(得分:1)

来自pyprof2calltree documentation中的ipython示例:

>>> 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