Bugfighter日记:python 库numpy2.x和opencv的不兼容(不要下载最新版本!!)

1.下载最新版本时要谨慎

2.看报错信息

3思路

报错信息a:

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.0 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

此报错信息表示opencv无法用2.x版本的numpy运行。而最新版本仅为2.0.0刚好不能用!!

于是我尝试用

pip install numpy==1000

这种不合法语句调出现有版本

继续尝试安装。可是安装进程又报错了。

 note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

        NOT AVAILABLE

      lapack_info:

      No module named 'numpy.distutils._msvccompiler' in numpy.distutils; trying from distutils

      customize MSVCCompiler

发现锅主要在于distutils这个东西。

找文心一言:

numpy.distutils._msvccompiler 是 NumPy 包中用于与 Microsoft Visual C++ (MSVC) 编译器交互的模块。这个模块是 NumPy 的构建系统的一部分,用于在 Windows 平台上编译扩展模块,尤其是当 NumPy 需要与本地代码(如 C、C++ 或 Fortran 代码)交互时。

当你在 Windows 上安装或构建 NumPy(或许多其他需要编译的 Python 包)时,numpy.distutils._msvccompiler 会被用来检测、配置和使用 MSVC 编译器。这个模块会尝试自动找到系统上安装的 MSVC 编译器,并设置适当的编译选项。

然而,这个模块通常是 NumPy 内部使用的,并且对于大多数用户来说,不需要直接与其交互。如果你是一个包开发者,并且需要编译包含本地代码的 Python 扩展,那么你可能会间接地通过这个模块与 MSVC 编译器进行交互。

请注意,随着 NumPy 和其他 Python 包的更新,这个模块的具体实现和接口可能会发生变化。因此,如果你正在开发需要编译本地代码的 Python 包,并且需要直接与此模块交互,建议查阅最新的 NumPy 文档和源代码以获取最准确的信息。

另外,从 NumPy 1.19.0 版本开始,NumPy 提供了对 meson 和 ninja 的支持,作为替代 MSVC 的另一种构建系统选项。这可能会影响到 numpy.distutils._msvccompiler 的使用,但具体取决于你的构建环境和需求。锅又在最新版本上

numpy最新版本不仅使我无法使用opencv

还使我无法在原有库的基础上安装其他版本的numpy,必须消灭其存在。

解决方案

1.uninstall删除numpy2.0

2重要前提:pip没问题