ImportError: No module named bz2 for Python 2.7.2

Posted on Dec 18, 2022

Question

I'm using Python 2.7.2 on Ubuntu 11.10. I got this error when importing the bz2 module:

ImportError: No module named bz2

I thought the bz2 module is supposed to come with Python 2.7. How can I fix this problem?

EDIT: I think I previously installed Python 2.7.2 by compiling from source. Probably at that point I didn't have libbz2-dev and so the bz2 module is not installed. Now, I'm hoping to install Python2.7 through

sudo apt-get install python2.7

But it will say it's already installed. Is there a way to uninstall the previous Python2.7 installation and reinstall?

Answer

I meet the same problem, here's my solution.

The reason of import error is while you are building python, system couldn't find the bz2 headers and skipped building bz2 module.

Install them on Ubuntu/Debian:

sudo apt-get install libbz2-dev

Fedora:

sudo yum install bzip2-devel

and then rebuild python

comes from another answer

@birryree's answer helps to back to the system's original python.