Mohammad Moghimi
Mohammad Moghimi

Reputation: 4686

Install OpenCV for Python (multiple python versions)

I have two different versions of python installed on my machine: 2.4 and 2.7. I'm trying to install OpenCV(2.4.5) for the 2.7 version.

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON ..

It detects the python 2.4 as the current installation:

--   Python:
--     Interpreter:                 /usr/bin/python2.4 (ver 2.4)
--     Libraries:                   /usr/lib64/python2.4/config/libpython2.4.a
--     numpy:                       /usr/lib64/python2.4/site-packages/numpy/core/include (ver 1.2.1)
--     packages path:               lib/python2.4/site-packages

and later in building opencv gives me this error:

[ 75%] Generating pyopencv_generated_funcs.h, pyopencv_generated_func_tab.h, pyopencv_generated_types.h, pyopencv_generated_type_reg.h, pyopencv_generated_const_reg.h
  File "/home/mmoghimi/opencv-2.4.5/modules/python/src2/gen2.py", line 815
    cname1=("cv::Algorithm" if classinfo.isalgorithm else classinfo.cname)))
                             ^
SyntaxError: invalid syntax
make[2]: *** [modules/python/pyopencv_generated_funcs.h] Error 1
make[1]: *** [modules/python/CMakeFiles/opencv_python.dir/all] Error 2
make: *** [all] Error 2

apparently it uses a new format that python2.4 does not support. So, my question is that is there any way to explicitly specify the version of python?

Upvotes: 9

Views: 26764

Answers (3)

lucidbrot
lucidbrot

Reputation: 6156

This is a low-quality answer but it is still very useful if you have been trying for ten hours and need some new ideas.


This issue took me a day and I still am unsure how I solved it, but here are a few pointers in case the given answers alone don't help:

Originally, I was following this guide which can be summarized as follows:

sudo apt-get update && sudo apt-get upgrade && sudo apt-get install build-essential git cmake pkg-config libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libgtk2.0-dev libatlas-base-dev gfortran

git clone https://github.com/Itseez/opencv.git && cd opencv &&git checkout 3.0.0

sudo apt-get install python2.7-dev && sudo apt-get install python3-dev

cd ~ && wget https://bootstrap.pypa.io/get-pip.py && sudo python get-pip.py

pip3.6 install numpy               # note that we specify the pip version after the command
                                   # it is also possible to write python3.6 to refer to the python version 3.6 command

# The following failed for me. Use it if it works for you.

cd ~/opencv && mkdir build && cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE 
 -D CMAKE_INSTALL_PREFIX=/usr/local 
 -D INSTALL_PYTHON_EXAMPLES=ON 
 -D INSTALL_C_EXAMPLES=ON 
 -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules 
 -D BUILD_EXAMPLES=ON ..

# This must be done after cmake worked. Pay attention to the start of the cmake output. It tells you whether it found the Python Interpreter.


cd ~/opencv && mkdir build && cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE 
 -D CMAKE_INSTALL_PREFIX=/usr/local 
 -D INSTALL_PYTHON_EXAMPLES=ON 
 -D INSTALL_C_EXAMPLES=ON 
 -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules 
 -D BUILD_EXAMPLES=ON ..

I had python 2.7 and 3.4 installed already, and built python 3.6 myself in /usr/local/bin. After following Aurelius' Answer and some trying out, I built this cmake command:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D BUILD_EXAMPLES=ON -D PYTHON_EXECUTABLE=/usr/local/bin/python3.6/ -D PYTHON_INCLUDE=/usr/local/include/python3.6m/ -D PYTHON_LIBRARY=/usr/local/lib/python3.6/ -D PYTHON_PACKAGES_PATH=/usr/local/lib/python3.6/site-packages/ -D PYTHON_NUMPY_INCLUDE_DIR=/usr/local/lib/python3.6/dist-packages/numpy/core/include/ -D PYTHON_INCLUDE_DIR=/usr/local/include/python3.6m/ -D PYTHON_LIBRARY=/usr/local/lib/  -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON3_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D BUILD_EXAMPLES=ON -D PYTHON3_INCLUDE=/usr/local/include/python3.6m/ -D PYTHON3_LIBRARY=/usr/local/lib/python3.6/ -D PYTHON3_PACKAGES_PATH=/usr/local/lib/python3.6/site-packages/ -D PYTHON3_NUMPY_INCLUDE_DIR=/usr/local/lib/python3.6/dist-packages/numpy/core/include/ -D PYTHON3_INCLUDE_DIR=/usr/local/include/python3.6m/ -D PYTHON3_LIBRARY=/usr/local/lib/ ..

Note that it is likely that many of these do not make sense. I was mindlessly trying out until I got something to work. Some of these pathes can be found using python itself such as

python3.6 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())"

or directly in the cmake command:

$ cmake .. \
-DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")  \
-DPYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))")

At this point, cmake was finding the correct python interpreter and libraries... but then failed, saying it did not find them. I can only quote the error from memory since I cannot scroll far enough back in tmux and I do not want to reproduce the error.

opencv found unsuitable version "1.4" but required is at least 3.6

The solution to this is telling cmake exactly what you want by modifying the file build/../cmake/OpenCVDetectPython.cmake to contain the following checks somewhere at the start:

find_package( PythonInterp 3.6 REQUIRED )
find_package( PythonLibs 3.6 REQUIRED )

(Note that these are deprecated)

It still did not work, and that was because I had set the cmake flag -D PYTHON3_EXECUTABLE. This e-mail chain was helpful for figuring that out and also suggests using virtualenv as an alternative solution. Which is probably what I should have started with.

Upvotes: 1

Aurelius
Aurelius

Reputation: 11329

There are some Cmake flags which allow you to explicitly specify which version of Python to use. You will need to set the values of these flags to the correct location for your installation of Python.

The flag names and likely locations are below:

PYTHON_EXECUTABLE=/usr/bin/python2.7/
PYTHON_INCLUDE=/usr/include/python2.7/
PYTHON_LIBRARY=/usr/lib/libpython2.7.a    //or .so for shared library
PYTHON_PACKAGES_PATH=/usr/local/lib/python2.7/site-packages/
PYTHON_NUMPY_INCLUDE_DIR=/usr/local/lib/python2.7/dist-packages/numpy/core/include

If these paths don't work, you will need to locate them on your machine.

Upvotes: 24

Sofia Velmer
Sofia Velmer

Reputation: 69

Use virtualenv

virtualenv -p python2.7 env
source env/bin/activate
python --version  # prints «Python 2.7.3»
pip install pyopencv

If you need support of 2.4 (or other version), just create new environment.

Upvotes: 2

Related Questions