1

I am using cmake3.17.3-GUI to build opencv4.3.0 in the windows10-vs2015 environment. Both python 2.7 and 3.7 are installed in my PC.

Configuring the CMake shows the warning message:

Found PythonInterp: C:/Python37/python.exe (found suitable version "3.7.7", minimum required is "2.7") 
CMake Warning at cmake/OpenCVDetectPython.cmake:81 (message):
  CMake's 'find_host_package(PythonInterp 2.7)' found wrong Python version:

  PYTHON_EXECUTABLE=C:/Python37/python.exe

  PYTHON_VERSION_STRING=3.7.7

  Consider providing the 'PYTHON2_EXECUTABLE' variable via CMake command line
  or environment variables

Call Stack (most recent call first):
  cmake/OpenCVDetectPython.cmake:271 (find_python)
  CMakeLists.txt:598 (include)

Also it shows that:

  Python 2:
    Interpreter:                 C:/Python27/ArcGIS10.7/python.exe (ver 2.7.16)
    Libraries:                   C:/Python27/ArcGIS10.7/libs/python27.lib (ver 2.7.16)
    numpy:                       C:/Python27/ArcGIS10.7/lib/site-packages/numpy/core/include (ver 1.9.3)
    install path:                C:/Python27/ArcGIS10.7/Lib/site-packages/cv2/python-2.7

  Python 3:
    Interpreter:                 C:/Python37/python.exe (ver 3.7.7)
    Libraries:                   C:/Python37/libs/python37.lib (ver 3.7.7)
    numpy:                       C:/Python37/lib/site-packages/numpy/core/include (ver 1.18.5)
    install path:                C:/Python37/Lib/site-packages/cv2/python-3.7

  Python (for build):            C:/Python27/ArcGIS10.7/python.exe

Here is a screenshot regarding the python setup in the CMake interface: enter image description here

How to force Cmake to use Python 3.7 for build?

jwm
  • 4,832
  • 10
  • 46
  • 78
  • It looks like building with the optional Python component is best done by passing several `PYTHON3_*` variables to CMake, as seen in this [post](https://www.cerebrumedge.com/single-post/2017/12/26/Compiling-OpenCV-with-CUDA-and-FFMpeg-on-Ubuntu-1604). Can you provide the list of variables you pass to CMake, or show the list of those you defined in the CMake GUI? – Kevin Jun 17 '20 at 13:16
  • I updated the post. The python configurations are automatically set by CMake – jwm Jun 17 '20 at 13:23
  • Does this answer your question? [I have 2 versions of python installed, but cmake is using older version. How do I force cmake to use the newer version?](https://stackoverflow.com/questions/15291500/i-have-2-versions-of-python-installed-but-cmake-is-using-older-version-how-do) – dboy Jun 17 '20 at 17:18

1 Answers1

0

I found a solution from https://thinkinfi.com/install-opencv-gpu-with-cuda-for-windows-10/ .

We can modify the file where the path is "opencv\cmake\OpenCVDetectPython.cmake".

In the section "10.3" shows the way.

Previous code:

if(PYTHON_DEFAULT_EXECUTABLE)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
elseif(PYTHON2_EXECUTABLE AND PYTHON2INTERP_FOUND)
    # Use Python 2 as default Python interpreter
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
elseif(PYTHON3_EXECUTABLE AND PYTHON3INTERP_FOUND)
    # Use Python 3 as fallback Python interpreter (if there is no Python 2)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON3_EXECUTABLE}")
endif()

After code:

if(PYTHON_DEFAULT_EXECUTABLE)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
elseif(PYTHON3INTERP_FOUND) 
 # Use Python 3 as default Python interpreter
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON3_EXECUTABLE}")
elseif(PYTHON2INTERP_FOUND) 
    # Use Python 2 as fallback Python interpreter (if there is no Python 3)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
endif()

Perhaps you may use OpenCV 4.5.x .
Here is the code:

if(PYTHON_DEFAULT_EXECUTABLE)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
elseif(PYTHON3_EXECUTABLE AND PYTHON3INTERP_FOUND)
    # Use Python 3 as default Python interpreter
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON3_EXECUTABLE}")
elseif(PYTHON2_EXECUTABLE AND PYTHON2INTERP_FOUND)
    # Use Python 2 as fallback Python interpreter (if there is no Python 3)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
endif()
Fulin Xu
  • 1
  • 2