Install OpenCV3 into virtualenv on Mac

This post introduces how to install OpenCV3 into a virtualenv on Mac.

If you have not setup virtualenv on your mac, check my post here to do that before you proceed the tutorial in this post.

Let us get started.

Step 1: Activate your virtualenv in your terminal

for example:

$ source ~/ipy-jupyter-venv3/bin/activate  

(ipy-jupyter-venv3)$  # Your prompt should change

Step 2: Install OpenCV (modules) according to your needs

  1. If you need only main modules, in your activated virtualenv in your terminal, run the following
(ipy-jupyter-venv3)$ pip3 install opencv-python 

2. If you need both main and contrib modules (check extra modules listing from OpenCV documentation),  in your activated virtualenv in your terminal, run the following

(ipy-jupyter-venv3)$ pip3 install opencv-contrib-python 

Step 3: Test whether openCV is installed correctly

To test whether OpenCV installed correctly into your virtualenv, in your terminal type in those command below in bold.

(ipy-jupyter-venv3) liping$ python3.6

Python 3.6.5 (default, Mar 30 2018, 06:41:53)

[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin

Type “help”, “copyright”, “credits” or “license” for more information.

>>> import cv2

>>> cv2.__version__

‘3.4.3’

>>

Step 4 (optional): To uninstall opencv inside virtualenv, following the command below according to how you installed it.

(ipy-jupyter-venv3) Liping:~$ pip3 uninstall opencv-python 

or

(ipy-jupyter-venv3) Liping:~$ pip3 uninstall opencv-contrib-python

 

Summary:

In this post, you learned how to install and uninstall OpenCV into your virtualenv.

OpenCV installation in virtualenv

This post introduces how to install openCV in virtualenv on Ubuntu 16.04.

Make sure Python and Virtualenv is installed on your Ubuntu first.

Check my post about more details about how to setup python virtual environment and why it is better to install python libraries in Python virtual environment.

  • Install OpenCV inside a virutalenv

activate your virtualenv first, then

if you only  need main modules, run the following commands

$ pip3 install opencv-python

if you need both main and contrib modules (check extra modules listing from OpenCV documentation), run the following commands:

$ pip3 install opencv-contrib-python
  • If you want to install OpenCV on your machine system-wide,

use the following instructions.

$ sudo apt-get install libopencv-dev python-opencv

# if you only  need main modules, run
$ sudo pip3 install opencv-python

# if you need both main and contrib modules (check extra modules listing from OpenCV documentation), run
$ sudo pip3 install opencv-contrib-python

References:

Opencv-python project description (PDF)