118

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
Satish
  • 16,544
  • 29
  • 93
  • 149

16 Answers16

185

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.

Nick T
  • 25,754
  • 12
  • 83
  • 121
jviotti
  • 17,881
  • 26
  • 89
  • 148
  • 6
    Fedora package is bzip2-devel (at least as of Fedora 14) – Dan Halbert Apr 01 '13 at 20:17
  • 1
    I had to install all this to build a working python2.7 on CentOS6.4. yum install -y bzip2-devel zlib zlib-devel openssl openssl-devel – jamshid Dec 21 '13 at 06:17
  • @Mithril After installing libbz2, we need to recompile python sitting in virtual env. Now, I'm not sure if after re-compilation, my already installed packages will remain as it. – CKM Oct 03 '17 at 12:10
  • 1
    This worked for me on python3 (3.6+). I use pyenv I just had to recreate the before after `sudo apt-get install libbz2-dev` – lv10 Dec 24 '18 at 15:51
  • How to do this in RHEL? – MSS Feb 08 '19 at 06:55
  • 3
    Hey, I have this issue on rhel 7.. I have installed "yum install bzip2-devel" Still having this issue. – ScipioAfricanus Sep 23 '19 at 17:05
  • 2
    This doesn't work. Same issue. Though mine is _bz2 – limasxgoesto0 Mar 23 '20 at 18:45
  • I have this issue on a Oacle Linux 7 VM where I don't have root access. Is it possible to force python to use a pip library instead of the OS one? ('pip3 install --user bz2file' alone didn't work) – tuxErrante Jun 01 '20 at 08:36
  • @lv10 I'm also using pyenv but I don't get what are you saying (before after), may you explain in steps? – Nikhil Bhardwaj Jan 10 '22 at 19:25
  • Not working on Ubuntu 22 – Jérémy Pouyet Sep 26 '22 at 12:39
  • before execute yum install bzip2-devel, make sure /bin/python3 and /usr/bin/pip-3 link to the right version of your python and pip – AlphaGoMK Sep 30 '22 at 07:08
  • Can confirm that running this and then a `make install` inside python3.8 dir resolved the issue! – Petro Oct 21 '22 at 19:09
  • Note that if you've compiled you're own Python distribution (eg through ```pyenv install```), you'll need to re-compile the distribution after installing the package. – Panagiss Nov 11 '22 at 08:08
60

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.

BryanMinorPhD
  • 749
  • 5
  • 10
  • 10
    that worked for me! I used Python 3.7, so I did this: `sudo cp /usr/lib/python3.7/lib-dynload/_bz2.cpython-37m-x86_64-linux-gnu.so /usr/local/lib/python3.7` – csch Jul 21 '20 at 07:09
  • Great answer, worked like a charm! No need for python rebuild, as suggested in other places. – Orly Dec 10 '20 at 09:19
  • 9
    I was in a virtual environment - had to do this as well: ```sudo cp /usr/lib/python3.8/lib-dynload/_bz2.cpython-38-x86_64-linux-gnu.so /workspaces/demo_classification/.venv/lib/python3.8/site-packages/``` – Partha Mandal Mar 19 '21 at 21:57
  • For pyenv user, need to copy this .so file to some paths like `~/.pyenv/versions/3.6.7/lib/python3.6/`. – Ynjxsjmh Dec 22 '21 at 02:46
14

On CentOS 7, install bzip2-devel:

sudo yum install  bzip2-devel

Then re-compile python.

James Tang
  • 593
  • 6
  • 13
8

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.

Mithril
  • 12,947
  • 18
  • 102
  • 153
  • This solution worked perfectly for me. I was unable to import bzip2 for a python script until I copied over this bz2.so file from the system installation over into my virtualenv folder. – TheDailyToast Nov 29 '16 at 22:31
  • I was using this solution until I read the other comment and installed bzip2-devel prior to building python 2.7.14. – trinth Oct 25 '17 at 23:33
  • 3
    I am in Python3, and get error: ImportError: dynamic module does not define module export function (PyInit_bz2) – DunkOnly Oct 30 '17 at 03:42
  • Worked for me in CentOS7 on a cluster where I don't have root access. How on earth did you work this out? Regardless of the method, thank you very much! – Gabriel Nov 29 '17 at 13:33
6

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.

Jingjia Luo
  • 61
  • 1
  • 1
  • could you please add the details between point 2 and 3? I'm using this script but still receiving an exception https://pastebin.com/DJxUXaYd "Could not import runpy module" – tuxErrante Jun 01 '20 at 15:20
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
>>>
user9869932
  • 6,571
  • 3
  • 55
  • 49
5

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
hafiz031
  • 2,236
  • 3
  • 26
  • 48
4

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.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
4

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.

bluetata
  • 577
  • 7
  • 12
  • thanks for this answer, due to this i solved lots of library problem , in server i have 3.6 in venv i have python 3.10 , so some modules are not available in venv python. – Ashish Maradiya Jul 27 '23 at 15:06
3

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.

AFoglia
  • 7,968
  • 3
  • 35
  • 51
2

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

trinth
  • 5,919
  • 9
  • 40
  • 45
2

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.

Binh Le
  • 81
  • 5
1

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

Jalal Mostafa
  • 984
  • 10
  • 23
0

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
mrghofrani
  • 1,335
  • 2
  • 13
  • 32
0

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
0

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.

Guru Bhandari
  • 109
  • 1
  • 7