"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
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)
Comments
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.
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.
Thanks Man
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.
Thanks.
Thanks! Still useful info.