8

I need to use opencv in a project that is in a virtual environment, but you cannot install opencv with pip.

installing on my local machine (OSX Yosemite, version 10.10.1) with homebrew works just fine, but I cannot do "brew install" in my virtualenv.

How can I include opencv in my environment?

Ryan Saxe
  • 17,123
  • 23
  • 80
  • 128
  • Do you want to install OpenCV only in virtualenv or just make OpenCV work in virtualenv? – yangjie Jul 13 '15 at 17:33
  • Ideally I would want it in the virtualenv so that others can copy my identical environment. – Ryan Saxe Jul 13 '15 at 17:37
  • I've been looking around on the web and I found this [blog post](http://www.pyimagesearch.com/2015/06/15/install-opencv-3-0-and-python-2-7-on-osx/). Hope it helps! – tenfishsticks Aug 18 '15 at 21:15

5 Answers5

5

As described here (see Option B) you can run this from inside your virtual environment:

pip install opencv-contrib-python

As mentioned at that link, opencv-contrib-python is an unofficial pre-built OpenCV package.

I tested this on Linux.

packoman
  • 1,230
  • 1
  • 16
  • 36
1

I use Windows 10. In my case, in order to use openCV in my new virtual environment, I manually copied all of the files regarding openCV which are in C:\Program Files (x86)\python36\Lib\site-packages to virtual environment directory C:\Program Files (x86)\ibrahim\Lib\site-packages. Then, import cv2 to python and it worked for me. Be noted that you need to copy all of the files inside red boxes that I showed below.

**Files inside red boxes to be deleted**

Khin Maung Htay
  • 189
  • 1
  • 5
0

For OpenCV 2, try this.

Basically, install Python bindings for OpenCV 2 system-wide, then copy the library file into your virtual environment.

Velimir Mlaker
  • 10,664
  • 4
  • 46
  • 58
0

To make OpenCV available within the virtual environment, if this installed as a "system-wide package", extend the Python-path with the location where the package is installed. E.g. (on Ubuntu, after installation of prebuild package python3-opencv)

import sys
sys.path.extend([
    "/usr/lib/python3/dist-packages"
])

import cv2 as cv
print("OpenCV version:", cv.__version__)
Pim
  • 1
  • 1
-1

don't forget to execute commands in cmd peps

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34889570) – Gugu72 Aug 25 '23 at 21:43
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 25 '23 at 23:45