ImportError: No module named bz2 for Python 2.7.2

2019-01-06 13:37发布

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?

10条回答
我想做一个坏孩纸
2楼-- · 2019-01-06 14:18

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

Worked fine for me...

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-06 14:19

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.

查看更多
你好瞎i
4楼-- · 2019-01-06 14:19

For Ubuntu/Debian:

sudo apt-get install libbz2-dev

For Fedora:

sudo yum install bzip2-devel

And then recompile the python and install it.

查看更多
虎瘦雄心在
5楼-- · 2019-01-06 14:22

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
查看更多
仙女界的扛把子
6楼-- · 2019-01-06 14:29

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/
查看更多
Melony?
7楼-- · 2019-01-06 14:29

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/
查看更多
登录 后发表回答