可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have installed at my home directory.
[spatel@~ dev1]$ /home/spatel/python-2.7.3/bin/python -V
Python 2.7.3
I am trying to run one script which required python 2.7.x version, and i am getting missing bz2 error
[spatel@~ dev1]$ ./import_logs.py
Traceback (most recent call last):
File "./import_logs.py", line 13, in <module>
import bz2
ImportError: No module named bz2
I have tried to install bz2 module but i got lots of error
[spatel@dev1 python-bz2-1.1]$ /home/spatel/python-2.7.3/bin/python setup.py install
...
...
...
bz2.c:1765: error: âBZ_FINISH_OKâ undeclared (first use in this function)
bz2.c:1765: warning: comparison between pointer and integer
bz2.c:1771: error: âPyMemberDefâ has no member named âavail_outâ
bz2.c:1778: error: âPyMemberDefâ has no member named ânext_outâ
bz2.c:1778: error: âPyMemberDefâ has no member named âtotal_out_hi32â
bz2.c:1778: error: âPyMemberDefâ has no member named âtotal_out_lo32â
bz2.c:1778: error: invalid operands to binary +
bz2.c:1778: warning: statement with no effect
bz2.c:1779: error: âPyMemberDefâ has no member named âavail_outâ
bz2.c:1779: error: âPyMemberDefâ has no member named ânext_outâ
bz2.c:1779: error: invalid operands to binary -
bz2.c:1779: error: invalid operands to binary -
bz2.c:1779: warning: statement with no effect
bz2.c:1783: error: âPyMemberDefâ has no member named âavail_outâ
bz2.c:1784: error: âPyMemberDefâ has no member named âtotal_out_hi32â
bz2.c:1784: error: âPyMemberDefâ has no member named âtotal_out_lo32â
bz2.c:1784: warning: passing argument 2 of â_PyString_Resizeâ makes integer from pointer without a cast
error: command 'gcc' failed with exit status 1
回答1:
Probably as you built python from source, you don't have bz2 headers.
Install them on Ubuntu/Debian:
sudo apt-get install libbz2-dev
Fedora:
sudo yum install bzip2-devel
And build python again.
You may notice that python checks for lots of libraries when configuring/building, if you miss some of them you probably will get no support for libs like bz2 on your case.
You should get prebuild binaries to avoid this kind of stuff. Ubuntu 12.04 packs python 2.7.3, the version your script needs.
回答2:
If you python install on a specific location, just install libbz2-dev
would not work.
There is a workaround for centos:
python_install_path
usually is /usr/local/lib/python2.7/
, you would need replace that if you have custom python path.
回答3:
On CentOS 7, install bzip2-devel:
sudo yum install bzip2-devel
Then re-compile python.
回答4:
You need to have the development version of the bz2 c library installed. You probably don't and that's why it wasn't installed when you built your user copy of python. On Ubuntu it's the libbz2-dev package. It's probably named the same or similar on Fedora. Or you can download it from www.bzip.org.
回答5:
You must reinstall bzip2
by source code:
yum install bzip2-devel
wget http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz
tar -zxvf bzip2-1.0.6.tar.gz
cd bzip2-1.0.6
make && make install
configure and re compile python
those steps working sometimes.
Finally, I have figured out the problem, it needs the /usr/local/Python-3.5.2/lib/python3.5/lib-dynload/_bz2.cpython-35m-x86_64-linux-gnu.so , it must have a problem when I compile bzip2 by source code. I copy this file from another VM to solve the problem.
回答6:
I should also add that on CentOS 6, make sure you have bzip2-devel
, not bzip2-libs
installed.
回答7:
the solution above can solve bz2 problems with python2.7. but not python 3.x
yeah, you need _bz2.cpython-3xm-x86_64-linux-gnu.so, however you should build it in your own env.
here's my solution:
- yum install bzip2-devel. (or apt-get)
- download bzip2-1.0.6. make && make install ()
- build Python3's _bz2.cpython like this:
vim run.sh under python3's source code folder:Python-3.x.x
export CFLAGS="-I/usr/include"
export LDFLAGS="-L/usr/lib64"
export LD_LIBRARY_PATH=/usr/lib64
make distclean
./configure --prefix=/home/xxx/Python3 && make && make install
you can set prefix the same of your pre version, that will not uninstrall any package you installed. And before that, make a backup folder.