ModuleNotFoundError: No module named ‘cv2’ in Python

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

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.

When you run above program, you might get below output:

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.

Was this post helpful?

Leave a Reply

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