how to build opencv for python3 when both python2 and python3 are installed

前端 未结 5 2098
我在风中等你
我在风中等你 2020-12-13 10:29

I was trying to build opencv for python3. However, cmake always sets python build option to be python2.7.11 even after I manually specified include and lib option for python

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 11:08

    I've been trying to install opencv on a Pi3 and this solution didn't work for me as python (for build) was always set to Python2.7 but I found that by changing the order of an elseif statement at the bottom of 'OpenCVDetectPython.cmake' fixed the problem. For me, this file is located at '~/opencv-3.3.1/cmake'.

    The original code segment:

    if(PYTHON_DEFAULT_EXECUTABLE)
        set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    elseif(PYTHON2INTERP_FOUND) # Use Python 2 as default Python interpreter
        set(PYTHON_DEFAULT_AVAILABLE "TRUE")
        set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
    elseif(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()
    

    My re-ordered code segment:

    if(PYTHON_DEFAULT_EXECUTABLE)
        set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    elseif(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}")
    elseif(PYTHON2INTERP_FOUND) # Use Python 2 as default Python interpreter
        set(PYTHON_DEFAULT_AVAILABLE "TRUE")
        set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
    endif()
    

    I don't know the reasoning behind it, but cmake is set to default to python2 if python2 exists, swapping the order of these elseif statements switches it to default to python3 if it exists

    ** Disclaimer **

    1. I was using the script found at https://gist.github.com/willprice/c216fcbeba8d14ad1138 to download, install and build everything (script was modified to not create a virtual environment as I didn't want one and with j1 not j4 as it failed around 85% when running with multiple cores).
    2. I don't think the relevant file exists until you have attempted a build.

提交回复
热议问题