从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的解决方案

虽然升级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

其他回答

我在使用tensorflow对象api时遇到了这个问题。Tensorflow目前不兼容numpy==1.20(尽管这个问题直到后面才会显现出来)。 在我的例子中,这个问题是由pycocotools引起的。我通过安装旧版本来解决这个问题。

pip install pycocotools==2.0.0

我用的是Python 3.8.5。这听起来太简单了,但我有同样的问题,我所做的只是重新安装numpy。一去不复返了。

pip install --upgrade numpy

or

pip uninstall numpy
pip install numpy

我在python3.10.4,numpy1.21.5中遇到了同样的问题,我只有在通过pip卸载numpy和pip安装numpy将numpy更新到1.22.3后才解决了这个问题。只有pip install -upgrade numpy没有工作。

PS D:\quant\vnpy-master\examples\veighna_trader> python .\run.py Traceback (most recent call last): File "D:\quant\vnpy-master\examples\veighna_trader\run.py", line 31, in from vnpy_optionmaster import OptionMasterApp File "D:\it_soft\python3.10.4\Lib\site-packages\vnpy_optionmaster__init__.py", line 26, in from .engine import OptionEngine, APP_NAME File "D:\it_soft\python3.10.4\Lib\site-packages\vnpy_optionmaster\engine.py", line 34, in from .pricing import binomial_tree_cython as binomial_tree File "binomial_tree_cython.pyx", line 1, in init binomial_tree_cython ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject

Numpy 1.22版本为我解决了这个问题。

升级numpy版本:

pip install -U numpy