修复 Python 报错 ModuleNotFoundError: No module named 问题
后知后觉 暂无评论

部署 superset 时,系统为 Ubuntu 18.04,自带的版本比较旧,因此额外安装的 Python 3.9 版本。

起因

在执行导入操作时报错

  File "/home/superset/venv/lib/python3.9/site-packages/pandas/io/common.py", line 8, in <module>
    import bz2
  File "/usr/local/lib/python3.9/bz2.py", line 18, in <module>
    from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'

完整报错如下:

$ superset db upgrade
Traceback (most recent call last):
  File "/home/superset/venv/bin/superset", line 33, in <module>
    sys.exit(load_entry_point('apache-superset==2.1.0', 'console_scripts', 'superset')())
  File "/home/superset/venv/bin/superset", line 25, in importlib_load_entry_point
    return next(matches).load()
  File "/usr/local/lib/python3.9/importlib/metadata.py", line 86, in load
    module = import_module(match.group('module'))
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/superset/venv/lib/python3.9/site-packages/superset/__init__.py", line 21, in <module>
    from superset.app import create_app
  File "/home/superset/venv/lib/python3.9/site-packages/superset/app.py", line 23, in <module>
    from superset.initialization import SupersetAppInitializer
  File "/home/superset/venv/lib/python3.9/site-packages/superset/initialization/__init__.py", line 33, in <module>
    from superset.extensions import (
  File "/home/superset/venv/lib/python3.9/site-packages/superset/extensions/__init__.py", line 32, in <module>
    from superset.utils.async_query_manager import AsyncQueryManager
  File "/home/superset/venv/lib/python3.9/site-packages/superset/utils/async_query_manager.py", line 26, in <module>
    from superset.utils.core import get_user_id
  File "/home/superset/venv/lib/python3.9/site-packages/superset/utils/core.py", line 75, in <module>
    import pandas as pd
  File "/home/superset/venv/lib/python3.9/site-packages/pandas/__init__.py", line 48, in <module>
    from pandas.core.api import (
  File "/home/superset/venv/lib/python3.9/site-packages/pandas/core/api.py", line 47, in <module>
    from pandas.core.groupby import (
  File "/home/superset/venv/lib/python3.9/site-packages/pandas/core/groupby/__init__.py", line 1, in <module>
    from pandas.core.groupby.generic import (
  File "/home/superset/venv/lib/python3.9/site-packages/pandas/core/groupby/generic.py", line 76, in <module>
    from pandas.core.frame import DataFrame
  File "/home/superset/venv/lib/python3.9/site-packages/pandas/core/frame.py", line 172, in <module>
    from pandas.core.generic import NDFrame
  File "/home/superset/venv/lib/python3.9/site-packages/pandas/core/generic.py", line 147, in <module>
    from pandas.core.describe import describe_ndframe
  File "/home/superset/venv/lib/python3.9/site-packages/pandas/core/describe.py", line 45, in <module>
    from pandas.io.formats.format import format_percentiles
  File "/home/superset/venv/lib/python3.9/site-packages/pandas/io/formats/format.py", line 105, in <module>
    from pandas.io.common import (
  File "/home/superset/venv/lib/python3.9/site-packages/pandas/io/common.py", line 8, in <module>
    import bz2
  File "/usr/local/lib/python3.9/bz2.py", line 18, in <module>
    from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'

解决

这是因为编译 Python 时,系统缺少相关的依赖,因为部分模块没有自动编译出来,需要补充依赖重新编译即可生成额外的模块。

sudo apt install libbz2-dev

然后重新编译即可

cd Python-3.9.16/
./configure --enable-optimizations
make
sudo make altinstall

然后检查编译结果,可以看到在重新编译后,可以看到模块 _bz2.cpython-39-x86_64-linux-gnu.so 存在。

sudo find /usr/ -name '*_bz2*'
/usr/lib/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so
/usr/local/qcloud/python/lib/python3.7/test/test_bz2.py
/usr/local/qcloud/python/lib/python3.7/test/__pycache__/test_bz2.cpython-37.opt-1.pyc
/usr/local/qcloud/python/lib/python3.7/test/__pycache__/test_bz2.cpython-37.opt-2.pyc
/usr/local/qcloud/python/lib/python3.7/test/__pycache__/test_bz2.cpython-37.pyc
/usr/local/lib/python3.9/test/test_bz2.py
/usr/local/lib/python3.9/test/__pycache__/test_bz2.cpython-39.opt-2.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.pyc
/usr/local/lib/python3.9/lib-dynload/_bz2.cpython-39-x86_64-linux-gnu.so

附录

参考链接

本文撰写于一年前,如出现图片失效或有任何问题,请在下方留言。博主看到后将及时修正,谢谢!
禁用 / 当前已拒绝评论,仅可查看「历史评论」。