How To Fix ModuleNotFoundError: No Module Named ‘cv2’ In Python?

ModuleNotFoundError: No module named 'cv2' in Python

If you are getting the error ModuleNotFoundError: No module named ‘cv2’ in Python. Don’t worry too much. In this article, we will work with you to find the solution to the above error. Scroll down to continue reading.

First, what is CV2?

CV2 is the symbolic name of the OpenCV library. OpenCV stands for Open Source Computer Vision. It is the leading library for Computer Vision and Machine Learning.

OpenCV is used for a variety of purposes and applications, including:

  • Street view images
  • Automated testing and monitoring
  • Robots and self-driving cars
  • Medical image analysis
  • Search and recover photos/videos

When does the ModuleNotFoundError: No module named ‘cv2’ in Python appear? How to fix it?

OpenCV library not installed

We may encounter the ModuleNotFoundError: No module named ‘cv2’ when we have not installed OpenCV but try to import and run the program. Like this:

# Import cv2 but not installed
import cv2

# Read banner.png file with GRAYSCALE
image = cv2.imread("banner.png", 0)

# Display image
cv2.imshow("Display window", image)

# Press any key to turn off
k = cv2.waitKey()

Output:

ModuleNotFoundError: No module named 'cv2'

To fix the error, you need to install the OpenCV library for the Windows operating system with the command line below:

pip install opencv-python

Or Linux/ macOS with the command line:

pip3 install opencv-python

Now you have fixed that error. Just import and use it.

import cv2

# Read banner.png file with GRAYSCALE
image = cv2.imread("banner.png", 0)

# Display image
cv2.imshow("Display window", image)

# Press any key to turn off
k = cv2.waitKey()

Output:

Use the wrong name when importing

When importing the OpenCV library, If you are new to Python, you will probably type: ‘import Cv2’ and then run the program. You will immediately get the error. See the example below for a better understanding.

# Cv2 is the wrong name
import Cv2

# Read banner.png file with default color
image = Cv2.imread("banner.png")

# Display image
Cv2.imshow("Display window", image)

# Press any key to turn off
k = Cv2.waitKey()

Output:

ModuleNotFoundError: No module named 'Cv2'

In case of this error, you need to be more careful in importing the library, and the library name will not always be the same as when importing. So you need to lowercase the ‘Cv’ to ‘cv’, and the error will be fixed immediately. Like this:

import cv2

# Read banner.png file with default color
image = cv2.imread("banner.png")

# Display image
cv2.imshow("Display window", image)

# Press any key to turn off
k = cv2.waitKey()

Output:

Install OpenCV in PyCharm

If you installed OpenCv using pip but ran the application on PyCharm, you still get the ModuleNotFoundError error. That means you have not installed OpenCv on PyCharm.

Like the first way, open a terminal in PyCharm and type:

pip install opencv-python

If the following message appears:

Successfully installed opencv-python-4.6.0.66

That means you have successfully installed it. Now import and write the code.

import cv2

# Read banner.png
image = cv2.imread("banner.png")

# Display image
cv2.imshow("Display window", image)

# Press any key to turn off
k = cv2.waitKey()

Output:

Summary

The above article has shown the three most common causes of the ModuleNotFoundError: No module named ‘cv2’ in Python and ways to fix it. We hope that through this article, you will be able to fix the error and never see it again.

Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *