Is it possible to run opencv (python binding) from a virtualenv?

Question:

I would like to keep everything contained within the virtualenv. Is this possible with OpenCV? I’m fine with building from scratch, do I just need to setup the virtualenv first then use special compile flags to tell it where to install to?

Asked By: xamox

||

Answers:

From opencv install guide :

By default the OpenCV build system will choose the most recent version of Python that it can find, or you can force it to use a specific version using the PYTHON_EXECUTABLE variable when you invoke cmake.)

I just installed it on my ubuntu 11.10, on virtual env –with-no-site-package, by following the instruction on the link above.
you need to build whole opencv. and its python wrapper together.

EDIT 1:

  1. Create a temporary directory, which we denote as , where you want to put the generated Makefiles, project files as well the object files and output binaries.

    cd ~/opencv
    mkdir release
    cd release
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
    
  2. Enter the created temporary directory () and proceed with:

    make
    sudo make install
    

———

after build & install add the extension modules on PYTHON_PATH

export PYTHONPATH=~/projects/opencv/release/lib:$PYTHONPATH
Answered By: pylover

I found the solution was that I had to copy over cv2.so and cv.py to the directory running the virtualenv, then pip install numpy. To do this on Ubuntu 12.04 I used.

virtualenv virtopencv
cd virtopencv
cp /usr/local/lib/python2.7/dist-packages/cv* ./lib/python2.7/site-packages/
./bin/pip install numpy
source bin/activate
python
import cv
Answered By: xamox

On Debian, I apt installed python-opencv, python-virtualenv, python-pip and then created a virtualenv using the option –system-site-packages.

Answered By: yuval

This is possible by passing the python executable as an argument to cmake. I would also suggest then to use a local install folder, so you don’t need sudo at all. And then, if make install doesn’t ask you for sudo permissions, it is probably targetting your virtualenv python .

Open cmakelists.txt with cmake-gui to see the python variables. It would also probably give an error if you don’t have numpy in your virtualenv, so that way you know it’s choosing the right python. This would also work with independent of python version, as it is built specifically for your python executable

Answered By: The Nomadic Coder
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.