0

Summary

For OpenCV environment, after CMake configuration, I tried make, but error occurred, saying Python.h: No such file or directory.

Environment

  • ubuntu 18.04,
  • anaconda3-5.3.1 on pyenv,
  • python3.7,
  • opencv(4.0.1) and opencv_contrib(4.0.1)

What I've done

First, I tried CMake configure with some options below.$CONDA_HOME is equal to /home/qcmp/anaconda3.

(cv) (anaconda3-5.3.1/envs/cv) (base) qcmp@qcmp-Alienware-Aurora-R8:~/opencv/build$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D PYTHON3_EXECUTABLE=$CONDA_HOME/envs/cv/bin/python -D PYTHON3_PACKAGES_PATH=$CONDA_HOME/envs/cv/lib/python3.7/site-packages -D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=ON -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF  -D OPENCV_ENABLE_NONFREE=ON -D BUILD_EXAMPLES=ON -D ENABLE_PRECOMPILED_HEADERS=OFF -D BUILD_TESTS=OFF -D PYTHON3_LIBRARY=/home/qcmp/anaconda3/lib/libpython3.7m.so -D PYTHON3_INCLUDE_DIR=/home/qcmp/anaconda3/include/python3.7m -D WITH_CUBLAS=OFF -D OpenBLAS_FOUND=OFF ..

The output is here. There are some warning like Could not find OpenBLAS include, but maybe it doesn't relate to make problem(?)

After that, when make -j8, error occurred like below:

/home/qcmp/opencv/modules/python/src2/cv2.cpp:9:10: fatal error: Python.h: No such file or directory
 #include <Python.h>
          ^~~~~~~~~~
compilation terminated.
modules/python3/CMakeFiles/opencv_python3.dir/build.make:62: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o' failed
make[2]: *** [modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o] Error 1
CMakeFiles/Makefile2:12152: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/all' failed
make[1]: *** [modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2

The error says Python.h cannot be found. According to this link, when running cmake, I attached options for adding path like -D PYTHON3_LIBRARY=/home/qcmp/anaconda3/lib/libpython3.7m.so -D PYTHON3_INCLUDE_DIR=/home/qcmp/anaconda3/include/python3.7m. Actually there is indeed a Python.h file in home/qcmp/anaconda3/include/python3.7m/Python.h.

I already tried sudo apt-get install python3.7-dev and sudo apt-get install python3-dev, according to this link, but it did not resolve the issue.

What should I do to fix it?

Edited

I changed cmake options from setting file path to searching one.

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)")  -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D PYTHON3_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -D PYTHON3_EXECUTABLE=$(which python) -D PYTHON3_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") -D PYTHON3_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR') + '/' + sysconfig.get_config_var('LDLIBRARY'))") -D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=ON -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF  -D OPENCV_ENABLE_NONFREE=ON -D BUILD_EXAMPLES=ON .. 

Searching reference is below.

$ which python
/home/qcmp/.pyenv/shims/python
$ python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())"
/home/qcmp/.pyenv/versions/anaconda3-5.3.1/envs/cv/include/python3.7m
$ python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
/home/qcmp/.pyenv/versions/anaconda3-5.3.1/envs/cv/lib/python3.7/site-packages
$ python -c "import sys; print(sys.prefix)"
/home/qcmp/.pyenv/versions/anaconda3-5.3.1/envs/cv
$ python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR') + '/' + sysconfig.get_config_var('LDLIBRARY'))"
/home/qcmp/.pyenv/versions/anaconda3-5.3.1/envs/cv/lib/libpython3.7m.so

However, I still get the same error.

shmp
  • 1
  • 2
  • Welcome to Stackoverflow!! It looks like you may be providing two different Python locations in your options to `cmake`. You have this `$CONDA_HOME/envs/cv/bin/python` and this `/home/qcmp/anaconda3/lib`. Are these truly pointing to the same Python installation? If not, was this intentional? – Kevin Oct 23 '19 at 13:40
  • I forgot to say that `$CONDA_HOME` is equal to `/home/qcmp/anaconda3`. Also, I confused at Python location. I agree instead of `/home/qcmp/anaconda3/lib`, `/home/qcmp/anaconda3/envs/cv/lib`or `$CONDA_HOME/envs/cv/lib` is correct. However, this cannot fix the error. – shmp Oct 23 '19 at 15:16
  • [Edited] `$CONDA_HOME` is not equal to `/home/qcmp/anaconda3`, but `/home/qcmp/.pyenv/versions/anaconda3-5.3.1`. – shmp Oct 26 '19 at 15:24

0 Answers0