Cannot install openCV 3.1.0 with python3. CMAKE not including or linking python correctly
I fixed my problem by deleting the entire /build directory, and running cmake again (with -D BUILD_opencv_python3=yes). I think it gave an error because of some sort of caching from cmake, so when I deleted everything and started over again, the error was gone.
The above solutions didn't work for me, I had to specify a whole bunch of options to make cmake recognize python3. This did the trick:
cmake {...} -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
-D PYTHON3_EXECUTABLE=$(which python3) \
-D PYTHON_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON_INCLUDE_DIR2=$(python3 -c "from os.path import dirname; from distutils.sysconfig import get_config_h_filename; print(dirname(get_config_h_filename()))") \
-D PYTHON_LIBRARY=$(python3 -c "from distutils.sysconfig import get_config_var;from os.path import dirname,join ; print(join(dirname(get_config_var('LIBPC')),get_config_var('LDLIBRARY')))") \
-D PYTHON3_NUMPY_INCLUDE_DIRS=$(python3 -c "import numpy; print(numpy.get_include())") \
-D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
I believe, the issue is raised by internal bug from cmake-gui; OPENCV_PYTHON_VERSION variable is considered as Boolean, while cmake scripts consider the variable as placeholder for your preferred python version. Thus, delete the variable and re-added manually as string datatype with an initial value of your preferred python version. e.g., 3.7. Once you press Generate, cmake will accept python module with your filled libraries and include variables. However, if you're required to press Generate again, do that freely without modifying the OPENCV_PYTHON_VERSION variable as the cmake gui will return it as Boolean again, while your inserted preferred value is maintained and cached internally.