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?
Okay, this is much easier to understand in answer form, so I’ll move what I would write in my comment to this answer.
Luckily for you, you didn’t overwrite the system version of python, as Ubuntu 11.10 comes with 2.7.2 preinstalled.
Your python binaries (
pythonandpython2.7) are located in/usr/local/bin, which is a directory where user-specific stuff is usually installed. This is fine, it means your system python is still there.First, just try to run the system python. Type this from the command line:
This should print out something like this:
If so, means you’re fine.
So you just have to fix your
PATH, which tells the shell where to find commands./usr/local/binis going to have priority over/usr/local, so there are some ways to fix this, in order of difficulty/annoyance/altering your system:Remove the symlink
pythonfrom/usr/local/binThis will make it so that when you type
python, it should go back to executing/usr/bin/python, which is an alias for the system’s python 2.7.2.Move
/usr/binto have higher precedence in thePATHMight not be desirable if you already have stuff in
/usr/local/binthat should have precedence over/usr/bin, but I’m adding this for completeness.In your shell profile (not sure what Ubuntu’s default is, but I’m using
~/.bash_profile, you can do this:Remove your python install
This is extreme and the first option I presented should be your first option.
Do you really need your own version of Python? If you want isolated python environments you probably really want
virtualenv. You can probably remove yours unless there’s a reason not to.It’s going to be a little annoying though, but basically:
pythonandpython2.7andpythonwandpythonw2.7commands from/usr/local/bin./usr/local/lib/python/2.7.2This part is not complete because I forget what else there is.