OpenCV Python on Ubuntu branch question – follow up

  Kiến thức lập trình

Setup: Trying to do python development with Anaconda 3, PyCharm Community Edition (2023.3.2) on Ubuntu (22.04.3 LTS) and a lot of existing packages including opencv-python. I was having the problem described in question

Python OpenCV Error: Current thread is not the object’s thread

but is really a new question because I apparently have a configuration problem of some sort. In short, CV2 calls in Python 3 result in an error that looks like this:

QObject::moveToThread: Current thread () is not the object’s thread (0x1e57d70).
Cannot move to target thread ().

The recommended solution that seemed to help other folks was to first uninstall the opencv-python package and then reinstall it having the package rebuilt on the target platform with this command:

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

While trying this, the build gets to 100% when I get a somewhat implausible error that Python.h can’t be found. The full error is pasted below but a little background might help. While I’m not new to Linux, I’m a little new to Ubuntu and newer still to development (more of a Systems than Software Engineer). If these installers (apt, apt-get, pip et al.) can’t resolve the dependencies and paths, it will be difficult for me to figure it out. The PyCharm package view seems to be pretty good and its connection to conda seems to be okay (simple programs work okay) but something underneath is not right. The connection to gcc perhaps? I appreciate any and all suggestions and offers of assistance. Thanks in advance.


  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/", 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/", 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/", line 742, in make_impl
      raise SKBuildError(msg)
  An error occurred while building with CMake.
      /tmp/pip-build-env-deg9zvgy/overlay/lib/python3.10/site-packages/cmake/data/bin/cmake --build . --target install --config Release --
    Install target:
    Source directory:
    Working directory:
  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

New contributor

Rocky714 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.