Saltycrane logo

SaltyCrane Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

"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, in 
    cProfile.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)

4 Comments — feed icon Comments feed for this post


#1 Luke commented on 2010-01-21:

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.


#2 Eliot commented on 2010-01-21:

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


#3 Hiankun commented on 2011-02-24:

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 2011-03-29:

Thanks Man

Post a comment

Required
Required, but not displayed
Optional

Format using Markdown. (No HTML.)
  • Code blocks: prefix each line by at least 4 spaces or 1 tab (and a blank line before and after)
  • Code span: surround with backticks
  • Blockquotes: prefix lines to be quoted with >
  • Links: <URL>
  • Links w/ description: [description](URL)
Created with Django | Hosted by Slicehost