从pyxdameraulevenshtein导入会出现以下错误

pyxdameraulevenshtein==1.5.3
pandas==1.1.4
scikit-learn==0.20.2. 

Numpy是1.16.1。

在Python 3.6中工作良好,在Python 3.7中问题。

有人在使用Python 3.7(3.7.9)时遇到过类似的问题吗?

from pyxdameraulevenshtein import normalized_damerau_levenshtein_distance as norm_dl_dist
__init__.pxd:242: in init pyxdameraulevenshtein
    ???
E   ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject

当前回答

的确,(构建和)安装numpy>=1.20.0应该可以工作,如下面的答案所指出的。然而,我认为一些背景可能会很有趣,并提供替代解决方案。

numpy 1.20.0中的C API发生了变化。在某些情况下,pip似乎下载了numpy的最新版本用于构建阶段,但随后该程序使用已安装的numpy版本运行。如果在<1.20中使用的构建版本,但安装的版本是=>1.20,这将导致一个错误。

(反过来说也没关系,因为向后兼容。但是如果用户使用的是numpy<1.20的安装版本,他们就不会预料到即将到来的更改。)

这就引出了解决问题的几种可能方法:

升级(构建版本)到numpy>=1.20.0 在pyproject中使用最小支持的numpy版本。toml (oldest-supported-numpy) 使用——no-binary安装 使用——no-build-isolation安装

有关潜在解决方案的更详细讨论,请参见 https://github.com/scikit-learn-contrib/hdbscan/issues/457 # issuecomment - 773671043。

其他回答

这对我很有用(当这一页上的任何东西都不起作用时):

# Create environment with conda or venv.
# Do *not* install any other packages here.
pip install numpy==1.21.5
# Install all other packages here.
# This works as a package may build against the currently installed version of numpy.

这解决了一个特别残酷的问题,截至2022-04-11,本页上的所有其他答案都无法解决:

其他答案试图在问题发生后解决问题,这种方法在问题发生前解决问题。

此外,可以尝试不同版本的Python,例如3.8、3.9、3.10。

@FZeiser的回答很好,解释了为什么这种方法有效。

不升级numpy的解决方案

虽然升级numpy版本通常可以解决这个问题,但并不总是可行的。一个很好的例子是,当你使用tensorflow==2.6.0时,它与最新的numpy版本不兼容(它需要~=1.19.2)。

正如在FZeiser的回答中已经提到的,在1.20.0版本中numpys C API发生了变化。有一些包在构建时依赖于这个C API,例如pyxdameraulevenshtein。鉴于pips依赖解析器不保证安装包的任何顺序,可能会发生以下情况:

pip figures out that it needs to install numpy and it chooses the latest version, 1.21.2 as of the time writing this answer. It then builds a package that depends on numpy and its C API, e.g. pyxdameraulevenshtein. This package is now compatible with numpy 1.21.2 C API. At a later point pip needs to install a package that has a requirement for an older version of numpy, e.g. tensorflow==2.6.0 which would try to install numpy==1.19.5. As a result, numpy==1.21.2 is uninstalled and the older version is installed. When running code that uses pyxdameraulevenshtein, its current installation relies on the updated numpy C API, yet the numpy version was downgraded which would result in the error.

解决方案

您应该使用过时的numpy C API重新构建包,以确保它与当前安装的numpy版本兼容。例如,对于pyxdameraulevenshtein:

pip uninstall pyxdameraulevenshtein
pip install pyxdameraulevenshtein --no-binary pyxdameraulevenshtein

对于几乎相同的图像:python:3.7-slim-buster

我今天才开始有这个问题,以前是不存在的。

我通过从require .txt文件中删除numpy来解决这个问题,并在我的Dockerfile中执行以下操作:

RUN pip3 install --upgrade  --no-binary numpy==1.18.1 numpy==1.18.1 \
&& pip3 install -r requirements.txt 

我使用了一些旧版本的keras和它的库,升级到numpy 1.20.0对这些库不起作用。但我认为解决方案包含在我给你的第一个命令中,它告诉pip不要编译numpy,而是下载一个预编译的版本。

命令中的技巧是,您可能会发现有人告诉您使用pip的——no-binary选项来解决问题,但他们没有指定如何解决问题,这可能很棘手(就像我遇到的那样);您必须在命令中编写两次包才能使其工作,否则PIP将向您抛出一个错误。

我认为第一个命令中的——upgrade选项是不必要的。

对于任何使用诗歌的人来说,有必要进行实验。对于numpy<1.20依赖项的应用程序,New-installer设置为true才能正确构建,即:

poetry config experimental.new-installer true

默认情况下是正确的,但如果它被改变了(就像我的情况一样),它会让你识破。

我的应用程序使用Tensorflow,因此我没有升级到>1.20的选项。诗歌也不支持无二进制依赖。

对我有用的是:

pip uninstall numpy
conda install -y -c conda-forge numpy

虽然听起来很奇怪……我甚至不需要用Conda卸载它,这对我来说似乎很奇怪。我使用的是python 3.9