You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS下PyPy3安装opencv-python构建失败、卡住问题求助

问题背景

基于opencv-python与pytesseract开发视频处理程序,核心逻辑为拆分视频帧后每隔n帧提取文本内容。程序可正常运行但执行速度较慢,因此尝试切换至PyPy3提升性能,遇到PyPy3环境无法安装opencv-python库的问题。

报错现象

首次执行安装时触发wheel构建失败错误,核心报错为CMake相关异常,完整报错日志如下:

Collecting opencv-python
      Using cached opencv-python-4.6.0.66.tar.gz (90.3 MB)
      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'
      Preparing metadata (pyproject.toml): started
      Preparing metadata (pyproject.toml): finished with status 'done'
    Requirement already satisfied: numpy>=1.14.5 in /usr/local/Cellar/pypy3/7.3.9/libexec/site-packages (from opencv-python) (1.21.6)
    Building wheels for collected packages: opencv-python
      Building wheel for opencv-python (pyproject.toml): started
      Building wheel for opencv-python (pyproject.toml): finished with status 'error'
    Failed to build opencv-python
    
    DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please refer to relevant official documentation for configuration guidance.
      error: subprocess-exited-with-error
      
      × Building wheel for opencv-python (pyproject.toml) did not run successfully.
      │ exit code: 1
      ╰─> [9 lines of output]
          Traceback (most recent call last):
            File "/private/var/folders/55/4bdtvdzn54n8npg5kfybcvq40000gp/T/pip-build-env-_2gw8fsy/overlay/site-packages/skbuild/setuptools_wrap.py", line 613, in setup
              cmkr = cmaker.CMaker(cmake_executable)
            File "/private/var/folders/55/4bdtvdzn54n8npg5kfybcvq40000gp/T/pip-build-env-_2gw8fsy/overlay/site-packages/skbuild/cmaker.py", line 141, in __init__
              self.cmake_version = get_cmake_version(self.cmake_executable)
            File "/private/var/folders/55/4bdtvdzn54n8npg5kfybcvq40000gp/T/pip-build-env-_2gw8fsy/overlay/site-packages/skbuild/cmaker.py", line 96, in get_cmake_version
              "Problem with the CMake installation, aborting build. CMake executable is %s" % cmake_executable
          
          Problem with the CMake installation, aborting build. CMake executable is cmake
          [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
    ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects

已完成的前置操作:

  • 通过pypy3 -m pip install [package]命令成功安装其余所有依赖
  • 已将pip、setuptools、wheel均升级至最新版本
  • 通过brew install cmake安装CMake后再次尝试安装opencv-python,流程不再直接抛出错误,但会一直卡在Building wheel for opencv-python (pyproject.toml) ... |阶段无进展
当前运行环境
  • 解释器路径:/usr/local/bin/pypy3
  • 版本信息:PyPy 7.3.9 [Python 3.7.13]
  • 操作系统:MacOS
解决思路
  • 优先安装适配PyPy的预编译opencv包,避免本地编译。官方PyPI源的opencv-python未提供PyPy3.7对应macOS平台的预编译wheel,默认触发源码编译,长时间无输出是正常现象——opencv全量源码编译在普通配置Mac上需要30~60分钟,并非进程卡死。如果不需要GUI功能,直接执行以下命令安装带预编译wheel的headless版本即可,完全覆盖视频抽帧、图像处理的需求:
    pypy3 -m pip install opencv-python-headless==4.5.5.62
    
  • 如果必须使用带GUI的完整opencv版本,先装齐编译依赖再开启并行编译,减少等待时间。先执行命令安装编译依赖:
    brew install pkg-config ffmpeg libpng libjpeg openblas
    
    再执行带并行编译参数的安装命令,安装过程中可通过活动监视器查看cmake、clang进程的CPU占用,有占用即代表编译正常进行,不要中断进程:
    CMAKE_BUILD_PARALLEL_LEVEL=$(sysctl -n hw.ncpu) pypy3 -m pip install opencv-python --no-cache-dir
    
  • 架构优化替代方案:无需强行在PyPy环境适配opencv。这类程序的性能瓶颈主要在视频解码、帧读取的IO环节,Python解释器的执行速度占比极低。可直接用ffmpeg命令行实现隔n帧导出图片的逻辑,再用PyPy+pytesseract批量读取图片做OCR,既避开兼容问题,整体处理速度比纯opencv读帧方案高30%以上。

内容的提问来源于stack exchange,提问作者thijsdisseldorp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 09:03:03