Pandas导入:模块未找到错误:没有名为“pandas._libs.tslib”的模块。

3
无论是在虚拟环境中还是其他情况下,每当我尝试导入 pandas 时,都会遇到这个错误。
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
Traceback (most recent call last):
  File "C:\Users\ishan\AppData\Roaming\Python\Python36\site-packages\pandas\__init__.py", line 26, in <module>
from pandas._libs import (hashtable as _hashtable,
  File "C:\Users\ishan\AppData\Roaming\Python\Python36\site-packages\pandas\_libs\__init__.py", line 3, in <module>
from .tslib import iNaT, NaT, Timestamp, Timedelta, OutOfBoundsDatetime
ModuleNotFoundError: No module named 'pandas._libs.tslib'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ishan\AppData\Roaming\Python\Python36\site-packages\pandas\__init__.py", line 35, in <module>
"the C extensions first.".format(module))
ImportError: C extension: No module named 'pandas._libs.tslib' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.

我尝试了以下解决方案:
  1. 从Git克隆pandas并运行SETUP.py(在直接安装到我的Win10操作系统的Python 3.6实例上)
  2. 使用Anaconda作为Python分发和conda安装pandas
  3. 更新Microsoft Visual C++ 2017可再发行包
  4. 更新C:\ ProgramData \ Anaconda3 \ Lib \ site-packages \ PyInstaller \ hooks \ hook-pandas.py
这些都似乎没有起作用。请帮我理解这里的问题是什么。

在运行setup.py时,您设置了哪些选项1标志? - OriolAbril
AppData\Roaming是什么?提示中显示“Anaconda custom”,你是如何安装Anaconda的? - Andy Hayden
@xg.plt.py:我使用了 [python setup.py build_ext --inplace --force]。 - ISHAN BOSE
@ISHANBOSE 你不应该执行 setup.py build_ext,除非你正在从源代码构建,而你不应该这样做。我猜测虚拟环境没有使用anaconda,并且pandas的安装出了问题(可能是在安装anaconda之前创建的?)。我建议删除此目录 C:\Users\ishan\AppData\Roaming\Python\Python36,看看是否有所帮助。 - Andy Hayden
@AndyHayden... 它起作用了...非常感谢。你是怎么发现这是问题的呢? - ISHAN BOSE
显示剩余2条评论
5个回答

2
如果您正在使用Conda发行版(例如AnaConda,MiniConda),似乎是这种情况,卸载并重新安装Pandas可能会有所帮助。
在cmd控制台上运行以下命令:
conda uninstall pandas

conda install pandas

2
这个异常:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ishan\AppData\Roaming\Python\Python36\site-packages\pandas\__init__.py", line 35, in <module>
"the C extensions first.".format(module))
ImportError: C extension: No module named 'pandas._libs.tslib' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first

这段话的意思是建议重新安装pandas,因为可能在安装时没有正确构建。后面的句子说如果要从源代码导入pandas,则需要先运行指定的命令来构建C扩展。但一般情况下,anaconda在安装pandas时已经处理好了,所以不需要从源代码进行安装。作者猜测问题可能出在使用了虚拟环境而未使用anaconda安装pandas,建议删除错误信息中提到的目录并重新安装。
*注意:这是一件有趣的事情,为熊猫社区做贡献:有些低垂的果实、错别字或代码更改,因此我建议您调查是否有任何方式可以做出贡献。

0

在设置Python、Keras和其他任何东西时,我遇到了相同的错误。

背景:

我安装了Anaconda,并按照https://www.youtube.com/watch?v=z0qhKP2liHs的说明进行操作,并按照http://docs.anaconda.com/anaconda/user-guide/faq/#how-do-i-get-the-latest-anaconda-with-python-3-5的说明降级到Python 3.6。

从Jupyter运行时,我遇到了与作者相同的问题。我通过以下方式解决了我的问题:

看起来是Anaconda和Jupyter的组合没有起作用,或者选择了pandas版本解决了问题。


0

由于最新版本的pandas已经弃用了tslib,因此尝试去除pd.tslib.Dataframe,并在ggplot库中替换为pd.DataFrame。这样就可以解决问题啦!

你可以在ggplot文件夹的lib文件中找到这些包。

谢谢!


0

不确定这是否是详尽的答案,但似乎相关。

我遇到了这个问题,因为我正在从源代码构建Python(你也是吗?),然后使用它来构建我的应用程序的安装程序 - 在某个时候,我设法从OP中获得了异常:

ImportError: C extension: No module named 'pandas._libs.tslib' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.

然后我尝试从新打开的Python REPL中导入有问题的模块(即import pandas._libs.tslib)。结果证明这是一个有用的努力,因为我在那里得到的错误可能是关于缺少_bz2的。

那时我已经安装了许多Linux软件包,这些软件包需要在运行Python的./configure之前存在,以便它们被包含(例如为了使pip正常工作),很明显我错过了另一个软件包。

事实上,如此处所建议的响应该错误,只需简单地执行

sudo apt-get install libbz2-dev

然后重新运行./configuremakemake install,最终pandas愿意被加载。

然而,现在它抱怨缺少lzma扩展程序 :) 这就是生活:

UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.

从我的角度来看,这最终似乎是一个软性错误(应用程序启动成功!)

我的猜测是,由于当时Python缺乏可用的bzip2,因此构建时缺少该项,导致Pandas给出了一个略微误导性的错误并且无法加载,无论它是否正确地评估为一个致命错误。可能这是一个非常罕见的情况,从一开始就很容易修复,他们可能已经停止维护它,现在它不够好了吗?


更新:如果有人想知道,可以通过sudo apt-get install liblzma-dev来减轻lzma依赖警告。参见UserWarning: Could not import the lzma module. Your installed Python is incomplete,并且在我的情况下重新构建Python和PyInstaller打包应用程序,顺带一提,在整个过程中还包括重新安装Pandas。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接