pandas 在 jupyter ipython notebook 上导入熊猫失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35253338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Import pandas on jupyter ipython notebook fails
提问by Pat
I am able to import the pandas package within the spyder ide; however, if I attempt to open a new juypter notebook, the import fails.
我可以在 spyder ide 中导入 pandas 包;但是,如果我尝试打开一个新的 juypter 笔记本,导入会失败。
I use the Anaconda package distribution on MAC OS X.
我在 MAC OS X 上使用 Anaconda 包分发。
Here is what I do:
这是我所做的:
In [1]: import pandas
and this is the response I get:
这是我得到的回应:
---------------------------------------------------------------------------
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.
回答by Pouria
You have more than one Python 2 engines installed. One in your main OS platform, another one inside Anaconda's virtual environment. You need to install Panda on the latter.
您安装了不止一个 Python 2 引擎。一个在您的主操作系统平台中,另一个在 Anaconda 的虚拟环境中。您需要在后者上安装 Panda。
Run in your Bash prompt:
在 Bash 提示符下运行:
which python
Then run the following in Jupyter/IPython and compare the result with the output you got from the Bash script:
然后在 Jupyter/IPython 中运行以下命令并将结果与您从 Bash 脚本获得的输出进行比较:
from sys import executable
print(executable)
If they differ, you should note the result of the latter (i.e. copy it), and then go to your Bash prompt, and do as follows:
如果它们不同,您应该注意后者的结果(即复制它),然后转到您的 Bash 提示符,并执行以下操作:
<the 2nd output> -m pip install pandas
so it would be something likethis:
所以这将是什么样的:
/usr/bin/anaconda/python2 -m pip install pandas
And Pandas will be installed for your Anaconda Python.
将为您的 Anaconda Python 安装 Pandas。
There is a way to add library paths to your existing environment, using sys.path.append('path to alternative locations')
, but this has to be done every time your want to use the alternative environment as the effects are temporary.
有一种方法可以将库路径添加到现有环境中,使用sys.path.append('path to alternative locations')
,但是每次您想要使用替代环境时都必须这样做,因为效果是暂时的。
You can alternatively install everything in your main environment:
您也可以在主环境中安装所有内容:
python -m pip install cython scipy panda matplotlib jupyter notebook ipython
Update:
更新:
Based on responses to the above section:
基于对上述部分的答复:
Install homebrew
like so:
homebrew
像这样安装:
In your Terminal:
在您的终端中:
xcode-select --install
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
then run:
然后运行:
brew doctor
brew update
brew upgrade
Now go ahead and install Python 2 through Brew:
现在继续并通过 Brew 安装 Python 2:
brew install python
or for Python 3
或 Python 3
brew install python3
Or do both. The install other useful stuff!
或者两者都做。安装其他有用的东西!
brew install git conda gfortran clang pkg-config
Then you can go ahead and install your desired libraries either using brew, or using pip
, but first you should ensure that pip
itself is installed.
然后你可以继续使用 brew 或 using 安装你想要的库pip
,但首先你应该确保它pip
本身已经安装。
easy_install pip
then you can install Python packages like so (NumPy is included in SciPy, and SciPy and Matplotlib depend on Cython and C, Scipy additionally uses fortran for ODE):
然后你可以像这样安装 Python 包(NumPy 包含在 SciPy 中,SciPy 和 Matplotlib 依赖于 Cython 和 C,Scipy 还使用 fortran 进行 ODE):
python2 -m install cython scipy pandas matplotlib jupyter
you can do that same thing for Python 3.
你可以对 Python 3 做同样的事情。
This clean install should really solve the problem. If it didn't, download Python from Python.org and re-install it. brew
sometime refuses to install a package if it finds out that the package already exists. I don't recommend removing Python 2 so that you can install it through brew
. That might cause issues with OS X. So the best alternative is to repair existing installations by installing the package downloaded from the website. OS X ensures that the package is installed in the right place. Once this is done, you can then go back to the instructions, but start from brew install python3
.
这种全新安装应该可以真正解决问题。如果没有,请从 Python.org 下载 Python 并重新安装。brew
如果发现某个包已经存在,有时会拒绝安装该包。我不建议删除 Python 2,以便您可以通过brew
. 这可能会导致 OS X 出现问题。因此,最好的替代方法是通过安装从网站下载的软件包来修复现有安装。OS X 确保软件包安装在正确的位置。完成此操作后,您可以返回到说明,但从brew install python3
.
回答by volhv
I had the same issue on Mac OS X with Anaconda (Python 2). I tried importing the pandas package in python repl, and got this error:
我在使用 Anaconda (Python 2) 的 Mac OS X 上遇到了同样的问题。我尝试在 python repl 中导入 pandas 包,并收到此错误:
ValueError: unknown locale: UTF-8
Therefore, I've added the following lines to my ~/.bash_profile:
因此,我在 ~/.bash_profile 中添加了以下几行:
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
And this has fixed the issue for me.
这为我解决了这个问题。
回答by Robert Garcia
One thing that you can do it is install the libraries straight in Jupyter, you can try, in the cell:
您可以做的一件事是直接在 Jupyter 中安装库,您可以在单元格中尝试:
!pip install pandas
or
或者
!pip3 install pandas