Docker python3.10 import pandas报错 ModuleNotFoundError: No module named ‘_bz2‘

Problem Description

After installing the 3.10 version of python in Dokcer (previously 3.7), after installing pandas, the error is as follows:

Python 3.10.0 (default, Mar  8 2023, 06:40:09) [GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/pandas/__init__.py", line 48, in <module>
    from pandas.core.api import (
  File "/usr/local/lib/python3.10/site-packages/pandas/core/api.py", line 47, in <module>
    from pandas.core.groupby import (
  File "/usr/local/lib/python3.10/site-packages/pandas/core/groupby/__init__.py", line 1, in <module>
    from pandas.core.groupby.generic import (
  File "/usr/local/lib/python3.10/site-packages/pandas/core/groupby/generic.py", line 76, in <module>
    from pandas.core.frame import DataFrame
  File "/usr/local/lib/python3.10/site-packages/pandas/core/frame.py", line 172, in <module>
    from pandas.core.generic import NDFrame
  File "/usr/local/lib/python3.10/site-packages/pandas/core/generic.py", line 147, in <module>
    from pandas.core.describe import describe_ndframe
  File "/usr/local/lib/python3.10/site-packages/pandas/core/describe.py", line 45, in <module>
    from pandas.io.formats.format import format_percentiles
  File "/usr/local/lib/python3.10/site-packages/pandas/io/formats/format.py", line 105, in <module>
    from pandas.io.common import (
  File "/usr/local/lib/python3.10/site-packages/pandas/io/common.py", line 8, in <module>
    import bz2
  File "/usr/local/lib/python3.10/bz2.py", line 17, in <module>
    from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'

solution

  • Note here, if the updated python version is 3.9+, then when changing the name below, you don’t need to add m, if it is less than 3.9, you need to add 3.6m, 3.7m like this
root@4d3576302fd4:/# which python
/usr/bin/python

root@4d3576302fd4:/# ll /usr/bin/python
lrwxrwxrwx 1 root root 22 Mar 29  2022 /usr/bin/python -> /usr/local/bin/python3*

root@4d3576302fd4:/# find / -name "_bz2*"
find: '/proc/1/map_files': Operation not permitted
find: '/proc/48/map_files': Operation not permitted
/usr/lib/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so
/usr/local/lib/python3.7/lib-dynload/_bz2.cpython-37m-x86_64-linux-gnu.so

# 注意这里,如果是更新后python版本是3.9+,则下面改名字时,不需要加m, 小于3.9需要添加3.6m,3.7m这样
root@4d3576302fd4:/# mv /usr/local/lib/python3.7/lib-dynload/_bz2.cpython-37m-x86_64-linux-gnu.so /usr/local/lib/python3.10/lib-dynload/_bz2.cpython-310-x86_64-linux-gnu.so

root@4d3576302fd4:/# python
Python 3.10.0 (default, Mar  8 2023, 06:40:09) [GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>>

reference article

Guess you like

Origin blog.csdn.net/shiwanghualuo/article/details/129406929