Python ValueError: numpy.dtype 大小错误,尝试重新编译

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

ValueError: numpy.dtype has the wrong size, try recompiling

pythonnumpyinstallpandasstatsmodels

提问by Amber Chen

I just installed pandas and statsmodels package on my python 2.7 When I tried "import pandas as pd", this error message comes out. Can anyone help? Thanks!!!

我刚刚在 python 2.7 上安装了 pandas 和 statsmodels 包当我尝试“将熊猫导入为 pd”时,出现此错误消息。任何人都可以帮忙吗?谢谢!!!

numpy.dtype has the wrong size, try recompiling
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\analytics\ext\python27\lib\site-packages\statsmodels-0.5.0-py2.7-win32.egg\statsmodels\formula\__init__.py",
line 4, in <module>
    from formulatools import handle_formula_data
  File "C:\analytics\ext\python27\lib\site-packages\statsmodels-0.5.0-py2.7-win32.egg\statsmodels\formula\formulatools.p
y", line 1, in <module>
    import statsmodels.tools.data as data_util
  File "C:\analytics\ext\python27\lib\site-packages\statsmodels-0.5.0-py2.7-win32.egg\statsmodels\tools\__init__.py", li
ne 1, in <module>
    from tools import add_constant, categorical
  File "C:\analytics\ext\python27\lib\site-packages\statsmodels-0.5.0-py2.7-win32.egg\statsmodels\tools\tools.py", line
14, in <module>
    from pandas import DataFrame
  File "C:\analytics\ext\python27\lib\site-packages\pandas\__init__.py", line 6, in <module>
    from . import hashtable, tslib, lib
  File "numpy.pxd", line 157, in init pandas.tslib (pandas\tslib.c:49133)
ValueError: numpy.dtype has the wrong size, try recompiling

回答by Josef

(to expand a bit on my comment)

(扩展一下我的评论)

Numpy developers follow in general a policy of keeping a backward compatible binary interface (ABI). However, the ABI is not forward compatible.

Numpy 开发人员通常遵循保持向后兼容的二进制接口 (ABI) 的策略。但是,ABI 不向前兼容。

What that means:

那是什么意思:

A package, that uses numpy in a compiled extension, is compiled against a specific version of numpy. Future version of numpy will be compatible with the compiled extension of the package (for exception see below). Distributers of those other packages do not need to recompile their package against a newer versions of numpy and users do not need to update these other packages, when users update to a newer version of numpy.

在编译扩展中使用 numpy 的包是针对特定版本的 numpy 编译的。numpy 的未来版本将与包的编译扩展兼容(例外情况见下文)。当用户更新到较新版本的 numpy 时,这些其他包的分发者不需要针对较新版本的 numpy 重新编译他们的包,并且用户不需要更新这些其他包。

However, this does not go in the other direction. If a package is compiled against a specific numpy version, say 1.7, then there is no guarantee that the binaries of that package will work with older numpy versions, say 1.6, and very often or most of the time they will not.

然而,这并没有朝着另一个方向发展。如果一个包是针对特定的 numpy 版本编译的,比如 1.7,那么不能保证该包的二进制文件可以与较旧的 numpy 版本一起使用,比如 1.6,并且经常或大部分时间它们不会。

The binary distribution of packages like pandas and statsmodels, that are compiled against a recent version of numpy, will not work when an older version of numpy is installed. Some packages, for example matplotlib, if I remember correctly, compile their extensions against the oldest numpy version that they support. In this case, users with the same old or any more recent version of numpy can use those binaries.

当安装了旧版本的 numpy 时,针对最新版本的 numpy 编译的包的二进制分发包(如 pandas 和 statsmodels)将不起作用。一些包,例如 matplotlib,如果我没记错的话,会根据它们支持的最旧的 numpy 版本编译它们的扩展。在这种情况下,具有相同旧版本或任何更新版本的 numpy 的用户可以使用这些二进制文件。

The error message in the question is a typical result of binary incompatibilities.

问题中的错误消息是二进制不兼容的典型结果。

The solution is to get a binary compatible version, either by updating numpy to at least the version against which pandas or statsmodels were compiled, or to recompile pandas and statsmodels against the older version of numpy that is already installed.

解决方案是获得二进制兼容版本,方法是将 numpy 至少更新为编译 pandas 或 statsmodels 的版本,或者针对已安装的旧版本 numpy 重新编译 pandas 和 statsmodels。

Breaking the ABI backward compatibility:

打破 ABI 向后兼容性:

Sometimes improvements or refactorings in numpy break ABI backward compatibility. This happened (unintentionally) with numpy 1.4.0. As a consequence, users that updated numpy to 1.4.0, had binary incompatibilities with all other compiled packages, that were compiled against a previous version of numpy. This requires that all packages with binary extensions that use numpy have to be recompiled to work with the ABI incompatible version.

