Skip to content Skip to sidebar Skip to footer

Installing Opencv In Anaconda3 - Python.h: No Such File Or Directory

I'm trying to build opencv 3.1.0 for my anaconda3 installation and my build is failing at: [ 94%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.

Solution 1:

I fixed the issue. The problem was that PYTHON3_INCLUDE_DIR was set to ~/anaconda3/include while Anaconda's Python.h is actually inside ~/anaconda3/include/python3.5m. Setting PYTHON3_INCLUDE_DIR=anaconda3/include/python3.5m/ fixes this issue as it now sees Python.h

However, this led to another problem where some .h files such as hdf5.h and H5public.h were no longer visible to OpenCV as these were in the main anaconda3/include/ directory, not in the subfolder.

The solution for this was to copy all the .h files from the include/ directory into the include/python3.5m directory (copying the contents of both into another directory and setting that as the path would also work) so that all the .h files are in the same folder visible to the compiler. OpenCV now builds!

Solution 2:

Solved by setting CPLUS_INCLUDE_PATH variable prior to configuring cmake

exportCPLUS_INCLUDE_PATH=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")

Then configure cmake as

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \-D INSTALL_PYTHON_EXAMPLES=ON \-D INSTALL_C_EXAMPLES=OFF \-D WITH_TBB=ON \-D WITH_CUDA=ON \-D BUILD_opencv_cudacodec=OFF \-D ENABLE_FAST_MATH=1 \-D CUDA_FAST_MATH=1 \-D WITH_CUBLAS=1 \-D WITH_V4L=ON \-D WITH_QT=OFF \-D WITH_OPENGL=ON \-D WITH_GSTREAMER=ON \-D OPENCV_GENERATE_PKGCONFIG=ON \-D OPENCV_PC_FILE_NAME=opencv.pc \-D OPENCV_ENABLE_NONFREE=ON \-D OPENCV_EXTRA_MODULES_PATH=/home/udara/Documents/research/opencv/opencv-contrib-source/modules \-D BUILD_EXAMPLES=ON \-D HAVE_opencv_python3=ON \-D OPENCV_PYTHON3_INSTALL_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \-D PYTHON3_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \-D PYTHON_EXECUTABLE=$(which python) \-D PYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \-D PYTHON_LIBRARY=/home/udara/anaconda3/envs/jupyter/lib/libpython3.so ../opencv-source

Folder structure

├── build
├── opencv-contrib-source
└── opencv-source

Post a Comment for "Installing Opencv In Anaconda3 - Python.h: No Such File Or Directory"