Coder Perfect

python bz2 module is missing

Problem

I’ve put it in 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 attempted to install the bz2 module but received numerous errors.

 [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

Asked by Satish

Solution #1

You probably don’t have bz2 headers because you built Python from source.

Installing them on Ubuntu/Debian is as follows:

sudo apt-get install libbz2-dev

Fedora:

sudo yum install bzip2-devel 

And then reinstall Python. You’ll notice that python checks for a lot of libraries while configuring/building, and if you forget to include some of them, you’ll likely receive no support for libs like bz2.

To avoid problems like this, you should use prebuilt binaries. Python 2.7.3 is included in Ubuntu 12.04, which is the version your script requires.

Answered by jviotti

Solution #2

This happened to me while I was using Python 3.8.2 and importing pandas: import pandas as pd

“Error: ModuleNotFoundError: No module called ‘_bz2′” appeared in a long error message.

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 everything was fine.

Answered by BryanMinorPhD

Solution #3

Install bzip2-devel on CentOS 7:

sudo yum install  bzip2-devel

Then re-compile python.

Answered by James Tang

Solution #4

Installing libbz2-dev would not function if you installed Python in a specific area.

For centos, there is a workaround:

python install path is normally /usr/local/lib/python2.7/, but if you have a custom Python path, you’ll need to replace it.

Answered by Mithril

Solution #5

You’ll need to reinstall bzip2 from the source code:

Those steps can be effective at times.

Finally, I figured out the issue: it requires /usr/local/Python-3.5.2/lib/python3.5/lib-dynload/ bz2.cpython-35m-x86 64-linux-gnu.so, which must cause a difficulty when compiling bzip2 from source code. To overcome the problem, I copy this file from another VM.

Answered by lvxiaobo616

Post is based on https://stackoverflow.com/questions/12806122/missing-python-bz2-module