Satish
Satish

Reputation: 17437

missing python bz2 module

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

Upvotes: 138

Views: 264268

Answers (19)

Shady Smaoui
Shady Smaoui

Reputation: 1285

If you're building python from source in Ubuntu, I'll help you saving two hours of trial error.

Below the needed package for your image to work properly

RUN apt-get update && \
    apt-get install -y \
    build-essential \
    libssl-dev \
    zlib1g-dev \
    libbz2-dev \
    libncurses5-dev \
    libgdbm-dev \
    libnss3-dev \
    libsqlite3-dev \
    libreadline-dev \
    libffi-dev \

Upvotes: 0

Gaurav Pandey
Gaurav Pandey

Reputation: 21

I faced the same issue with python3.11 and then installing pandas in it. To fix this we not only need to install the library using sudo in system but also install it in virtual env that we are already using

sudo apt-get install libbz2-dev
sudo cp /usr/lib/python3.11/lib-dynload/_bz2.cpython-311-x86_64-linux-gnu.so  venv/lib/python3.11/site-packages/

Upvotes: 0

yuchi
yuchi

Reputation: 41

I'm using ubuntu22.04 and poetry. In my case, the following commands worked:

sudo apt-get install libbz2-dev
cp /usr/lib/python3.11/lib-dynload/_bz2.cpython-311-x86_64-linux-gnu.so <poetry-virtualenv-path>/lib/python3.11/site-package

You can check the poetry virtualenv path with the following:

$ poetry env info

Virtualenv
Python:         3.11.4  
Implementation: CPython  
Path:           /home/username/.cache/pypoetry/virtualenvs/server-FhuNspaF-py3.11
Executable:     /home/username/.cache/pypoetry/virtualenvs/server-FhuNspaF-py3.11/bin/python
Valid:          True  

Upvotes: 0

user9869932
user9869932

Reputation: 7337

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

Upvotes: 6

bluetata
bluetata

Reputation: 654

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.

Upvotes: 8

Guru Bhandari
Guru Bhandari

Reputation: 119

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.

Upvotes: 0

Jele Reteste
Jele Reteste

Reputation: 1

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

Upvotes: 0

Jalal Mostafa
Jalal Mostafa

Reputation: 1024

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

Upvotes: 1

hafiz031
hafiz031

Reputation: 2680

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

Upvotes: 5

BryanMinorPhD
BryanMinorPhD

Reputation: 839

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.

Upvotes: 69

Binh Le
Binh Le

Reputation: 91

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.

Upvotes: 3

chi doujiao
chi doujiao

Reputation: 1

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. sudo ln -s `find /usr/lib64/ -type f -name "libbz2.so.1*"` /usr/lib64/libbz2.so.1.0 credit to https://michaelheap.com/error-while-loading-shared-libraries-libbz2-so-1-0-cannot-open-shared-object-file-on-centos-7

Upvotes: 0

Jingjia Luo
Jingjia Luo

Reputation: 61

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.

Upvotes: 6

trinth
trinth

Reputation: 6047

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

Upvotes: 2

lvxiaobo616
lvxiaobo616

Reputation: 49

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.

Upvotes: 4

jviotti
jviotti

Reputation: 18929

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.

Upvotes: 222

Mithril
Mithril

Reputation: 13758

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.

Upvotes: 8

James Tang
James Tang

Reputation: 611

On CentOS 7, install bzip2-devel:

sudo yum install  bzip2-devel

Then re-compile python.

Upvotes: 16

AFoglia
AFoglia

Reputation: 8128

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.

Upvotes: 3

Related Questions