I have installed at my home directory.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3744475643525b77">[email protected]</a>~ 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
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92e1e2f3e6f7fed2">[email protected]</a>~ 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
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f7c7f6e7b6a634f6b6a793e">[email protected]</a> 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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
Method 2
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.
Method 3
On CentOS 7, install bzip2-devel:
sudo yum install bzip2-devel
Then re-compile python.
Method 4
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.
Method 5
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.
Method 6
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
>>>
Method 7
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.
Method 8
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
Method 9
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.
Method 10
I should also add that on CentOS 6, make sure you have bzip2-devel, not bzip2-libs installed.
Method 11
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:
- 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"
- Install bzip2-devel
yum install -y bzip2-devel
- 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.
Method 12
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/.
Method 13
I fixed it as below
# sudo find / -name '*_bz2*'
search result sample:
/usr/lib64/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so
# 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-dynloadsudo 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.
Method 14
Here is my solution on CentOS: (step 2-6 may skip)
-
sudo yum install bzip2-devel -
download
bzip2-1.0.6.tar.gzfrom https://github.com/nemequ/bzip2/releases tar -zxvf bzip2-1.0.6.tar.gzcd bzip2-1.0.6make && make install- 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 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
Method 15
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
Method 16
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0