15

When trying to execute the following command:

import matplotlib.pyplot as plt

The following error occurs:

from _bz2 import BZ2Compressor, BZ2Decompressor ImportError: No module named '_bz2'

So, I was trying to install bzip2 module in Ubuntu using :

sudo pip3 install bzip2

But, the following statement pops up in the terminal:

Could not find a version that satisfies the requirement bzip2 (from versions: ) No matching distribution found for bzip2

What can I do to solve the problem?

  • Try `sudo pip3 -v install bzip2` for more verbose output. – CristiFati May 14 '18 at 17:16
  • The following error is shown: 'No matching distribution found for %s' % req pip._internal.exceptions.DistributionNotFound: No matching distribution found for bzip2 – Sifat Muhammad Abdullah May 14 '18 at 18:32
  • You won't be able to install bz2 with pip because it's not on pypi, it's part of the standard library. I wonder if you have the minimal python installed (which doesn't have all of the standard library). What is the output of `dpkg -l libpython3-stdlib`? – mostlyoxygen May 18 '18 at 20:36
  • https://stackoverflow.com/questions/12806122/missing-python-bz2-module/61464947#61464947 this questions will resolve your problem – haoran920 Jul 15 '21 at 08:59

5 Answers5

16

If you compiling python yourself, you need to install libbz2 headers and .so files first, so that python will be compiled with bz2 support.

On ubuntu, apt-get install libbz2-dev then compile python.

ospider
  • 7,510
  • 3
  • 40
  • 43
  • 2
    Once install rebuild python3 ./configure --enable-optimizations sudo make altinstall – ANIL PATEL Dec 05 '20 at 09:37
  • I am a newbie, how do you rebuild python? – M Akin Nov 07 '21 at 21:05
  • 1
    @MAkin If you are asking how to rebuild python, then you have to figure out how the original python got in your system, then rerun the process with what I have answered. – ospider Nov 08 '21 at 07:00
1

In my case I got the error when importing Pandas. Installing Python 3.9 solved it.

My python version was 3.8.6. I am using Pyenv and running MacOS Big Sur.

Error

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

$ pyenv install --list
$ pyenv install 3.9.1
$ pyenv local 3.9.1
$ pyenv global 3.9.1
$ pip install pandas

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
  • 5,513
  • 3
  • 53
  • 46
1

I found a pattern in those issue.

It happens mostly if you are missing dev tools and other important libraries important to compile code and install python.

For me most of those step did not work. But I had to do the following :

  • Remove my python installation

pyenv uninstall python_version

  • Then install all the build tools to make sure I am not missing any

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \ libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \ xz-utils tk-dev libffi-dev liblzma-dev python-openssl git

  • Reinstall the new python version

pyenv install python_version

I hope that solves your issues.

Espoir Murhabazi
  • 5,134
  • 2
  • 40
  • 66
0

finally, I fix this problems in centos with python3.9,.when everything can't work,

  1. we can download file:_bz2.cpython-38-x86_64-linux-gnu.so.

downloads this file: https://pan.baidu.com/s/1iPuEBYnUABWf94QM9fQZgQ 提取码: nw2g

  1. then renames file because i use python3.9, this file is python3.8 :

    cp _bz2.cpython-38-x86_64-linux-gnu.so /usr/local/python3/lib/python3.9/lib-dynload/

  2. Modify file permissions if it's doesn‘t work

chmod +x _bz2.cpython-38-x86_64-linux-gnu.so

  1. if it report ImportError: libbz2.so.1.0: cannot open shared object file: No such file or directory , we should make sure bzip is installed. use: yum install -y bzip2* . then execute ln -s /usr/lib64/libbz2.so.1 /usr/lib64/libbz2.so.1.0 . execute python3 -c 'import _bz2', it can working normally !!!

i search from https://www.jianshu.com/p/b722adc2ba52.

Reegan Miranda
  • 2,761
  • 6
  • 43
  • 53
haoran920
  • 19
  • 2
0

Using pyenv

sudo apt-get install libbz2-dev
python --version
pyenv install version
Tlaloc-ES
  • 3,690
  • 5
  • 26
  • 62