大家好,又见面了,我是你们的朋友全栈君。
在运行PyTorch代码的时候,报了“ModuleNotFoundError: No module named ‘_bz2’”错误,完整报错提示信息如下:
Traceback (most recent call last):
File "stat_model.py", line 1, in <module>
from torchstat import stat
File "/usr/local/lib/python3.7/site-packages/torchstat/__init__.py", line 11, in <module>
from torchstat.reporter import report_format
File "/usr/local/lib/python3.7/site-packages/torchstat/reporter.py", line 1, in <module>
import pandas as pd
File "/usr/local/lib/python3.7/site-packages/pandas/__init__.py", line 55, in <module>
from pandas.core.api import (
File "/usr/local/lib/python3.7/site-packages/pandas/core/api.py", line 24, in <module>
from pandas.core.groupby import Grouper, NamedAgg
File "/usr/local/lib/python3.7/site-packages/pandas/core/groupby/__init__.py", line 1, in <module>
from pandas.core.groupby.generic import ( # noqa: F401
File "/usr/local/lib/python3.7/site-packages/pandas/core/groupby/generic.py", line 44, in <module>
from pandas.core.frame import DataFrame
File "/usr/local/lib/python3.7/site-packages/pandas/core/frame.py", line 88, in <module>
from pandas.core.generic import NDFrame, _shared_docs
File "/usr/local/lib/python3.7/site-packages/pandas/core/generic.py", line 70, in <module>
from pandas.io.formats.format import DataFrameFormatter, format_percentiles
File "/usr/local/lib/python3.7/site-packages/pandas/io/formats/format.py", line 48, in <module>
from pandas.io.common import _expand_user, _stringify_path
File "/usr/local/lib/python3.7/site-packages/pandas/io/common.py", line 3, in <module>
import bz2
File "/usr/local/lib/python3.7/bz2.py", line 19, in <module>
from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'
出现这个错误的原因是我用的是python3.7,但是bz2这个库是安装到了python3.6里面,所以找不到。为了解决这个问题,需要将python3.6里面的bz库拷贝到python3.7下面。具体过程如下:
1、找到python3.6路径下的bz库文件,即“_bz2.cpython-36m-x86_64-linux-gnu.so”。
ls /usr/lib/python3.6/lib-dynload/
可以看到该文件名称中的“-36m”对应的是python3.6版本。
2、切换到python3.7对应路径,将该文件复制到该目录下:
cd /usr/local/lib/python3.7/lib-dynload
sudo cp /usr/lib/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so ./
3、修改文件名称,将”-36m”修改为”-37m”即可:
sudo mv _bz2.cpython-36m-x86_64-linux-gnu.so _bz2.cpython-37m-x86_64-linux-gnu.so
至此,问题解决。
需要注意的是,还有一个路径/usr/lib/python3.7/lib-dynload,文件复制到该目录下面没有用,我这里需要复制到/usr/local/lib/python3.7/lib-dynload目录下。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/134939.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...