Reputation: 503
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?
Upvotes: 29
Views: 47834
Reputation: 6376
I found a pattern in those issues.
It happens mostly if you are missing dev tools and other important libraries required to compile code and install python.
For me, most of those steps did not work. But I had to do the following :
pyenv uninstall python_version.
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
pyenv install python_version.
I hope that solves your issues.
Upvotes: 13
Reputation: 11
I've experience the same error in a RHEL 7.9 with a own python version (compiled from the scratch)
[lsaavedr@boxer ~]$ python3
Python 3.6.8 (default, Mar 7 2022, 12:20:27)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import bz2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/bz2.py", line 23, in <module>
from _bz2 import BZ2Compressor, BZ2Decompressor
And I solved either copying or /symlink/ the bz2 shared library into the python3 directory tree:
[root@boxer python3.6]# pwd
/usr/local/lib/python3.6
[root@boxer python3.6]# ln -s ../../../lib64/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so
[root@boxer python3.6]# rpm -qf ../../../lib64/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so
python3-libs-3.6.8-19.el7_9.x86_64
Then bz2 works fine:
[lsaavedr@boxer ~]$ python3
Python 3.6.8 (default, Mar 7 2022, 12:20:27)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import bz2
>>> data = b"""All in all, it's just another brick in the wall"""
>>> c = bz2.compress(data)
>>> c
b'BZh91AY&SY\xc5\xd1nL\x00\x00\x05\x95\x80@\x84 \x00:}\x9e\x80 \x001L&\x9a\x03LA\xa8\x06\x86\x86\xd4i\xbe\x96d\xed\xb6\x96\x17x\x82EB\xd0\x02\x90\xe3\x0b\x88\xaa\x92\xec\x8a\xef/\x8b\xb9"\x9c(Hb\xe8\xb7&\x00'
>>> d = bz2.decompress(c)
>>> d
b"All in all, it's just another brick in the wall"
Upvotes: 1
Reputation: 1
if sudo apt-get install libbz2-dev is not sense, you can try
sudo ln -s /usr/lib/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so /usr/local/lib/python3.8/lib-dynload/_bz2.cpython-38-x86_64-linux-gnu.so
Upvotes: 0
Reputation: 5282
Using pyenv
sudo apt-get install libbz2-dev
python --version
pyenv install version
Upvotes: 0
Reputation: 27
finally, I fix this problems in centos with python3.9,.when everything can't work,
downloads this file: https://pan.baidu.com/s/1iPuEBYnUABWf94QM9fQZgQ 提取码: nw2g
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/
Modify file permissions if it's doesn‘t work
chmod +x _bz2.cpython-38-x86_64-linux-gnu.so
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.
Upvotes: 1
Reputation: 7337
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
>>>
Upvotes: 6
Reputation: 10421
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.
Upvotes: 31