Profiling Python codes with hotshot and KCachegrind

Stefan van der Walt stefan at
Wed Nov 8 09:20:02 CST 2006

On Wed, Nov 08, 2006 at 01:32:44AM -0700, Fernando Perez wrote:
> Hi all,
> in the past, Arnd Baecker has made a number of very useful posts on
> this matter, and provided some nice utilities to do it.  I now needed
> to profile some fairly complex codes prior to a big optimization push,
> so I went over his material and wrote a little tool to make the whole
> process as painless as possible.  Here it is, hoping others may find
> it useful:

This looks very interesting.  It works for me on simple scripts, but
whenever I include the lines

from numpy.testing import set_local_path

in the input, pycachegrind aborts with

  File "/home/stefan//lib/python2.4/site-packages/numpy/testing/", line 68, in set_local_path
    if f.f_locals['__name__']=='__main__':
KeyError: '__name__'

I guess this is because the script is run in a separate namespace.
I've managed to work around the problem by changing the definition of
'run' to:

def run(code):
    loc = locals()
    loc['__name__'] = '__main__'
    loc['__file__'] = sys.argv[0]

    exec code in locals()


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo

More information about the Numpy-discussion mailing list