树莓派4安装OpenCV-python失败求助:编译wheel及skbuild报错
问题描述
在Visual Studio Code中操作树莓派4,执行命令pip install opencv-python等待40分钟后出现报错:
FileNotFoundError: [Errno 2] No such file or directory: '_skbuild/linux-armv7l-3.9/cmake-install/python/cv2/config-3.py'
[end of output]note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for opencv-python
Failed to build opencv-python
ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects
已尝试升级pip后重新安装,问题未解决。当前使用Python版本为3.9.2。
补充:执行命令pip3 install --no-use-pep517 opencv-python时出现新报错:
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [6 lines of output]
Traceback (most recent call last):
File "", line 2, in
File "", line 34, in
File "/tmp/pip-install-zwr61c7o/opencv-python_1867909748974f69a8333e238206f9df/setup.py", line 10, in
import skbuild
ModuleNotFoundError: No module named 'skbuild'
[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.
解决步骤
1. 安装缺失的构建依赖
针对skbuild缺失的问题,先安装系统和Python依赖:
sudo apt-get update && sudo apt-get install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev pip install scikit-build setuptools wheel
2. 使用系统预编译包安装(推荐)
树莓派上编译OpenCV耗时极长,直接用系统包管理器安装预编译版本,速度快且稳定:
sudo apt-get install -y python3-opencv
安装完成后,在Python中验证:
import cv2 print(cv2.__version__)
3. 指定兼容版本通过pip安装
如果需要特定版本的OpenCV,尝试指定与Python3.9兼容的版本(该版本提供了armv7l预编译轮子):
pip install opencv-python==4.5.5.62
4. 清理缓存后重试
若之前的编译缓存导致异常,清理后重新安装:
pip cache purge pip install opencv-python --no-cache-dir
内容的提问来源于stack exchange,提问作者Ramin Moradi

