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
cProfile.run('my_super_slow_routine()')
Produces this error:
Traceback (most recent call last): File "test_pstats_error.py", line 3, incProfile.run('my_super_slow_routine()') File "/usr/lib/python2.6/cProfile.py", line 36, in run result = prof.print_stats(sort) File "/usr/lib/python2.6/cProfile.py", line 80, in print_stats import pstats ImportError: No module named pstats
Solution:
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)
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: https://bugs.launchpad.net/ubuntu/+source/python-defaults/+bug/123755) - in order to get it installed, I had to enable the multiverse repositories for my Ubuntu install.
Luke, Thanks for pointing that out. Thanks for the bug report link also. I'll update my post.
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »