Ubuntu – How to install OpenCV3 for Python 3.6 on Ubuntu 16

16.04cmakecompilingopencvpython3

I have tried installing OpenCV from source. It installs perfectly for python 2.7 and python 3.5, but isn't installing for python 3.6.

I built it using the following command:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D PYTHON_EXECUTABLE=/usr/bin/python3.6 ..

The output shows:

--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.5.2)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.5m.so (ver 3.5.2)
--     numpy:                       /home/courts/.local/lib/python3.5/site-packages/numpy/core/include (ver 1.12.0)
--     packages path:               lib/python3.5/dist-packages
-- 
--   Python (for build):            /usr/bin/python3

And when I make install the OpenCV library becomes present in the dist-packages folder for python3.5, but not python3.6.

This is in the build log:

-- Found PythonInterp: /usr/bin/python3.6 (found suitable version "3.6.2", minimum required is "2.7") 
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.5.2", minimum required is "3.4") 
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.5m.so (found suitable exact version "3.5.2") 

Best Answer

  • I listed all the compiler flags by running:

    cmake -L | awk '{if(f)print} /-- Cache values/{f=1}'
    

    Using the flags that looked helpful, I constructed this:

    cmake -D CMAKE_BUILD_TYPE=RELEASE \
          -D CMAKE_INSTALL_PREFIX=/usr/local/ \
          -D PYTHON_EXECUTABLE=/usr/bin/python3.6 \
          -D PYTHON_INCLUDE=/usr/include/python3.6/ \
          -D PYTHON_LIBRARY=/usr/lib/python3.6/ \
          -D PYTHON_PACKAGES_PATH=/usr/local/lib/python3.6/dist-packages/ \
          -D PYTHON_NUMPY_INCLUDE_DIR=/usr/local/lib/python3.6/dist-packages/numpy/core/include/ \
          ..
    

    This worked.

  • Related Question