有时 numpy 中的改进或重构会破坏 ABI 向后兼容性。这(无意)发生在 numpy 1.4.0 中。因此,将 numpy 更新到 1.4.0 的用户与所有其他编译包的二进制不兼容,这些包是针对以前版本的 numpy 编译的。这要求所有使用 numpy 的二进制扩展包都必须重新编译才能使用 ABI 不兼容的版本。

回答by Calimo

I had a similar error with another library and realized that I had several versions of numpy installed on my system. The fix for me was to edit my PYTHONPATH and put the site-packages that contained the latest version of numpy in first position.

我在另一个库中遇到了类似的错误,并意识到我的系统上安装了多个版本的 numpy。我的解决方法是编辑我的 PYTHONPATH 并将包含最新版本 numpy 的站点包放在第一位。

回答by Houcheng

I also encounter this error when use pandas to access MYSQL. This error message indicates a binary compatible issue and can be resolved by using latest version of pandas and numpy package. Here is my steps to resolve this issue, and it works well on my Ubuntu 12.04:

我在使用pandas访问MYSQL时也遇到这个错误。此错误消息表明存在二进制兼容问题,可以通过使用最新版本的 pandas 和 numpy 包来解决。这是我解决此问题的步骤,它在我的 Ubuntu 12.04 上运行良好:

cd /tmp/
wget https://pypi.python.org/packages/source/p/pandas/pandas-0.12.0.tar.gz
tar xzvf pandas-0.12.0.tar.gz
cd pandas-0.12.0
easy_install --upgrade numpy

回答by Paul

In my case, I had installed pandas-0.10.0.win-amd64-py2.7 but was checking to see if a bug had been fixed in a more recent version of pandas. So I did an easy_install -U to force the upgrade, but then got the above error due to some incompatibilities with numpy etc... when I did

就我而言,我已经安装了 pandas-0.10.0.win-amd64-py2.7,但正在检查是否在更新版本的 Pandas 中修复了错误。所以我做了一个 easy_install -U 来强制升级,但是由于与 numpy 等的一些不兼容而得到了上述错误......当我这样做的时候

import pandas

To fix, I just reinstalled the pandas-0.10.0.win-amd64-py2.7 binary and everything works. I didn't see this answer (suggests to use pip) which may have helped me (though not sure) Install particular version with easy_install

为了修复,我刚刚重新安装了 pandas-0.10.0.win-amd64-py2.7 二进制文件,一切正常。我没有看到这个答案(建议使用 pip),这可能对我有帮助(虽然不确定)使用 easy_install 安装特定版本

Also this highlights why one should use virtualenv (which I wasn't).

这也突出了为什么应该使用 virtualenv (我不是)。

回答by user3915137

For me (Mac OS X Mavericks) it worked to install the version for python2.6:

对我来说(Mac OS X Mavericks)它可以安装 python2.6 版本:

sudo port install py26-scikit-learn

then run:

然后运行:

python2.6 myscript.py

回答by ph7

The problem I solved on Webfaction was old numpy library(1.5) which was in conflict with my fresh

我在 Webfaction 上解决的问题是旧的 numpy 库(1.5),它与我的新库相冲突

pip install pandas

pip 安装熊猫

installation in .virtualenv.

在 .virtualenv 中安装。

The problem was solved after I did pip install pandas out of the virtual environment. The idea came from discussion on https://github.com/pydata/pandas/issues/3711, thanks, cpcloud!

在我从虚拟环境中执行 pip install pandas 后,问题解决了。这个想法来自https://github.com/pydata/pandas/issues/3711 上的讨论,谢谢,cpcloud

回答by Fedor Chervinskii

For me (Mac OS X Maverics, Python 2.7)

对我来说(Mac OS X Maverics,Python 2.7)

easy_install --upgrade numpy

helped. After this you can install up-to-date packages pandas, scikit-learn, e.t.c. using pip:

有帮助。在此之后,您可以使用pip安装最新的包pandasscikit-learn等:

pip install pandas

回答by Yuhao

I just meet this 'ValueError' issue and have addressed it. Definitely there's something wrong with numpy package.

我刚刚遇到了这个“ValueError”问题并解决了它。numpy 包肯定有问题。

But when I try to pip install --upgrade numpyit failed, so I uninstall and download the newest numpy.zipfile. Then manuallyuncompress and python setup.py installit.

但是当我尝试pip install --upgrade numpy失败时,我卸载并下载了最新的numpy.zip文件。然后手动解压python setup.py install就可以了。

Luckly, it works!

幸运的是,它有效!

回答by RexFuzzle

I found it to be a simple version being outdated or mismatch and was fixed with:

我发现它是一个过时或不匹配的简单版本,并修复了:

pip install --upgrade numpy
pip install --upgrade scipy
pip install --upgrade pandas

Or might work with the one liner:

或者可能与一个班轮一起工作:

pip install --upgrade numpy scipy pandas

回答by Tal

As in here, for me only sudo pip install pandas==0.13.1worked

就像在这里一样,对我来说只sudo pip install pandas==0.13.1工作