ModuleNotFoundError:No module name '_bz2'

现象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@ocr]# python3.6 server.py 
Traceback (most recent call last):
File "server.py", line 6, in <module>
from image_preprocessing import *
File "/home/lp/iocr-kit-core-release-1.5/image_preprocessing.py", line 6, in <module>
from model_info import model_size
File "/home/lp/iocr-kit-core-release-1.5/model_info.py", line 2, in <module>
import pandas as pd
File "/usr/local/python3.6.9/lib/python3.6/site-packages/pandas/__init__.py", line 52, in <module>
from pandas.core.api import (
File "/usr/local/python3.6.9/lib/python3.6/site-packages/pandas/core/api.py", line 29, in <module>
from pandas.core.groupby import Grouper, NamedAgg
File "/usr/local/python3.6.9/lib/python3.6/site-packages/pandas/core/groupby/__init__.py", line 1, in <module>
from pandas.core.groupby.generic import DataFrameGroupBy, NamedAgg, SeriesGroupBy
File "/usr/local/python3.6.9/lib/python3.6/site-packages/pandas/core/groupby/generic.py", line 57, in <module>
from pandas.core.aggregation import (
File "/usr/local/python3.6.9/lib/python3.6/site-packages/pandas/core/aggregation.py", line 27, in <module>
from pandas.core.series import FrameOrSeriesUnion, Series
File "/usr/local/python3.6.9/lib/python3.6/site-packages/pandas/core/series.py", line 68, in <module>
from pandas.core import algorithms, base, generic, nanops, ops
File "/usr/local/python3.6.9/lib/python3.6/site-packages/pandas/core/generic.py", line 102, in <module>
from pandas.io.formats import format as fmt
File "/usr/local/python3.6.9/lib/python3.6/site-packages/pandas/io/formats/format.py", line 71, in <module>
from pandas.io.common import stringify_path
File "/usr/local/python3.6.9/lib/python3.6/site-packages/pandas/io/common.py", line 3, in <module>
import bz2
File "/usr/local/python3.6.9/lib/python3.6/bz2.py", line 23, in <module>
from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'

解决

报错显示缺少 _bz2 这个 module,其实就是缺少了_bz2.cpython-36m-x86_64-linux-gnu.so 文件。报这个错的原因通常是系统装了多个 Python 环境,然后 bz2 只装到了一个环境下面,所以找不到。

  1. 确保安装了对应的库

    1
    2
    3
    4
    # Ubuntu
    sudo apt-get install bzip2-devel
    # CentOS
    sudo yum install bzip2-devel
  2. 首先在本地的linux环境搜索是否存在这个文件,如果没有就到网上下载一个。

    1
    find / -name _bz2.cpython-37m-x86_64-linux-gnu.so  # “-37m”对应的是python3.7版本
  3. 把这个文件保存到你的 Python 的安装目录 /you_python_path/lib/python3.6/lib-dynload/ 文件夹下,然后将”-37m”修改为”-36m”即可。

    1
    2
    3
    cd /you_python_path/lib/python3.6/lib-dynload/
    cp /usr/lib/python3.6/lib-dynload/_bz2.cpython-37m-x86_64-linux-gnu.so ./
    sudo mv _bz2.cpython-37m-x86_64-linux-gnu.so _bz2.cpython-36m-x86_64-linux-gnu.so

注:python36和37的可以互换,但是和39的不互通,所以如果缺python39的最好是到网上找一个。


ModuleNotFoundError:No module name '_bz2'
https://flepeng.github.io/021-Python-71-报错-ModuleNotFoundError:No-module-name-bz2/
作者
Lepeng
发布于
2021年7月5日
许可协议