1
votes

I wanted to install Opencv and followed this tutorial : https://medium.com/@sourabhjigjinni/install-opencv-4-0-0-for-c-windows-7-10-code-blocks-tdm-gcc-64-dff65addf162

I followed it step by step, built it in code blocks, but at about 99% built i got the error : fatal error : Python.h : no such file or directory.

Pc : Windows 10, 64 bit, python3 installed.

I added the full path to the python include directory in the compiler's "search directories" section. I checked if the path to python.exe and to the include directory were filled while generating the Makefiles with Cmake. I aslo checked lots of forums but never found a satisfying answer...

[ 99%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.obj
cd /d C:\opencv\build\modules\python3 && C:\TDM-GCC-64\bin\g++.exe  -DCVAPI_EXPORTS -D_USE_MATH_DEFINES -D__OPENCV_BUILD=1 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS @CMakeFiles/opencv_python3.dir/includes_CXX.rsp -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -Wno-unused-function -Wno-deprecated-declarations -Wno-overloaded-virtual -Wno-undef -O3 -DNDEBUG  -DNDEBUG   -std=c++11 -o CMakeFiles\opencv_python3.dir\__\src2\cv2.cpp.obj -c C:\opencv\source\opencv-4.1.0\modules\python\src2\cv2.cpp

C:\opencv\source\opencv-4.1.0\modules\python\src2\cv2.cpp:9:20: fatal error: Python.h: No such file or directory

compilation terminated.
mingw32-make.exe[2]: *** [modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.obj] Error 1
mingw32-make.exe[1]: *** [modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
modules\python3\CMakeFiles\opencv_python3.dir\build.make:62: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.obj' failed
mingw32-make.exe[2]: Leaving directory 'C:/opencv/build'
CMakeFiles\Makefile2:3512: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/all' failed
mingw32-make.exe[1]: Leaving directory 'C:/opencv/build'
C:/opencv/build/Makefile:161: recipe for target 'all' failed
Process terminated with status 2 (0 minute(s), 37 second(s))
4 error(s), 0 warning(s) (0 minute(s), 37 second(s))

Well, I tried all i knew to let the compiler know where Python.h was, but it didn't seem to work. Let me know if you need more data.

2
I added the full path to the python include directory in the compiler's search directories. My guess is that you haven't done this step correctly because it should say something like -I/full/path/to/python-include-directory/ in the line cd /d C:\opencv\build\modules\python3 && C:\TDM-GCC-64\bin\g++.exe -DCVAPI_EXPORTS ... - jackw11111
Thanks for your reply ! I added C:\Users\Me\AppData\Local\Programs\Python\Python37\include in the Compiler - search directory section, shouldn't it be enough? - Thibal
What is weird is that if a build a test project with : include <Python.h>, it works perfectly fine... Only OpenCV.cbp doesn't work... - Thibal
Just to be sure, did you add that path to code::blocks or Cmake? - jackw11111

2 Answers

1
votes

Ok, I rebuilt all from scratches (deleted the content of opencv/build folder + re-generated CMakeFiles with Cmake) and it built itself without any error. My mistakes : - The first time I built OpenCV.cbp, my python include dir path was not specified in "search directories" in code::blocks. - After few unsuccessful builds, I changed my python version, and actualized all the python paths in Cmake, and added the include dir path to code::blocks

The issue : OpenCV.cbp did not take into account all the changes, even if rebuilt it (idk why), and kept asking for Python.h

To solve the problem : BEFORE building OpenCV.cbp, make sure the python paths specified in Cmake are correct, and add the python include directory to code::blocks "search directories", which was not my case. Then, build !

0
votes

It seems Cmake can't find the Python.h header file in the directory @Cmakefiles/opencv_python.dir.

Things to try:

  • You may need to try to find how to set the @Cmakefiles/opencv_python.dir flag in the Cmake interface, or manually in CMakeLists.txt, and set it to the right Python.h location.

  • Also, make sure Cmakes Python include path flag is correct (PYTHON_INCLUDE=/path/to/python/include/).

  • Lastly, make sure your Python path is correctly set by following this post here with your Python location.

Edit: I was able to follow the instructions and get the webcam demonstration to build and run. This was tested on Windows 10, with Python 3.7.1. Unsure of what to try short of a complete reinstall. If you have any questions or want to know specific information let me know,and if I think of anything to try I will edit this post. Goodluck.