Before you can build exciting computer vision applications, you need to install OpenCV and prepare your Python development environment. A proper installation ensures that you can read images, process videos, detect objects, and use all the powerful features provided by the OpenCV library.
In this lesson, you will learn how to install OpenCV in Python using pip, understand the different OpenCV packages available, install the required supporting libraries, and verify that everything has been installed correctly.
Before installing OpenCV, make sure your computer has Python and pip installed. OpenCV supports Python 3.8 and above, and using the latest stable version of Python is recommended for better compatibility.
Open Command Prompt (Windows), Terminal (Linux/macOS), or PowerShell and type the following command.
python --version
Some systems may require the following command instead:
python3 --version
If Python is installed successfully, you should see an output similar to:
Python 3.12.2
If you receive an error such as ‘python’ is not recognized, install Python first and ensure that the “Add Python to PATH” option is selected during installation.
The Python package manager, pip, allows you to install external libraries like OpenCV with a single command.
pip --version
Example output:
pip 25.0 from ...
If pip is unavailable, reinstall Python or upgrade pip using:
python -m ensurepip --upgrade
The easiest way to install OpenCV is using pip. Open your terminal and run:
pip install opencv-python
This command downloads and installs the standard OpenCV package with GUI support, allowing you to display images and videos using OpenCV windows.
During installation, pip automatically downloads all required dependencies.
OpenCV provides multiple package variants. Choosing the correct package depends on your project requirements.
This is the standard package used by most beginners. It includes graphical user interface (GUI) functionality and supports displaying images and videos using OpenCV windows.
pip install opencv-python
This package excludes GUI components and is designed for cloud servers, Docker containers, Google Colab, and production environments where no display is available.
pip install opencv-python-headless
This package contains all standard OpenCV modules plus additional experimental and advanced algorithms, including extra feature detectors, tracking algorithms, and computer vision modules.
pip install opencv-contrib-python
For most beginners, installing opencv-python is sufficient. As you progress to advanced topics, you may also install opencv-contrib-python.
Although OpenCV is powerful on its own, most computer vision projects also use additional Python libraries.
pip install numpy matplotlib jupyter
In this part, you learned the prerequisites for installing OpenCV, verified Python and pip, installed the correct OpenCV package, understood the differences between available packages, and installed essential supporting libraries. In the next part, you will set up Jupyter Notebook, create your first notebook, and verify that OpenCV is working correctly.
After successfully installing OpenCV and its supporting libraries, the next step is to configure a development environment where you can write, test, and visualize your Python code. One of the most popular environments for beginners and data scientists is Jupyter Notebook.
Jupyter Notebook allows you to write code in small sections called cells, execute them individually, and immediately view the results. This interactive workflow makes it ideal for learning OpenCV, image processing, data analysis, and machine learning.
Jupyter Notebook is an open-source web-based application that enables you to create and share documents containing live Python code, visualizations, equations, images, and explanatory text. Unlike traditional code editors, Jupyter allows you to execute one code cell at a time, making debugging and experimentation much easier.
Because OpenCV projects often involve displaying images, plotting graphs, and testing different algorithms, Jupyter Notebook has become one of the preferred development environments for computer vision and artificial intelligence.
Once Jupyter is installed, open your Command Prompt (Windows), Terminal (Linux/macOS), or PowerShell and navigate to the folder where you want to create your OpenCV project.
Use the following command to start Jupyter Notebook:
jupyter notebook
After running the command, your default web browser will automatically open the Jupyter Notebook dashboard. If it does not open automatically, copy the URL displayed in the terminal and paste it into your browser.
From the Jupyter dashboard:
You can rename it to something meaningful such as:
opencv_lesson_1.ipynb
A Jupyter Notebook consists of multiple cells. Each cell can contain either Python code or formatted documentation.
Code cells are used to write and execute Python programs.
Example:
print("Welcome to OpenCV")
Markdown cells allow you to write notes, explanations, headings, bullet lists, and documentation alongside your code.
This makes Jupyter Notebook an excellent choice for creating tutorials, reports, and project documentation.
Before using OpenCV, make sure your notebook is working correctly by executing a simple Python program.
print("Hello, OpenCV!")
To execute a cell, press:
The output should appear immediately below the cell.
Every OpenCV program begins by importing the OpenCV library. In Python, the library is imported using the alias cv2.
import cv2
If the command executes without any error, OpenCV has been installed successfully.
It is good practice to verify the installed version before starting any project.
import cv2
print(cv2.__version__)
Example output:
4.11.0
The exact version number may differ depending on the version you installed.
The following example imports OpenCV and prints a success message along with the installed version.
import cv2
print("OpenCV Installed Successfully!")
print("Version:", cv2.__version__)
If you see the version number displayed without any errors, your OpenCV installation is complete and ready to use.
Keeping your projects organized will help as you build more complex computer vision applications.
OpenCV_Project/
│
├── images/
├── videos/
├── notebooks/
├── outputs/
├── models/
└── main.py
This folder structure makes it easier to manage datasets, notebooks, trained models, and output files.
In this lesson, you learned how to launch Jupyter Notebook, create your first notebook, understand code and Markdown cells, execute Python code, import OpenCV, verify the installation, and organize your project files. With your development environment now ready, you can begin working with images and videos in the upcoming lessons.
Although installing OpenCV is usually straightforward, beginners may encounter errors related to Python, pip, Jupyter Notebook, or package compatibility. Understanding these common issues and their solutions will save time and help you set up your development environment correctly.
This is one of the most common errors when working with OpenCV. It means that Python cannot find the OpenCV library in the current environment.
Install or reinstall OpenCV using:
pip install opencv-python
Or upgrade the package:
pip install --upgrade opencv-python
If your terminal displays the message ‘pip’ is not recognized as an internal or external command, it usually means that Python or pip is not added to the system PATH.
python --version
pip --version
Sometimes OpenCV works in the terminal but not inside Jupyter Notebook. This happens because Jupyter is using a different Python environment.
Install OpenCV using the same Python interpreter that Jupyter uses:
python -m pip install opencv-python
You can also verify the active Python interpreter:
import sys
print(sys.executable)
Some operating systems restrict package installation for standard users.
pip install --user opencv-python
Alternatively, run the terminal with administrator privileges if appropriate.
As your projects grow, installing all packages globally can lead to dependency conflicts. A virtual environment keeps each project’s libraries isolated.
python -m venv opencv_env
opencv_env\Scripts\activate
source opencv_env/bin/activate
pip install opencv-python
Using virtual environments helps maintain clean, reproducible projects and is considered a best practice in Python development.
Python 3.10 or later is recommended because it offers better performance, improved compatibility, and long-term support with modern libraries.
No. You can use Visual Studio Code, PyCharm, Spyder, or any Python IDE. However, Jupyter Notebook is highly recommended for beginners because it provides an interactive environment for testing code and displaying images.
The standard package includes the core OpenCV library, while the contrib package contains additional experimental and advanced modules such as feature detectors, tracking algorithms, and specialized computer vision tools.
Yes. OpenCV works efficiently on CPUs. GPU acceleration is optional and is mainly used for high-performance applications and deep learning workloads.
Yes. OpenCV is widely used in commercial software, robotics, healthcare, manufacturing, surveillance, and autonomous systems around the world.
pip install opencv-python command.cv2.__version__.cv2 library and print its version.opencv-python and opencv-contrib-python.ModuleNotFoundError: No module named 'cv2' error.In this lesson, you learned how to install OpenCV, configure Jupyter Notebook, verify the installation, understand different OpenCV packages, troubleshoot common installation issues, and use virtual environments for better project management. With your development environment fully configured, you are now ready to begin working with images using OpenCV. In the next lesson, you will explore how digital images are represented as pixels and learn about popular color spaces such as RGB, BGR, Grayscale, and HSV.