缺少 python bz2模块

我已经安装在我的主目录。

[spatel@~ dev1]$ /home/spatel/python-2.7.3/bin/python -V
Python 2.7.3

我试图运行一个脚本,需要 python 2.7.x 版本,我得到了遗漏 bz2错误

[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

我尝试安装 bz2模块,但我得到了很多错误

 [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
169946 次浏览

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.

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.

On CentOS 7, install bzip2-devel:

sudo yum install  bzip2-devel

Then re-compile python.

You must reinstall bzip2 by source code:

  1. yum install bzip2-devel

  2. wget http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz

  3. tar -zxvf bzip2-1.0.6.tar.gz

  4. cd bzip2-1.0.6

  5. make && make install

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

If you python install on a specific location, just install libbz2-dev would not work.

There is a workaround for centos:

  • 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 would need replace that if you have custom python path.

I should also add that on CentOS 6, make sure you have bzip2-devel, not bzip2-libs installed.

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:

  1. yum install bzip2-devel. (or apt-get)
  2. download bzip2-1.0.6. make && make install ()
  3. 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.

Here is my solution on CentOS: (step 2-6 may skip)

  1. sudo yum install bzip2-devel

  2. download bzip2-1.0.6.tar.gz from https://github.com/nemequ/bzip2/releases

  3. tar -zxvf bzip2-1.0.6.tar.gz
  4. cd bzip2-1.0.6
  5. make && make install
  6. download file from https://github.com/Imaginashion/ccdev-vision/blob/master/.fr-d0BNfn/django-jquery-file-upload/venv/lib/python3.5/lib-dynload/_bz2.cpython-35m-x86_64-linux-gnu.so and move it to /[your python path]/lib-dynload/_bz2.cpython-35m-x86_64-linux-gnu.so
  7. credit to https://michaelheap.com/error-while-loading-shared-libraries-libbz2-so-1-0-cannot-open-shared-object-file-on-centos-7

I also have this problem when installing Python from a different location (I use Python 3.7.5 on Centos 7).

Here are steps that I make it be able to work:

  1. Export environment variables
export PATH=<YOUR_PYTHON_PATH>/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
export PYTHONPATH=<YOUR_PYTHON_PATH>/lib/python3.7/site-packages
export LD_RUN_PATH=/usr/local/lib:/usr/lib64
export LD_LIBRARY_PATH=/usr/local/lib:/usr/lib64
export CFLAGS="-I/usr/include"
export LDFLAGS="-L/usr/lib64"
  1. Install bzip2-devel
yum install -y bzip2-devel
  1. Download & compile bzip2
wget/curl <bzip2_url>
make
make install

By doing this, you don't need to download the _bz2.cpython-35m-x86_64-linux-gnu.so file.

I had this happen for python 3.8.2 when importing pandas: import pandas as pd

resulted in a long error message ending with: "error: ModuleNotFoundError: No module named '_bz2'"

This was resolved by doing the following 2 bash commands:

sudo apt-get install libbz2-dev
sudo cp /usr/lib/python3.8/lib-dynload/_bz2.cpython-38-x86_64-linux-gnu.so  /usr/local/lib/python3.8/

Then it worked fine.

It is happening because of a .so file being missing.

Say for python3.7 download the file from: _bz2.cpython-37m-x86_64-linux-gnu.so

For different versions of python try finding this file for your version. Say for python3.8 change 37 to 38 etc. and find and download the file.

Now for Ubuntu: copy the file inside /usr/local/lib/python3.7 folder using sudo privilege.

To do this, go to the folder where the file is downloaded and execute the command (change your filename and destination folder based on your python versions accordingly):

sudo cp _bz2.cpython-37m-x86_64-linux-gnu.so /usr/local/lib/python3.7

Finally download python, extract the zip file and after extraction configure and compile it:

./configure --enable-optimizations
sudo make altinstall

It is only happening in Jupyter when importing pandas for me.

My fix was to copy the contents of /usr/lib/python3.8/lib-dynload (including _bz2.cpython-38-x86_64-linux-gnu.so) to ~/.local/lib/python3.8/site-packages/.

I had the same problem on debian stretch with a locally compiled python 3.6.9 In /usr/local/lib/python3.6/lib-dynload/, there was a _bz2.cpython-365m-x86_64-linux-gnu.so file (note the '365m' part...) I created the symlinks to this lib, and it solved the problem :

sudo ln -s _bz2.cpython-365m-x86_64-linux-gnu.so _bz2.cpython-369m-x86_64-linux-gnu.so
sudo ln -s _bz2.cpython-365m-x86_64-linux-gnu.so _bz2.cpython-36m-x86_64-linux-gnu.so

I have also got this annoying output and fixed that error. I got that error actually in Python 3.7 and 3.8. I didn't even have sudo privileges in my remote server but I managed to fix that error by downgrading Python. Installing Python 3.6 instead of 3.7 or 3.8 solves the problem.

In my case I got this error when importing pandas. Installing python 3.9.1 solved the problem.

My initial python version was 3.8.6. I was using PyEnv and running MacOS Big Sur.

Initially:

$ python
Python 3.8.6 (default, Nov 21 2020, 02:39:42)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
Traceback (most recent call last):
...
from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'

Installed python 3.9.1:

$ pyenv install --list
$ pyenv install 3.9.1
$ pyenv local 3.9.1
$ pyenv global 3.9.1
$ pip install pandas

Running again:

$  python
Python 3.9.1 (default, Jul  5 2021, 22:26:09)
[Clang 12.0.5 (clang-1205.0.22.11)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>>

I fixed it as below

  1. # sudo find / -name '*_bz2*'

search result sample:

/usr/lib64/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so

  1. # sudo cp /usr/lib64/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so /usr/local/python3.8.5/lib/python3.8/lib-dynload
  2. sudo mv _bz2.cpython-36m-x86_64-linux-gnu.so _bz2.cpython-38-x86_64-linux-gnu.so

if your python is 3.7, you should change the file name from 36m to 37m.