Python 如何解决熊猫导入错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30761152/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:55:12  来源:igfitidea点击:

How to solve import error for pandas?

pythonpython-2.7pandaspydevpython-import

提问by Alex F

I installed Anaconda with python 2.7.7.
However, whenever I run "import pandas" I get the error:
"ImportError: C extension: y 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."
I tried running the suggested command but it stated that

我用 python 2.7.7 安装了 Anaconda。
但是,每当我运行“import pandas”时,我都会收到错误消息:
"ImportError: C extension: y 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."
我尝试运行建议的命令,但它指出

skipping 'pandas\index.c' Cython extension (up-to-date)      
skipping 'pandas\src\period.c' Cython extension (up-to-date) 
skipping 'pandas\algos.c' Cython extension (up-to-date)      
skipping 'pandas\lib.c' Cython extension (up-to-date)        
skipping 'pandas\tslib.c' Cython extension (up-to-date)      
skipping 'pandas\parser.c' Cython extension (up-to-date)     
skipping 'pandas\hashtable.c' Cython extension (up-to-date)  
skipping 'pandas\src\sparse.c' Cython extension (up-to-date) 
skipping 'pandas\src\testing.c' Cython extension (up-to-date)
skipping 'pandas\msgpack.cpp' Cython extension (up-to-date)

Has anyone encountered this before and found a solution?

有没有人遇到过这个问题并找到了解决方案?

采纳答案by vicg

Pandas has portions of its code written in C to make it run faster. If you tried to install pandas manually you would need to build it. Try reinstalling it with miniconda package manager here: http://conda.pydata.org/miniconda.html

Pandas 的部分代码是用 C 编写的,以使其运行得更快。如果您尝试手动安装熊猫,则需要构建它。尝试在此处使用 miniconda 包管理器重新安装它:http://conda.pydata.org/miniconda.html

and then you can just do

然后你就可以做

conda install pandas

There are very simple instructions on how to do it in the link below. Just do ctrl-f miniconda to find the section that talks about it

下面的链接中有关于如何操作的非常简单的说明。只需执行 ctrl-f miniconda 即可找到讨论它的部分

http://pandas.pydata.org/pandas-docs/dev/install.html

http://pandas.pydata.org/pandas-docs/dev/install.html

回答by Nicolas

I had the same problem and the issue came from an encoding problem. My os was previously set up in French and everything was fine. But then when I switched to English I had the error above.

我遇到了同样的问题,问题来自编码问题。我的操作系统以前是用法语设置的,一切都很好。但是当我切换到英语时,我遇到了上面的错误。

You can type

你可以输入

locale

in the terminal to check the local environment variables.

在终端中检查本地环境变量。

When set up in French, I had this configuration: French config. Then, after I switched to English, I had: English config.

当用法语设置时,我有这样的配置: French config。然后,在我切换到英语后,我有: English config

I then added the following lines in the .bash_profile under /Users/myName and everything went back to normal.

然后我在 /Users/myName 下的 .bash_profile 中添加了以下几行,一切都恢复正常。

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

回答by Kaushik Velusamy

try

尝试

/miniconda3/bin/conda install python

python: 3.6.0-0 --> 3.6.1-2

and

/miniconda3/bin/conda install pandas

Try the same with your Anaconda version.

用你的 Anaconda 版本试试同样的方法。

回答by colo

Instead of installing it with conda or pip, try to install it with your package manager:

不要使用 conda 或 pip 安装它,而是尝试使用您的包管理器安装它:

sudo apt-get install python3-pandas

须藤 apt-get 安装 python3-pandas

回答by Jo?o Quintas

I was having the same problem now with Python 3.4.3.

我现在在使用 Python 3.4.3 时遇到了同样的问题。

I was using pandas-0.18.0.

我使用的是pandas-0.18.0。

Upgrading (using pip) solved the issue for me:

升级(使用 pip)为我解决了这个问题:

[sudo] pip install --upgrade pandas

[sudo] pip install --upgrade pandas

The final result of the upgrade:

升级的最终结果:

Successfully installed numpy-1.13.3 pandas-0.21.0 python-dateutil-2.6.1 pytz-2017.3 six-1.11.0

Successfully installed numpy-1.13.3 pandas-0.21.0 python-dateutil-2.6.1 pytz-2017.3 six-1.11.0

After this, the issue was gone!

之后,这个问题就没有了!

回答by WY Hsu

I tried all the solutions above, but nothing works out...

我尝试了上述所有解决方案,但没有任何效果......

Error Message

错误信息

I got an error message with ipython

我收到一条错误消息 ipython

ImportError: C extension: iNaT 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.

and it suggests

它建议

$ python setup.py build_ext --inplace --force

Solution

解决方案

My suggestion: Be careful about the version issue!

我的建议:小心版本问题!

I clone pandasfrom the official github repo, then build it myself and install by pip

pandas从官方 github repo克隆,然后自己构建并安装pip

Following is the command I typed in terminal

以下是我在终端中输入的命令

$ cd pandas

$ python setup.py build_ext --inplace --force

$ sudo pip install .  # don't forget the dot 

or, if you want to install in your personal Linux account instead of under the system (due to multiple users issue)

或者,如果您想安装在您的个人 Linux 帐户中而不是在系统下(由于多用户问题)

you can add --userflag

你可以添加--user标志

$ pip --user install . # don't forget the dot, too

Now, everything works fine on my laptop

现在,我的笔记本电脑一切正常

My configuration

我的配置

Ubuntu 16.04
Python 2.7
Numpy 1.13.1 

Good luck!

祝你好运!

回答by zawdd

I was having this problem with python 2.7.13 here is my solution: 1. install Cython with

我在使用 python 2.7.13 时遇到了这个问题,这是我的解决方案: 1. 安装 Cython

pip install Cython

2. install g++ and gcc

2.安装g++和gcc

apt-get install gcc, g++

3. uninstall pandas

3.卸载熊猫

pip uninstall pandas

4. reinstall pandas

4.重新安装熊猫

pip install pandas

then everything will be OK.

那么一切都会好起来的。

回答by John McCurdy

I had this issue when I needed up upgrade from Python 32 bit to 64 bit to use tensorflow.

当我需要从 Python 32 位升级到 64 位以使用 tensorflow 时,我遇到了这个问题。

Running this command uninstalled pandas 0.21 and reinstalled 0.22 :

运行此命令卸载了 pandas 0.21 并重新安装了 0.22 :

pip install --upgrade pandas

pip install --upgrade 熊猫

Sorted.

排序。

回答by Dimitry Boguslavsky

I was unable to upgrade pandas with regular

我无法使用常规升级熊猫

pip install --upgrade pandas 

"tensorflow 1.6.0 has requirement numpy>=1.13.3, but you'll have numpy 1.13.1 which is incompatible."

However bumping it with:

然而撞它:

pip install --upgrade pandas --force

solved issue completely

彻底解决问题

回答by Martin Thoma

I just had exactly the same issue when running tox.

我在运行时遇到了完全相同的问题tox

Steps to solve:

解决步骤:

  1. Update setup.pyto contain pandas==0.23.0(instead of 0.21.0).
  2. Remove .toxdirectory
  3. Run toxagain.
  1. 更新setup.py为包含pandas==0.23.0(而不是0.21.0)。
  2. 删除.tox目录
  3. 再跑tox