ModuleNotFoundError: No module named ‘cv2’ in Python

In this post, We will see how to resolve ModuleNotFoundError No module named 'cv2' in Python.
[toc]

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.

# 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:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
 in 
      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:

  1. Open command prompt
  2. Run following command:
    $ python -m pip install –upgrade pip
  3. Install opencv using following command:
    $ pip install opencv-python

In case you are using Anaconda, then follow below steps.

  1. Open command prompt
  2. Update conda navigator with following command:
    $ conda update anaconda-navigator
    $ conda update navigator-updater
  3. 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:

  1. Open terminal
  2. Run following command:
    $ sudo pip3 install opencv-python
  3. or

    $ sudo apt install python-opencv

Add site-packages to PYTHONPATH

It might be possible that PYTHONPATH does not have site-packages.

  1. Open ~/.bashrc
  2. Add following line:

    $ export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

That’s all about ModuleNotFoundError: No module named ‘cv2’ in Python.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.