missing python bz2 module

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.


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.


On CentOS 7, install bzip2-devel:

sudo yum install  bzip2-devel

Then re-compile python.


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.


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.