Python and OpenCV are used to develop services and applications using Computer Vision, Machine Learning (ML), Artifact Intelligence (AI), and to learn about these technologies.
Python and OpenCV can be developed with almost the same code and procedures, independent of the OS you are using, once the development environment is set up. For example, the book I am reading at the time of writing this article on learning uses Ubuntu Linux. However, the I use Windows and macOS. Nevertheless, the Python code in the books works the same as it is. Also, the way to install the necessary libraries is the same.
This article describes how to set up Python and OpenCV for Computer Vision, ML, and AI.
WSL: Setting up Windows Services for Linux
To develop on Windows as you would on Ubuntu or macOS, you need WSL, see the following article on how to set up WSL.

About Shells
In this article we will run commands in a shell; on macOS we will use the Terminal app; on Windows we will use the WSL shell.
Install Python3
For Windows (WSL) and Ubuntu, Python3 is installed by default. Even if it is not installed, it will be installed as one of the dependencies when pip3
is installed later.
For macOS, when Xcode is installed, Python and NumPy are also installed during the setup process when Xcode is launched for the first time.

Install Mesa 3D Graphics Library (Windows and Ubuntu)
The Mesa project is an open source version of OpenGL. It can be installed with apt as follows.
sudo apt install libgl1-mesa-dev
Install Pip3 (Windows and Ubuntu)
Pip is Python’s official package management tool. It is probably installed by default, but you can install it with apt
.
sudo apt install python3-pip
Install Tkinter
Tkinter is a library for handling GUI from Python. We often use Matplotlib to display images, graphs, etc. from Python. You can install it with apt
.
sudo apt install python3-tk
If it is not installed, the following error message will appear and Matplotlib’s GUI functions will not work.
Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
plt.show()
Install OpenCV
Install OpenCV, use pip3
to install packages for Python since we will be using it from Python.
Enter the following to get a list of available versions that can be installed.
pip3 install opencv-python==
Defaulting to user installation because normal site-packages is not writeable
ERROR: Could not find a version that satisfies the requirement opencv-python== (from versions: 3.4.0.14, 3.4.10.37, 3.4.11.39, 3.4.11.41, 3.4.11.43, 3.4.11.45, 3.4.13.47, 3.4.15.55, 3.4.16.57, 3.4.16.59, 3.4.17.61, 3.4.17.63, 3.4.18.65, 4.3.0.38, 4.4.0.40, 4.4.0.42, 4.4.0.44, 4.4.0.46, 4.5.1.48, 4.5.3.56, 4.5.4.58, 4.5.4.60, 4.5.5.62, 4.5.5.64, 4.6.0.66)
ERROR: No matching distribution found for opencv-python==
In this example, the latest version is 4.6.0.66
.
Install the latest version: 4.6.0.66
as found in STEP 1.
pip3 install opencv-python==4.6.0.66
Install OpenCV extensions
Install the OpenCV extension module, using pip3
as with OpenCV.
Enter the following to get a list of available versions to install
pip3 install opencv-contrib-python==
Defaulting to user installation because normal site-packages is not writeable
ERROR: Could not find a version that satisfies the requirement opencv-contrib-python== (from versions: 3.4.11.45, 3.4.13.47, 3.4.14.51, 3.4.15.55, 3.4.16.59, 3.4.17.61, 3.4.17.63, 3.4.18.65, 4.4.0.46, 4.5.1.48, 4.5.2.52, 4.5.3.56, 4.5.4.58, 4.5.4.60, 4.5.5.62, 4.5.5.64, 4.6.0.66)
ERROR: No matching distribution found for opencv-contrib-python==
Install the same version as OpenCV. 4.6.0.66
is installable, it is the same version as OpenCV.
pip3 install opencv-contrib-python==4.6.0.66
Install Matplotlib
Matplotlib is a library used for drawing graphs in Python, and if you try to use Matplotlib in an environment where Matplotlib is not installed, you will receive the following error.
from matplotlib import pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
Install with pip3
.
pip3 install matplotlib
You can also specify a version. If you do not specify a version, the latest version will be installed.
Install Python bindings for Qt
When using OpenCV cv2.imshow()
, etc., I get the following error if the Python bindings for Qt are not installed.
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/akira/.local/lib/python3.10/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: xcb.
Aborted
Install the Python bindings for Qt. Install the Python3 binding for Qt5 with apt
as follows.
sudo apt install python3-pyqt5