38

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?

Nipun Thennakoon
  • 3,586
  • 1
  • 18
  • 26
ushadow
  • 565
  • 1
  • 6
  • 11
  • 1
    http://stackoverflow.com/questions/812781/pythons-bz2-module-not-compiled-by-default – Rafe Kettler Nov 13 '11 at 22:37
  • 1
    If you installed Python 2.7 from source, it shouldn't have registered with the package manager as being installed (unless you installed it from source via the package manager). Ubuntu comes with Python 2.7.2 installed by default, so whatever you did might have masked that version of python in your path. Can you tell us the output of the commands: `which python`, `which python 2.7`? If you did install over your previous python install (with `--prefix` or just letting it go), then that's a trickier problem to fix. – wkl Nov 13 '11 at 23:19
  • 1
    @birryree: `which python`: `/usr/local/bin/python`, `which python 2.7`: `/usr/local/bin/python` – ushadow Nov 13 '11 at 23:22
  • @ushadow Check my answer, you can just go back to using the system installed python 2.7.2. – wkl Nov 13 '11 at 23:35
  • To anyone reading this in the future, as a side-note, NEVER EVER try to install another python onto the system python. You will likely break a ton of dependencies by doing so. Always use some isolation tool to isolate python and/or virtual environments. – Joe May 02 '14 at 01:57

10 Answers10

47

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.

Community
  • 1
  • 1
arbel
  • 470
  • 1
  • 4
  • 4
27

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 (python and python2.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:

/usr/bin/python -c "import bz2; print bz2.__doc__"

This should print out something like this:

λ > /usr/bin/python -c "import bz2; print bz2.__doc__"

The python bz2 module provides a comprehensive interface for
the bz2 compression library. It implements a complete file
interface, one shot (de)compression functions, and types for
sequential (de)compression.

If so, means you're fine.

So you just have to fix your PATH, which tells the shell where to find commands. /usr/local/bin is 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 python from /usr/local/bin

This 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.

sudo rm /usr/local/bin/python

Move /usr/bin to have higher precedence in the PATH

Might not be desirable if you already have stuff in /usr/local/bin that 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:

export PATH=/usr/bin:$PATH

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:

  • Remove the python and python2.7 and pythonw and pythonw2.7 commands from /usr/local/bin.
  • Remove /usr/local/lib/python/2.7.2

This part is not complete because I forget what else there is.

wkl
  • 77,184
  • 16
  • 165
  • 176
  • Thanks, @birryree! It works! I removed my python installation in `/usr/local/bin` and `usr/local/lib` because it's really unnecessary. – ushadow Nov 13 '11 at 23:58
  • 1
    Nice, thorough, and very instructive answer. I would like to add that while `virtualenv` allows for isolated environments, `pythonbrew` and `pythonz` (A fork of `pythonbrew`) allow for running different major versions of Python. 1.5, 2.2, PyPy, Jython, you name it. They also come with `virtualenv` builtin. – Ehtesh Choudhury Aug 22 '12 at 00:43
  • I tried "sudo rm /usr/local/bin/python", but then got an error later in the node installation, "/usr/local/bin/python not found". So I had to return it and make it a link to "/usr/bin/python". – Erel Segal-Halevi May 29 '13 at 06:29
  • You might not want to replace the system version of Python. This can break things like yum or AWS tools on ec2 instances. – danieljimenez Oct 10 '13 at 16:24
  • @danieljimenez I never recommended removing the system version of Python. All my instructions were about removing references to the version OP installed herself. – wkl Oct 10 '13 at 17:58
24

In case, you must be used python2.7, you should run: (Centos 6.4)

sudo cp /usr/lib64/python2.6/lib-dynload/bz2.so /usr/local/lib/python2.7/
CK.Nguyen
  • 1,258
  • 17
  • 16
8

Maybe it will helps someone:

apt-get install libbz2-dev # for bz2
apt-get install libssl-dev # for _ssl
apt-get install libsqlite3-dev # for sqlite
apt-get install libreadline6-dev # for readline,  _curses,  _curses_panel
Oleg Neumyvakin
  • 9,706
  • 3
  • 58
  • 62
  • 3
    add `apt-get install ncurses-dev` to the list. otherwise, works for build Python 2.7.5 from source on Ubuntu 12.04 LTS – oberstet Oct 22 '13 at 08:14
5

For Ubuntu/Debian:

sudo apt-get install libbz2-dev

For Fedora:

sudo yum install bzip2-devel

And then recompile the python and install it.

vahid abdi
  • 9,636
  • 4
  • 29
  • 35
yfliu
  • 61
  • 1
  • 2
3

matocnhoi's answer works for me in centOS

sudo cp /usr/lib64/python2.6/lib-dynload/bz2.so /usr/local/lib/python2.7/

and I used virtualenv, so the command is

sudo cp /usr/lib64/python2.6/lib-dynload/bz2.so ../../../env/lib/python2.7/
田大龙
  • 31
  • 1
2

I used a symlink between /usr/lib64/python2.6/lib-dynload/bz2.so /usr/local/lib/python2.7/lib-dynload/

Worked fine for me...

phildobbin
  • 814
  • 8
  • 9
1

Make sure you bz2 installed, run sudo yum install bzip2-devel.

  • Centos 6

    sudo cp /usr/lib64/python2.6/lib-dynload/bz2.so /python_install_path/lib/python2.7
    
  • Centos 7

    sudo cp /usr/lib64/python2.7/lib-dynload/bz2.so /python_install_path/lib/python2.7
    

python_install_path usually is /usr/local/lib/python2.7/, you need replace that if you install python in a another path.

Mithril
  • 12,947
  • 18
  • 102
  • 153
1

If your bz2 in /usr/lib64/python2.7/lib-dynload/ is named as: "bz2.x86_64-linux-gnu.so", remember to rename it to bz2.so when copying it to your path or it may not be correctly sourced:

cp /usr/lib64/python2.6/lib-dynload/bz2.x86_64-linux-gnu.so /python_install_path/lib/python2.7/bz2.so
Weiyu Cheng
  • 261
  • 3
  • 5
0

I had the same problem with Python 2.17.15 and pyenv on Ubuntu. System python from /usr/bin/python worked fine. In my case it helped to install libbz2-dev and then to reinstall python 2.7.15:

sudo apt-get install libbz2-dev
pyenv uninstall 2.7.15
pyenv install 2.7.15
Vladimir Gorovoy
  • 2,259
  • 1
  • 10
  • 3