How to run shell-commands in IPython? (Python Profiling GUI tools)

326 views Asked by At

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. Here is my code trying to do that. All codes are executed in 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:

!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? How to solve it?

I can run it perfectly in pure shell command. But I don't like to switch frequently between IPython and a terminal, and I want to do all stuff just in 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?

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; Operating System Ubuntu 14.04; pyprof2calltree installed via pip

1

There are 1 answers

0
Vincent On BEST ANSWER

From the ipython example in the pyprof2calltree documentation:

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