【问题标题】:Import pandas on jupyter ipython notebook fails在 jupyter ipython notebook 上导入熊猫失败
【发布时间】:2016-05-17 03:40:32
【问题描述】:

我可以在 spyder ide 中导入 pandas 包;但是,如果我尝试打开一个新的 juypter 笔记本,导入会失败。

我在 MAC OS X 上使用 Anaconda 包分发。

这是我的工作:

In [1]: import pandas 

这是我得到的回应:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-5-97925edf8fb0> in <module>()
----> 1 import pandas

//anaconda/lib/python2.7/site-packages/pandas/__init__.py in <module>()
     11                       "pandas from the source directory, you may need to run "
     12                       "'python setup.py build_ext --inplace' to build the C "
---> 13                       "extensions first.".format(module))
     14 
     15 from datetime import datetime

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

【问题讨论】:

  • 您是否尝试过错误消息中所述的python setup.py build_ext --inplace
  • 如果 @albert 的建议不起作用,您可以使用 brew 获得正确的 python 设置。例如,您可以遵循本指南的前三个步骤:blog.manbolo.com/2014/09/27/use-python-effectively-on-os-x
  • @albert 我应该在哪里粘贴“python setup.py build_ext --inplace”?
  • @Pat 在您的系统终端中。你应该确保你在包含 Pandas 的 setup.py 的目录中
  • 终端返回:SyntaxError: invalid syntax

标签: python pandas ipython anaconda jupyter


【解决方案1】:

您安装了多个 Python 2 引擎。一个在您的主要操作系统平台中,另一个在 Anaconda 的虚拟环境中。您需要在后者上安装 Panda。

在 Bash 提示符下运行:

which python

然后在 Jupyter/IPython 中运行以下命令,并将结果与​​从 Bash 脚本获得的输出进行比较:

from sys import executable
print(executable)

如果它们不同,您应该记下后者的结果(即复制它),然后转到您的 Bash 提示符,并执行以下操作:

<the 2nd output> -m pip install pandas

所以它会是这样的:

/usr/bin/anaconda/python2 -m pip install pandas

将为您的 Anaconda Python 安装 Pandas。

有一种方法可以使用sys.path.append('path to alternative locations') 将库路径添加到您现有的环境中,但是每次您想要使用替代环境时都必须这样做,因为效果是暂时的。

您也可以在主环境中安装所有内容:

python -m pip install cython scipy panda matplotlib jupyter notebook ipython

更新:

根据以上部分的回复:

像这样安装homebrew

在您的终端中:

xcode-select --install

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

然后运行:

brew doctor
brew update
brew upgrade

现在继续通过 Brew 安装 Python 2:

brew install python

或者对于 Python 3

brew install python3

或者两者都做。安装其他有用的东西!

brew install git conda gfortran clang pkg-config

然后您可以继续使用 brew 或使用 pip 安装所需的库,但首先您应该确保安装了 pip 本身。

easy_install pip

然后你可以像这样安装 Python 包(NumPy 包含在 SciPy 中,SciPy 和 Matplotlib 依赖于 Cython 和 C,Scipy 还使用 fortran 进行 ODE):

python2 -m install cython scipy pandas matplotlib jupyter

你可以对 Python 3 做同样的事情。

这个全新安装应该可以真正解决问题。如果没有,请从 Python.org 下载 Python 并重新安装。 brew 有时会在发现软件包已经存在时拒绝安装该软件包。我不建议删除 Python 2,以便您可以通过 brew 安装它。这可能会导致 OS X 出现问题。因此,最好的替代方法是通过安装从网站下载的软件包来修复现有安装。 OS X 确保软件包安装在正确的位置。完成此操作后,您可以返回到说明,但从 brew install python3 开始。

【讨论】:

    【解决方案2】:

    我在带有 Anaconda (Python 2) 的 Mac OS X 上遇到了同样的问题。我尝试在 python repl 中导入 pandas 包,得到了这个错误:

    ValueError: unknown locale: UTF-8
    

    因此,我在 ~/.bash_profile 中添加了以下几行:

    export LC_ALL=en_US.UTF-8
    export LANG=en_US.UTF-8
    

    这已经为我解决了这个问题。

    【讨论】:

    • 同样的问题。 Mac 上的 Python 2。使用上面提到的出口,它的工作原理。
    【解决方案3】:

    您可以做的一件事是直接在 Jupyter 中安装库,您可以在单元格中尝试:

    !pip install pandas
    

    !pip3 install pandas
    

    【讨论】:

    • %pip install pandas 用于现代设置。有关详细信息,请参阅 hereherehere
    猜你喜欢
    • 2017-06-14
    • 2016-11-18
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2015-10-15
    • 1970-01-01
    • 2016-12-27
    相关资源
    最近更新 更多