No module named cv2 ModuleNotFoundError in Python

Sometimes, we may face ModuleNotFoundError: No module named cv2 error occurs especially when you tried to import the Python OpenCV module without installation or the import of the module was not successful. In this short article, we will learn how we can get rid of ModuleNotFoundError: No module named cv2 problem by installing OpenCV on various platforms. We will also cover how we can update the existing OpenCV module on our system. Moreover, we will understand and interpret errors in Python taking ModuleNotFoundError: No module named cv2 as an example which will help us to understand such errors in the future.

Solve ModuleNotFoundError: No module named cv2 in Python

We know that the error we are getting is because the Python script can’t import the OpenCV module. The reason for the failure of the import can be either the module is not installed or there is a problem in the path. One of the simplest methods to get rid of the error if this error is because the module is not installed, then we can install the module using the pip command: pip install opencv-python

ModuleNotFoundError-No-module-named-cv2-error

Here we will discuss various reasons and solutions to solve ModuleNotFoundError: No module named cv2 error.

Installing OpenCV Python

As we know that the simplest way to solve the issue is to install OpenCV Python on your system. Here we will list different methods to install OpenCV on your system.

You can use the pip command to install the OpenCV shown below:

#  using Python 2
pip install opencv-python

# (could also be pip3.10 depending on your version)
pip3 install opencv-python

If you get a permission error, then you can use the following commands:


# on linux operating system
sudo pip3 install opencv-python

# on windows
pip install opencv-python --user

Another case might be if you don’t have pip in your Path environment, then you can use the following commands:

# using pip version
python -m pip install opencv-python

#using pip3 version
python3 -m pip install opencv-python

If you are using conda version or jupyter notebook, then you can use the following commands:

# for Anaconda
conda install -c conda-forge opencv

# for Jupyter Notebook
!pip install opencv-python

Another alternative method to install OpenCV on your windows is:

# using py alias (Windows)
py -m pip install opencv-python

Hopefully, these methods will help you to install OpenCV on your system.

In case you are using a Virtual environment

If you are using a virtual environment, then make sure you had installed the OpenCV module on the virtual environment. Here are the steps to create a virtual environment and install the module:

First, create the virtual environment:

# create virtual environment
python3 -m venv venv

If the python3 -m venv venv does not work, then you can use the following alternative commands:

python -m venv venv
py -m venv venv

The second step is to activate the virtual environment:

#  activate on Unix or MacOS
source venv/bin/activate

# activate on Windows (cmd.exe)
venv\Scripts\activate.bat

# activate on Windows (PowerShell)
venv\Scripts\Activate.ps1

Now, you can install the Python OpenCV using any of the given commands:

pip install opencv-python

Hopefully, now you will have OpenCV installed on your system.

Uninstall the Open CV

If you already have installed OpenCV but still getting ModuleNotFoundError: No module named cv2 error when trying to import the module, then you need to uninstall the installed version and then again install the OpenCV module.

First check if the OpenCV is installed or not using the following commands:

# if you have opencv-python installed
pip3 show opencv-python

# if you don't have pip set up in PATH
python3 -m pip show opencv-python

Here is how you can uninstall it:

# uninstall opencv-python
pip3 uninstall opencv-python

# if you don't have pip set up in PATH
python3 -m pip uninstall opencv-python

Now again you can use any of the methods given in solution-1 to install the OpenCV module:

Upgrading the OpenCV module

Another solution is to upgrade the module without uninstalling and installing it again.

You can use any of the given commands to upgrade the module:

# using pip3 command
pip3 install opencv-python --upgrade

# if you don't have pip set up in PATH
python3 -m pip install opencv-python --upgrade

This will upgrade the open-cv to the newest version available and you will no more get the ModuleNotFoundError: No module named cv2 error.

Alternative methods to install the OpenCV module

Here we will list other alternative methods to install the OpenCV module on your system.

# using conda install
conda install -c https://conda.binstar.org/menpo opencv

# using conda install
conda install opencv 

# using conda install 
conda install -c conda-forge opencv

# using pip install
pip install opencv-contrib-python

# using sudo version
sudo apt install python-opencv

# for python 3
pip install opencv-python3 

# for python 2
pip install opencv-python

#using pip install
pip install cv2

Hopefully, you will not get rid of ModuleNotFoundError: No module named cv2 error:

Understanding the ModuleNotFoundError: No module named cv2 error

If you are still stuck or want to explore the error and understand it deeply, then you can continue reading this section:

The error in Python tells a lot so that it is easy to figure out and solve. The problem, itself has two parts: The first part shows the category of the problem which in this case is ModuleNotFoundError which means there is a problem with the modules and Python couldn’t import the module: Then the second part of the problem gives more specific information saying which module was not imported:

You may want to read: Implement the Median Filter on Image

What is ModuleNotFoundError in Python?

Sometimes, Python throws the ModuleNotFoundError afterward. What does this error mean in Python? As the name implies, this error occurs when you’re trying to access or use a module that cannot be found. In the case of the title, the “module named Python” cannot be found. Python here can be any module.

What is CV2 in Python?

cv2 is the module import name for opencv-python, “Unofficial pre-built CPU-only OpenCV packages for Python”. The traditional OpenCV has many complicated steps involving building the module from scratch, which is unnecessary. I would recommend remaining with the OpenCV-python library

Summary

In this short article, we learned how we can solve ModuleNotFoundError: No module named cv2 error using various methods. We went through five different methods to solve the issue by solving examples. Moreover, we also discussed the reasons for getting ModuleNotFoundError No module named ‘cv2’ error.

Leave a Comment