21

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 python3:

    --   Python 2:
    --   Interpreter:                 /home/ryu/anaconda2/bin/python2.7 (ver 2.7.11)

    --   Python 3:
    --     Interpreter:                 /usr/bin/python3 (ver 3.4.3)
    --     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.4m (ver 3.4.3)
    --     numpy:                       /home/ryu/.local/lib/python3.4/site-packages/numpy/core/include (ver 1.11.0)  

    --  packages path:               lib/python3.4/dist-packages

-- 
--   **Python (for build):            /home/ryu/anaconda2/bin/python2.7**

Did I miss some cmake option?

OS: Ubuntu 14,04

thanks

Rahul K P
  • 15,740
  • 4
  • 35
  • 52
  • 3
    The options I used are: cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D PYTHON3_EXECUTABLE=/usr/bin/python3 -D PYTHON_INCLUDE_DIR=/usr/include/python3.4m -D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.4m -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.4m -D PYTHON3_NUMPY_INCLUDE_DIRS=/home/ryu/.local/lib/python3.4/site-packages/numpy/core/include ../opencv-3.1.0 –  May 06 '16 at 11:15
  • I renamed python2.7 to some other name eventually and cmake could not find python2.7 any more. It finally generated makefile for python3 and everything went just fine –  May 07 '16 at 07:06
  • @user5671315 That should never be solution and especially advice – A.Ametov Jan 23 '20 at 13:28

5 Answers5

51

You can override the python executable to build to by appending the argument PYTHON_DEFAULT_EXECUTABLE with the python executable URI during the cmake invokation.

cmake {...} -DPYTHON_DEFAULT_EXECUTABLE=$(which python3) ..
Ivan De Paz Centeno
  • 3,595
  • 1
  • 18
  • 20
22

I was struggling with this one for some hours and the answers mentioned above didn't solve the problem straightaway.

Adding to Ivan's answer, I had to include these flags in cmake to make this work:

-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=ON \
-D HAVE_opencv_python3=ON \
-D PYTHON_DEFAULT_EXECUTABLE=<path_to_python3> 

I leave that here, so it is maybe useful for someone else in the future.

gdaras
  • 9,401
  • 2
  • 23
  • 39
5

it was take some hours for me. I built Dockerfile with opencv for python3 the key string is

pip install numpy

Full Docker file:

FROM python:3.8

RUN apt-get update && apt-get -y install \
    cmake \
    qtbase5-dev \
    libdc1394-22-dev \
    libavcodec-dev \
    libavformat-dev \
    libswscale-dev

RUN cd /lib \
    && git clone --branch 4.1.1 --depth 1  https://github.com/opencv/opencv.git \
    && git clone --branch 4.1.1 --depth 1  https://github.com/opencv/opencv_contrib.git

RUN pip install numpy \
    && mkdir /lib/opencv/build \
    && cd /lib/opencv/build \
    && cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DWITH_TBB=ON -DWITH_V4L=ON -DWITH_QT=ON -DWITH_OPENGL=ON -DWITH_FFMPEG=ON -DOPENCV_ENABLE_NONFREE=ON -DOPENCV_EXTRA_MODULES_PATH=/lib/opencv_contrib/modules .. \
    && make -j8 \
    && make install

CMD ["bash"]

The main point is to force compiler to build cv2 module for python

To make it we need python3 should be included in line To be built in CMakeCache.txt file in build folder of opencv

ref https://breakthrough.github.io/Installing-OpenCV/

If there are any errors, ensure that you downloaded all the required packages - the output should help track down what is missing. To ensure the Python module will be built, you should see python2 in the list of configured modules after running cmake

(in my case python3)

Alexey
  • 601
  • 7
  • 17
  • FYI, most users won't need libdc1394. I've dropped it from all of my opencv builds and never run into a problem. Odds are, if you are using IEEE 1394, you are already well aware and know to have this lib installed. It's silly that the openCV build examples still include it. – DeusXMachina Dec 16 '19 at 20:02
  • 1
    @DeusXMachina yes, all `lib*` packages in this Dockerfile are optional – Alexey Dec 17 '19 at 07:21
2

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.
Andy B
  • 21
  • 2
1

Changing the options in cmake did nothing for me no matter what options I modified. The simpliest (hacky) solution for me was to

sudo mv /usr/bin/python2.7 /usr/bin/pythonNO-temp

Then you build and install opencv

then

sudo mv /usr/bin/pythonNO-temp /usr/bin/python2.7

wprins
  • 846
  • 2
  • 19
  • 35