运行make时无法识别Python.h

时间:2019-10-23 13:25:02

标签: python opencv makefile cmake anaconda

摘要

对于OpenCV环境,在CMake配置之后,我尝试了make,但是发生了错误,说Python.h: No such file or directory

环境

  • ubuntu 18.04,
  • pyenv上的
  • anaconda3-5.3.1
  • python3.7,
  • opencv(4.0.1)和opencv_contrib(4.0.1)

我做了什么

首先,我尝试使用下面的一些选项进行CMake配置。$CONDA_HOME等于/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 ..

输出为here。有些警告类似Could not find OpenBLAS include,但可能与make问题(?)

无关。

此后,当make -j8时,发生如下错误:

/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

该错误表明找不到Python.h。根据{{​​3}},在运行cmake时,我附加了用于添加路径的选项,例如-D PYTHON3_LIBRARY=/home/qcmp/anaconda3/lib/libpython3.7m.so -D PYTHON3_INCLUDE_DIR=/home/qcmp/anaconda3/include/python3.7m。实际上,home/qcmp/anaconda3/include/python3.7m/Python.h中确实存在一个Python.h文件。

根据this link,我已经尝试过sudo apt-get install python3.7-devsudo apt-get install python3-dev,但无法解决问题。

该如何解决?

已编辑

我将cmake选项从设置文件路径更改为搜索一个。

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 .. 

搜索参考文献如下。

$ 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

但是,我仍然遇到相同的错误。

0 个答案:

没有答案