SaltyCrane Blog — Notes on JavaScript and web development

"ImportError: No module named pstats" error on Ubuntu

Even though the pstats module (used by cProfile) is part of the Python Standard Library, Ubuntu requires installing a separate package because of its non-free license. For more information, see this Ubuntu bug report. (thanks Luke)

Running this on Ubuntu Karmic:

import cProfile
def my_super_slow_routine(): pass'my_super_slow_routine()')

Produces this error:

Traceback (most recent call last):
  File "", line 3, in'my_super_slow_routine()')
  File "/usr/lib/python2.6/", line 36, in run
    result = prof.print_stats(sort)
  File "/usr/lib/python2.6/", line 80, in print_stats
    import pstats
ImportError: No module named pstats


sudo apt-get install python-profiler

Note: the multiverse repository must be enabled via either update-manager > "Settings..." > "Ubuntu Software" or directly editing /etc/apt/sources.list. (thanks Luke)


#1 Luke commented on :

If you're running on stock installs of some of the newer versions of Ubuntu (for example, I'm on 9), you won't be able to just install python-profiler, because it's not in the apt repositories that are enabled by default (this is apparently due to a licensing issue: - in order to get it installed, I had to enable the multiverse repositories for my Ubuntu install.

#2 Eliot commented on :

Luke, Thanks for pointing that out. Thanks for the bug report link also. I'll update my post.

#3 Hiankun commented on :

Thank you for the post. I've just known about cProfile and try my program with it, but encounter the import error about pstats. At first, I tried to install python-stats but got no luck. Then I found your post so that I knew the python-profiler is the right package.

#4 Sunil Shah commented on :

Thanks Man

#5 Adam Parkin commented on :

THANK YOU for this, had I not found this I would've spent the day pulling my hair out trying to figure out why part of the standard library isn't available.

#6 Nima commented on :


#7 John commented on :

Thanks! Still useful info.