32

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?

pjs
  • 18,696
  • 4
  • 27
  • 56
xamox
  • 2,599
  • 4
  • 27
  • 30

4 Answers4

43

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
xamox
  • 2,599
  • 4
  • 27
  • 30
  • 2
    But this doesn't really help you with automatic deploys right? Since you still need to have the right .so file for your particular system. – arno_v Sep 05 '12 at 09:15
  • 1
    This is true, I found you can also symlink the .so files as well so it could essentially work fine as long as the symlink lives in the same location regardless of what OpenCV version you are using. A down side is though that the virtualenv doesn't seem to be relocatable to another machine even using the --relocatable flag, so it kind of really is only useful in the instance I want to use different python libraries than what is installed on the system. – xamox Sep 06 '12 at 13:54
  • Cleanest way, no need to copy or hard-link anything : http://stackoverflow.com/a/24112175/562816 – Dominique PERETTI Jun 09 '14 at 00:16
  • Didn't work for me, always get `RuntimeError: _ARRAY_API is not PyCObject object[...] ImportError: numpy.core.multiarray failed to import` on `import cv2`. And yes, numpy is installed. – letmaik Jul 30 '14 at 10:52
  • I know this is kinda old, but I used `sudo apt-get install python-opencv` to install the package. Apparently, the cv2.so and cv.py are located under `/usr/lib/python2.7/dist-packages/` instead. Just let you guys know (Ubuntu 15.04 LTS) – Tim Sep 20 '15 at 07:18
5

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
pylover
  • 7,670
  • 8
  • 51
  • 73
  • I have followed the same steps you have mentioned. after "sudo make install", I got a message saying it was installed successfully. However, when I import opencv2 in python I get an import error. Any idea what I should do? – MAS Nov 29 '15 at 16:58
  • Ensure your PYTHONPATH is pointed to opencv as mentioned in this post – pylover Nov 29 '15 at 17:59
  • I did add it to my .bash_profile. when I do "pip freeze" in my virtualenv I don't see opencv.I find this strange – MAS Nov 29 '15 at 18:34
  • 1
    I followed this , however, i did while i was activated on the virtualenv. It worked! – KenobiBastila Nov 18 '16 at 23:57
5

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

yuval
  • 1,680
  • 2
  • 12
  • 8
0

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

The Nomadic Coder
  • 580
  • 1
  • 4
  • 16