3

In OpenCV linux install doc, section Building OpenCV from Source Using CMake it is said to run command like

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

Simultaneously, there are given parameters for python:

[optional] Building python. Set the following python parameters:
PYTHON2(3)_EXECUTABLE = <path to python>
PYTHON_INCLUDE_DIR = /usr/include/python<version>
PYTHON_INCLUDE_DIR2 = /usr/include/x86_64-linux-gnu/python<version>
PYTHON_LIBRARY = /usr/lib/x86_64-linux-gnu/libpython<version>.so
PYTHON2(3)_NUMPY_INCLUDE_DIRS = /usr/lib/python<version>/dist-packages/numpy/core/include/

Some of these parameters can easily be set for both version of Python:

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON2_EXECUTABLE=/usr/bin/python -D PYTHON3_EXECUTABLE=/usr/bin/python3  ..

but another ones are ambiguous

PYTHON_INCLUDE_DIR = /usr/include/python<version>

Is it possible to build for both versions of Python at once?

4
  • You might want to have a look at cmake/OpenCVDetectPython.cmake... near the bottom. There are two complete sets of those variables, one prefixed with PYTHON2 and other with PYTHON3. (e.g. I see both PYTHON2_INCLUDE_DIR and PYTHON3_INCLUDE_DIR there). Commented Aug 15, 2017 at 14:59
  • @DanMašek where is it, don't see? Commented Aug 15, 2017 at 15:16
  • 1
    github.com/opencv/opencv/blob/master/cmake/… Commented Aug 15, 2017 at 15:21
  • What is it? I have Python 3.5 Commented Aug 15, 2017 at 15:24

3 Answers 3

2

I would recommend this guide as an install reference.

But in general all you have to do is to install both versions of python and then run the install script. There is no need to manually specify python paths, cmake will find them.

Attached screenshot with recent OpenCV install output: terminal screenshot, python paths

Sign up to request clarification or add additional context in comments.

Comments

2

As of December 2018, I was able to compile the latest OpenCV version in my Ubuntu16.04 machine with CUDA, FFMpeg and TIFF (useful to work with Caffe too), and to run it in both Python2.7 and 3.5. The build was highly inspired in this post, so kudos to them!

Clone the latest repos:

Note that both have to be in the same release otherwise cmake will complain. Make sure to clone both at the same time:

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

Configure and build:

First, create the standard build environment:

cd opencv
mkdir build
cd build

Then, the following cmake command worked for me:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
             -D CMAKE_INSTALL_PREFIX=/usr/local \
             -D INSTALL_C_EXAMPLES=ON \
             -D OPENCV_EXTRA_MODULES_PATH=<LOCATION_OF_YOUR_OPENCV_CONTRIB>/modules \
             -D BUILD_EXAMPLES=ON \
             -D BUILD_opencv_python2=ON \
             -D WITH_FFMPEG=1 \
             -D WITH_TIFF=ON \
             -D WITH_CUDA=ON \
             -D CUDA_GENERATION=Pascal \
             -D ENABLE_FAST_MATH=1 \
             -D CUDA_FAST_MATH=1 \
             -D WITH_CUBLAS=1 \
             -D WITH_LAPACK=OFF \
             -D PYTHON2_EXECUTABLE=/usr/bin/python \
             -D PYTHON2_INCLUDE_DIR=/usr/include/python2.7 \
             -D PYTHON2_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7.so \
             -D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/local/lib/python2.7/dist-packages/numpy/core/include \
             -D PYTHON3_EXECUTABLE=/usr/bin/python3 \
             -D PYTHON3_INCLUDE_DIR=/usr/include/python3.5 \
             -D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so \
             -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.5/dist-packages/numpy/core/include \
             -D INSTALL_PYTHON_EXAMPLES=ON ..

Note that you have to set the <LOCATION_OF_YOUR_OPENCV_CONTRIB> (the place where you cloned it), and you may have to adapt your PYTHON2and PYTHON3flags to your system's disposition.

I got some warnings (not sure how bad that is), but it went through: after a while you should see something like

--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done

Make and install

Again, the standard make -j<NUM_CORES> && sudo make install works here. Make sure to adjust -j to your number of CPU cores to speed up the compilation process. After that, the python command

import cv2

worked both in Python 2 and 3. Hope this helps!
Andres

Comments

0

No, bindings to binary modules (compiled from C) are different in Python 2 and Python 3. The same built libs cannot be used from Python 2 and Python 3.

But you may run build instructions for Python 2, then Python 3 using distinct CMAKE_INSTALL_PREFIX values.

3 Comments

@Dims, yes but it seems there is no other solution. Python modules compiled from C cannot work for both Python 2 and Python 3.
Isn't it possible to have two sets of python modules only, leaving all other code shared?
@Dims "Yes" but this would involve lots of bug prone custom changes in the source bundle (do it yourself). Personally, I prefer to lose some Mbs in cheap hard drives than losing days of work for uncertain earnings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.