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

Ubuntu下Anaconda环境编译opencv-python时Python.h缺失求助

解决OpenCV编译时Python.h缺失问题

问题背景

环境配置

  • Ubuntu 22.04.3 LTS
  • Anaconda 3 + PyCharm Community Edition 2023.3.2
  • 已安装opencv-python等Python开发包

初始错误

执行CV2调用时触发线程相关错误:

QObject::moveToThread: Current thread (<address>) is not the object's thread (0x1e57d70).
Cannot move to target thread (<address>).

尝试操作后的新错误

卸载opencv-python后,执行编译安装命令:

pip install --no-binary opencv-python opencv-python

编译到100%时触发Python.h: No such file or directory错误,完整日志如下:

Note: Class cv::Feature2D has more than 1 base class (not supported by Python C extensions)
            Bases:  cv::Algorithm, cv::class, cv::Feature2D, cv::Algorithm
            Only the first base class will be used
      Note: Class cv::detail::GraphCutSeamFinder has more than 1 base class (not supported by Python C extensions)
            Bases:  cv::detail::GraphCutSeamFinderBase, cv::detail::SeamFinder
            Only the first base class will be used
      [100%] Built target gen_opencv_python_source
      [100%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o
      In file included from /tmp/pip-install-s65xv3yn/opencv-python_f0507c14ed7b488894517f39f468dff8/opencv/modules/python/src2/cv2.cpp:5:
      /tmp/pip-install-s65xv3yn/opencv-python_f0507c14ed7b488894517f39f468dff8/opencv/modules/python/src2/cv2.hpp:20:10: fatal error: Python.h: No such file or directory
         20 | #include <Python.h>
            |          ^~~~~~~~~~
      compilation terminated.
      gmake[2]: *** [modules/python3/CMakeFiles/opencv_python3.dir/build.make:76: modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o] Error 1
      gmake[1]: *** [CMakeFiles/Makefile2:2304: modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
      gmake: *** [Makefile:166: all] Error 2
      Traceback (most recent call last):
        File "/tmp/pip-build-env-deg9zvgy/overlay/lib/python3.10/site-packages/skbuild/setuptools_wrap.py", line 674, in setup
          cmkr.make(make_args, install_target=cmake_install_target, env=env)
        File "/tmp/pip-build-env-deg9zvgy/overlay/lib/python3.10/site-packages/skbuild/cmaker.py", line 697, in make
          self.make_impl(clargs=clargs, config=config, source_dir=source_dir, install_target=install_target, env=env)
        File "/tmp/pip-build-env-deg9zvgy/overlay/lib/python3.10/site-packages/skbuild/cmaker.py", line 742, in make_impl
          raise SKBuildError(msg)
      
      An error occurred while building with CMake.
        Command:
          /tmp/pip-build-env-deg9zvgy/overlay/lib/python3.10/site-packages/cmake/data/bin/cmake --build . --target install --config Release --
        Install target:
          install
        Source directory:
          /tmp/pip-install-s65xv3yn/opencv-python_f0507c14ed7b488894517f39f468dff8
        Working directory:
          /tmp/pip-install-s65xv3yn/opencv-python_f0507c14ed7b488894517f39f468dff8/_skbuild/linux-x86_64-3.10/cmake-build
      Please check the install target is valid and see CMake's output for more information.
      
      [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

解决方案

方案1:安装系统Python开发包

如果Anaconda环境依赖系统Python,先安装对应版本的Python开发依赖:

sudo apt-get update
sudo apt-get install python3.10-dev  # 替换为你的Anaconda环境对应Python版本

安装完成后重新执行编译安装命令。

方案2:指定Anaconda的Python头文件路径(推荐)

针对Anaconda环境,直接让编译工具找到Anaconda自带的Python头文件:

  1. 激活目标conda环境:
conda activate <你的环境名称>
  1. 执行编译安装时指定头文件路径:
C_INCLUDE_PATH=$(python -c "import sysconfig; print(sysconfig.get_path('include'))") \
CPLUS_INCLUDE_PATH=$(python -c "import sysconfig; print(sysconfig.get_path('include'))") \
pip install --no-binary opencv-python opencv-python

方案3:换用conda安装OpenCV(避免编译)

无需手动编译,直接安装适配conda环境的预编译版OpenCV,同时可解决初始线程问题:

conda activate <你的环境名称>
conda remove opencv-python
conda install -c conda-forge opencv

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 00:57:47