安装opencv-python失败:numpy构建出错,配置cmake仍无效
问题描述
在Windows系统的虚拟环境myvenv中执行pip install opencv-python时安装失败,pip尝试构建numpy 2.0.2过程中出现错误,提示无法找到Python3库文件E:\Python\myvenv\lib\libpython3.9.dll.a,无法检测到Python依赖。已配置cmake并多次尝试安装,但问题仍未解决。
具体报错日志
(myvenv) PS E:\Python> pip install opencv-python Collecting opencv-python Using cached opencv-python-4.10.0.84.tar.gz (95.1 MB) Installing build dependencies ... error error: subprocess-exited-with-error × pip subprocess to install build dependencies did not run successfully. │ exit code: 1 ╰─> [66 lines of output] Ignoring numpy: markers 'python_version == "3.6" and platform_machine != "aarch64" and platform_machine != "arm64"' don't match your environment Ignoring numpy: markers 'python_version == "3.7" and platform_machine != "aarch64" and platform_machine != "arm64"' don't match your environment Ignoring numpy: markers 'python_version == "3.8" and platform_machine != "aarch64" and platform_machine != "arm64"' don't match your environment Ignoring numpy: markers 'python_version < "3.9" and sys_platform == "linux" and platform_machine == "aarch64"' don't match your environment Ignoring numpy: markers 'python_version < "3.9" and sys_platform == "darwin" and platform_machine == "arm64"' don't match your environment Collecting cmake>=3.1 Using cached cmake-3.30.3.tar.gz (31 kB) Installing build dependencies: started Installing build dependencies: finished with status 'done' Getting requirements to build wheel: started Getting requirements to build wheel: finished with status 'done' Installing backend dependencies: started Installing backend dependencies: finished with status 'done' Preparing metadata (pyproject.toml): started Preparing metadata (pyproject.toml): finished with status 'done' Collecting numpy>=2.0.0 Downloading numpy-2.0.2.tar.gz (18.9 MB) ---------------------------------------- 18.9/18.9 MB 3.2 MB/s eta 0:00:00 Installing build dependencies: started Installing build dependencies: finished with status 'done' Getting requirements to build wheel: started Getting requirements to build wheel: finished with status 'done' Installing backend dependencies: started Installing backend dependencies: finished with status 'done' Preparing metadata (pyproject.toml): started Preparing metadata (pyproject.toml): finished with status 'error' error: subprocess-exited-with-error Preparing metadata (pyproject.toml) did not run successfully. exit code: 1 [23 lines of output] + E:\Python\myvenv\bin\python.exe C:\Users\admin\AppData\Local\Temp\pip-install-nzei4sh_\numpy_a368f9e2aa274993be96d795ef79ddd4\vendored-meson\meson\meson.py setup C:\Users\admin\AppData\Local\Temp\pip-install-nzei4sh_\numpy_a368f9e2aa274993be96d795ef79ddd4 C:\Users\admin\AppData\Local\Temp\pip-install-nzei4sh_\numpy_a368f9e2aa274993be96d795ef79ddd4\.mesonpy-cclotgs2 -Dbuildtype=release -Db_ndebug=if-release -Db_vscrt=md --native-file=C:\Users\admin\AppData\Local\Temp\pip-install-nzei4sh_\numpy_a368f9e2aa274993be96d795ef79ddd4\.mesonpy-cclotgs2\meson-python-native-file.ini The Meson build system Version: 1.4.99 Source dir: C:\Users\admin\AppData\Local\Temp\pip-install-nzei4sh_\numpy_a368f9e2aa274993be96d795ef79ddd4 Build dir: C:\Users\admin\AppData\Local\Temp\pip-install-nzei4sh_\numpy_a368f9e2aa274993be96d795ef79ddd4\.mesonpy-cclotgs2 Build type: native build Project name: NumPy Project version: 2.0.2 C compiler for the host machine: cc (gcc 12.1.0 "cc (Rev2, Built by MSYS2 project) 12.1.0") C linker for the host machine: cc ld.bfd 2.38 C++ compiler for the host machine: c++ (gcc 12.1.0 "c++ (Rev2, Built by MSYS2 project) 12.1.0") C++ linker for the host machine: c++ ld.bfd 2.38 Cython compiler for the host machine: cython (cython 3.0.11) Host machine cpu family: x86_64 Host machine cpu: x86_64 Program python found: YES (E:\Python\myvenv\bin\python.exe) Found pkg-config: YES (C:\mingw64\bin\pkg-config.EXE) 1.8.0 Could not find Python3 library 'E:\\Python\\myvenv\\lib\\libpython3.9.dll.a' Run-time dependency python found: NO (tried pkgconfig, pkgconfig and sysconfig) ..\meson.build:41:12: ERROR: Python dependency not found A full log can be found at C:\Users\admin\AppData\Local\Temp\pip-install-nzei4sh_\numpy_a368f9e2aa274993be96d795ef79ddd4\.mesonpy-cclotgs2\meson-logs\meson-log.txt [end of output] 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. [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × pip subprocess to install build dependencies did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip.
解决方案
- 安装兼容的预编译numpy版本:执行
pip install numpy==1.26.4 opencv-python,numpy 2.x对Windows虚拟环境的源码构建支持不完善,1.x版本有成熟的预编译wheel包,可避免源码构建问题。 - 用官方Python重建虚拟环境:确保使用python.org下载的官方Python创建虚拟环境,MinGW等非官方Python发行版可能缺少
libpython3.9.dll.a这类文件,导致构建失败。 - 强制使用预编译wheel:执行
pip install opencv-python --only-binary :all:,跳过源码构建,直接下载适配Windows的预编译包。 - 调整环境变量优先级:检查系统环境变量,确保官方Python的路径优先级高于MinGW,避免构建工具错误调用MinGW的编译器和pkg-config。
内容的提问来源于stack exchange,提问作者Minh Quang
相关产品推荐
相关产品推荐

