Install OpenCV-python using pip

Introduction

I want to install the current version of opencv-python using pip and a virtual environment. At the time of this post, the current version seems to be 4.9.*. I will be using the process I explained long ago, see:

Check there for the setup details if you need them. The first step is to setup a virtual environment using mkvirtualenv. I will call the virtual enironment opencv for obvious reasons:

Create virtual environment

$ mkvirtualenv opencv
created virtual environment CPython3.10.12.final.0-64 in 152ms
creator CPython3Posix(dest=/home/cstrelioff/virtenvs/opencv, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/cstrelioff/.local/share/virtualenv)
added seed packages: pip==24.0, setuptools==69.2.0, wheel==0.43.0
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
virtualenvwrapper.user_scripts creating /home/cstrelioff/virtenvs/opencv/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/cstrelioff/virtenvs/opencv/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/cstrelioff/virtenvs/opencv/bin/preactivate
virtualenvwrapper.user_scripts creating /home/cstrelioff/virtenvs/opencv/bin/postactivate
virtualenvwrapper.user_scripts creating /home/cstrelioff/virtenvs/opencv/bin/get_env_details
(opencv) $

A simple pip list will show that the basics are in place:

(opencv) $ pip list
Package Version
---------- -------
pip 24.0
setuptools 69.2.0
wheel 0.43.0
(opencv) $

We can also check the version of python and pip in this virtual environment:

(opencv) $ pip --version
pip 24.0 from /home/cstrelioff/virtenvs/opencv/lib/python3.10/site-packages/pip (python 3.10)
(opencv) $ python --version
Python 3.10.12
(opencv) $

Install opencv

There are two options for installing opencv-python:

  1. Install just the main packages using pip install opencv-python, or
  2. Install the main packages along with contributed/extra packages using pip install opencv-contrib-python

I chose option 2 and installed using:

(opencv) $ pip install opencv-contrib-python

...install info...

(opencv) $

After the installation, pip will list the following packages installed:

(opencv) $ pip list
Package Version
--------------------- --------
numpy 1.26.4
opencv-contrib-python 4.9.0.80
pip 24.0
setuptools 69.2.0
wheel 0.43.0
(opencv) $

Simple test of opencv install

As a final test that the install went okay I start python, import opencv, and print the version:

(opencv) $ python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
4.9.0
>>> exit()
(opencv) $

Looks good, everything worked without complaints!