Build fatal error: Python.h: No such file or directory

Hello!

The standard response for “fatal error: Python.h” No such file or directory does not seem to work for my platform.

I have cloned the opencv repository:

On branch 4.x
Your branch is up to date with 'origin/4.x'.

After using ccmake/cmake with default settings, my make fails with the following error message:

[100%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o
In file included from /home/chowkidar/projects/opencv/modules/python/src2/cv2.cpp:5:
/home/chowkidar/projects/opencv/modules/python/src2/cv2.hpp:20:10: fatal error: Python.h: No such file or directory
   20 | #include <Python.h>
      |          ^~~~~~~~~~
compilation terminated.

My configuration is:

$ python3 --version
Python 3.11.2
$ which python3
/usr/bin/python3
$ sudo apt install libpython3-dev
libpython3-dev is already the newest version (3.11.2-1+b1).
$ sudo apt install python3-dev
python3-dev is already the newest version (3.11.2-1+b1).
$ g++ --version
g++ (Debian 12.2.0-14) 12.2.0
$ uname -a
Linux raspberrypi 6.1.21-v8+ #1642 SMP PREEMPT Mon Apr  3 17:24:16 BST 2023 aarch64 GNU/Linux
OS: Debian GNU/Linux 12 (bookworm) aarch64
Host: Raspberry Pi 3 Model B Rev 1.2
$ find / | grep "Python.h"
/usr/include/python3.11/Python.h

ccmake is pointing to the following Python3 include directory:
PYTHON3_INCLUDE_DIR /usr/include/python3.11

What is the simplest way to turn off compilation for opencv_contrib using ccmake/cmake but ensure all opencv core programs are built so that I can make progress on a basic build? Thanks.

Regards.

please add:

  • os, opencv version
  • your cmake cmdline, and resp. output
  • do you actually whish for python support ? (why not use pip ?)
  • why are you trying to disable contrib modules ?

@berak,

Here are some quick responses to your question:
os: Raspberry Pi OS a.k.a. Debian GNU/Linux 12 (bookworm) aarch64 (as listed in the original note)
opencv version: Release 61, 4.8.1 (noted implicitly in the original note)
cmake cmdline: you would really need the CMakeCache.txt? cmdline, per se, is cmake …/opencv where the build folder and the opencv source are separate
do you actually…?: want to build from source (I understand what you are saying, of course, but don’t want to go off-topic)
why are you…?: unproven hunch that the problem may lie with a contrib make file

Thanks.

there are python3 cmake args, so, how did you invoke it, and what did it print out ?

Hello @berak,

My apologies for wasting everyone’s time. The issue has been resolved. I have a nightly Ansible playbook that runs update/upgrade in the farm that may have fixed something - purely an ignorant guess. The preceding git operations didn’t pull anything fresh from the master so it was clearly my issue alone.

The error message was generated at the 100% mark for opencv_version as noted in the original note. This afternoon’s run for the make had no errors:

[100%] Built target opencv_version

The subsequent sudo make install listed the following corresponding line:

-- Up-to-date: /usr/local/bin/opencv_version

And, of course, the proof is the following validation:

$ python3
Python 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.8.0-dev'
>>> exit()

Many, many thanks for the positive responses here. Can’t express how much I appreciate it. Thanks.

Regards.
P.S.
On a personal note, I have being doing builds with OpenCV source since 2014 with varying challenges. I have stopped doing builds on the older RPi boards because the pre-built packages now come out very quickly. However, I just like to learn a little bit more now and then, and therefore limit myself to RPi 3 & 4 boards for the build from source.