In this post, We will see how to resolve ModuleNotFoundError No module named 'cv2'
in Python.
Table of Contents
Reason for No module named ‘cv2’
Let’s first reproduce this error, and then we will see how to resolve ModuleNotFoundError No module named 'cv2'
.
We will run cv2 imread example over here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# import computer vision library(cv2) in this code import cv2 # main code if __name__ == "__main__" : # mentioning absolute path of the image img_path = "C:\\Users\\user\\Desktop\\flower.jpg" # using imread() method of cv2 image = cv2.imread(img_path) # show the image on the newly created image window cv2.imshow('image window',image) |
When you run above program, you might get below output:
1 2 3 4 5 6 7 8 9 10 11 12 |
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) <ipython-input-1-aa8cedc4ccbe> in <module> 1 # import computer vision library(cv2) in this code ----> 2 import cv2 3 4 # main code 5 if __name__ == "__main__" : ModuleNotFoundError: No module named 'cv2' |
You will get this error when cv2 module is not properly installed on your machine.
Resolution for No module named ‘cv2’
Let’s see how we can solve this error.
Windows user
If you are windows user, then follow the below steps:
- Open command prompt
- Run following command:
$ python -m pip install –upgrade pip
- Install opencv using following command:
$ pip install opencv-python
In case you are using Anaconda, then follow below steps.
- Open command prompt
- Update conda navigator with following command:
$ conda update anaconda-navigator
$ conda update navigator-updater - Install opencv using following command:
$ conda install -c conda-forge opencv
or
$ conda install -c https://conda.binstar.org/menpo opencv
Linux user
If you are linux user, then follow the below steps:
- Open terminal
- Run following command:
$ sudo pip3 install opencv-python
or
Add site-packages to PYTHONPATH
It might be possible that PYTHONPATH
does not have site-packages
.
- Open ~/.bashrc
-
Add following line:
$ export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
That’s all about ModuleNotFoundError: No module named ‘cv2’ in Python.