问题起因:

        最近给公司做数据可视化项目改造,其中BI选型为Superset ,在Centos8上安装Superset V1.1.0做superset db upgrade初始化时遇到了这个错误,Python版本是3.9.1。

错误提示:

(SupersetPy39) [root@localhost bin]# superset db upgrade
Traceback (most recent call last):
    import bz2
  File "/opt/python39/lib/python3.9/bz2.py", line 18, in <module>
    from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'

通常解决办法两种:

1)安装bzip2-devel(通常不行,看方法二且注意Python版本)

#检查是否安装bz2

(SupersetPy39) [root@localhost bin]# pip list | grep bz2

(SupersetPy39) [root@localhost bin]# yum install bzip2-devel
CentOS Linux 8 - AppStream                                    9.2 kB/s | 4.3 kB     00:00
CentOS Linux 8 - BaseOS                                       3.9 kB/s | 3.9 kB     00:00
CentOS Linux 8 - Extras                                       3.6 kB/s | 1.5 kB     00:00
CentOS Linux 8 - Extras                                        15 kB/s |  10 kB     00:00
Docker CE Stable - x86_64                                     6.1 kB/s | 3.5 kB     00:00
Docker CE Stable - x86_64                                      13 kB/s |  15 kB     00:01
软件包 bzip2-devel-1.0.6-26.el8.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!

#安装完,在Python源码目录下重新编译

(SupersetPy39) [root@localhost bin]# cd /tmp/Python-3.9.6/
(SupersetPy39) [root@localhost Python-3.9.6]# ./configure --prefix=/opt/python39 --with-ssl

(SupersetPy39) [root@localhost Python-3.9.6]# make && make install

2)已经安装了bzip2-devel,仍提示No module named '_bz2',此时注意Python版本

如果Python版本是3.7,<3.9

解决办法是:#查找_bz2.cpython-文件所在的目录,拷贝文件并改名到Python目录下(如果是Py3.7,改成37m;如果是Py3.5,改成35m)

# find / -name "*_bz2.cpython-*"

# cp /usr/lib64/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so /您Python对应版本的目录下/lib-dynload/_bz2.cpython-3*m-x86_64-linux-gnu.so

我的Python版本是3.9,所以

解决办法是:#查找_bz2.cpython-文件所在的目录,拷贝文件并改名到Python目录下(改成39)

#拷贝改名(/usr/local/lib/python3.9/是我Python3.9系统目录,/opt/python39/lib/python3.9/是我Python3.9编译后的Python目录,/tmp/Python-3.9.6/是我Python3.9下载待编译的Python目录,怕麻烦所以都拷贝改名了)

(SupersetPy39) [root@localhost bin]# cp /usr/lib64/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so /usr/local/lib/python3.9/lib-dynload/_bz2.cpython-39-x86_64-linux-gnu.so

(SupersetPy39) [root@localhost bin]# cp /usr/lib64/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so /opt/python39/lib/python3.9/lib-dynload/_bz2.cpython-39-x86_64-linux-gnu.so

(SupersetPy39) [root@localhost bin]# cp /usr/lib64/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so /tmp/Python-3.9.6/build/lib.linux-x86_64-3.9/_bz2.cpython-39-x86_64-linux-gnu.so

#检查拷贝文件名

(SupersetPy39) [root@localhost Python-3.9.6]# find / -name "*_bz2.cpython-39*"
/usr/local/lib/python3.9/test/__pycache__/test_bz2.cpython-39.pyc
/usr/local/lib/python3.9/test/__pycache__/test_bz2.cpython-39.opt-1.pyc
/usr/local/lib/python3.9/test/__pycache__/test_bz2.cpython-39.opt-2.pyc
/usr/local/lib/python3.9/lib-dynload/_bz2.cpython-39-x86_64-linux-gnu.so
/usr/local/lib/python3.9/_bz2.cpython-39-x86_64-linux-gnu.so
/opt/python39/lib/python3.9/test/__pycache__/test_bz2.cpython-39.pyc
/opt/python39/lib/python3.9/test/__pycache__/test_bz2.cpython-39.opt-1.pyc
/opt/python39/lib/python3.9/test/__pycache__/test_bz2.cpython-39.opt-2.pyc
/opt/python39/lib/python3.9/_bz2.cpython-39-x86_64-linux-gnu.so
/opt/python39/lib/python3.9/lib-dynload/_bz2.cpython-39-x86_64-linux-gnu.so
/tmp/Python-3.9.6/build/lib.linux-x86_64-3.9/_bz2.cpython-39-x86_64-linux-gnu.so

#重新编译Python3.9

#进入源码下载解压目录

(SupersetPy39) [root@localhost bin]# cd /tmp/Python-3.9.6/

#编译Python3.9
(SupersetPy39) [root@localhost Python-3.9.6]# ./configure --prefix=/opt/python39 --with-ssl

#安装Python3.9

(SupersetPy39) [root@localhost Python-3.9.6]# make && make install

#重新做superset db upgrade初始化,结果正常

(SupersetPy39) [root@localhost Python-3.9.6]# superset db upgrade

至此ModuleNotFoundError: No module named '_bz2'错误也没有提示。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