3

In a perfect world, I'd like to use pip inside a --no-site-packages virtualenv.

However: OpenCV. From what I've read so far it sounds tricky to reliably install OpenCV in a virtualenv, so I've accepted having to install that as a system package for now (target platform is Ubuntu, so I'm installing python-opencv with apt). I'd like to install everything else with pip into my virtualenv, though.

To make OpenCV available to my application, I've initialized the virtualenv with --system-site-packages.

When installing other dependencies with env/bin/pip install -U -r requirements.txt now, some of the requirements (which happen to be dependencies of OpenCV) are seen as satisfied. Not surprising, but also not what I want.

Is there a sane way to make only OpenCV available from site-packages, and have everything thing else loaded from, and installed to, the virtualenv?

nfelger
  • 823
  • 9
  • 21
  • Checkout the below link: [link](http://stackoverflow.com/questions/11184847/running-opencv-from-a-python-virtualenv?rq=1) Hope this helps. – Surinder ツ Aug 28 '14 at 08:26

2 Answers2

1

In 2018, you can easily install recent versions of OpenCV in a virtualenv using the opencv-python package.

nfelger
  • 823
  • 9
  • 21
0

I have already installed OpenCV but in order to use that installation in my virtual environment I followed the following steps

  • Find your directory for system site-packages. You may refer to @peterino's answer
  • Follow these steps:

# This is the path for my system-site-packages
$ cd /home/aadi/.local/lib/python3.6/site-packages
$ ls | grep cv`
# Here if you have open cv installed you'll find the result. For me, it was something like this
#I am attaching an image of my result'

Result (Image) from my end

# Then I copied both of the cv2 files i.e cv2 and opencv_python-3.4.0.12.dist-info to my virtual env site-packages.

$ cp -r opencv_python-3.4.0.12.dist-info /home/aadi/prog/python_apps/venv/lib/python3.6/site-package && cp -r cv2 /home/aadi/prog/python_apps/venv/lib/python3.6/site-packages    

Here python_apps is the directory where I've created my virtual environment After then I reactivated my virtual environment and bingo! importing cv2 doesn't give any error

(venv) ➜  python_apps python
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> 
Aditya
  • 31
  • 2