首页 > 技术文章 > 完美解决ModuleNotFoundError: No module named '_bz2'和AttributeError: module 'pandas' has no attribute 'compat'错误

ikww 2019-09-18 17:40 原文

笔者环境:操作系统Ubuntu,Python3.7.4

最近笔者在虚拟机上安装pandas时候遇到了各种各样的问题,在导入pandas包时就提示出错。然后借助查找各种资料最终解决了问题。

第一个错误就如上述ModuleNotFoundError: No module named '_bz2',在查阅资料后发现是虚拟机中安装的Python3.7没有_bz2.cpython-37m-x86_64-linux-gnu.so这个文件,但是可以在3.6版本中找到该文件。

随后笔者在Python3.6的安装目录中找到了_bz2.cpython-36m-x86_64-linux-gnu.so,然后通过指令将这个文件copy到了3.7.4的对应目录中,然后将_bz2.cpython-36m-x86_64-linux-gnu.s重命名为_bz2.cpython-37m-x86_64-linux-gnu.so后,再次运行代码就不提示错误了。

但是运行出来还是错误的,接下来就提示了另一个错误AttributeError: module 'pandas' has no attribute 'compat',笔者在网上查阅相关资料,有建议降低pandas版本的,还有建议升级版本的。笔者在重装了几次新旧版本后,此问题都未得到解决。后来仔细观察错误发现,错误代码在pandas的__init__.py文件中的if pandas.compat.PY37这个方法中。

其中有一个警告,大概意思是Panel这个类已经从pandas中移除。然后笔者在开发文档v0.20.1中找到了这样一段话:

Deprecate Panel

Panelis deprecated and will be removed in a future version. The recommended way to represent 3-D data are with a MultiIndexon a DataFramevia the to_frame()or with the xarray package . Pandas provides a to_xarray()method to automate this conversion ( GH13563 ).

大致意思就是Panle在以后的版本中都会移除,取而代之的是multiindexon或dataframevia to_frame()或xarray来表示三维数据。

笔者尝试将if else方法注释掉后,所有错误均被解决,但是Panel类已经不能再用了。

目前使用中没有遇到任何其他问题。

推荐阅读