Import pandas on jupyter ipython notebook fails

2020-02-29 08:31发布

问题:

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.

I use the Anaconda package distribution on MAC OS X.

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.

回答1:

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.

Run in your Bash prompt:

which python

Then run the following in Jupyter/IPython and compare the result with the output you got from the Bash script:

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:

<the 2nd output> -m pip install pandas

so it would be something like this:

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

And Pandas will be installed for your Anaconda Python.

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.

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:

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 install python

or for 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.

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):

python2 -m install cython scipy pandas matplotlib jupyter

you can do that same thing for 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.



回答2:

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:

ValueError: unknown locale: UTF-8

Therefore, I've added the following lines to my ~/.bash_profile:

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

And this has fixed the issue for me.



回答3:

One thing that you can do it is install the libraries straight in Jupyter, you can try, in the cell:

!pip install pandas

or

!pip3 install pandas