AppDividend
Latest Code Tutorials

Python cv2 module not found: How to Resolve This Error

0

OpenCV is a programming library focused on real-time computer vision. For Python language, OpenCV-Python is the library you can use to solve computer vision problems.

To install Python-OpenCV in your MacOS, type the following command.

python3 pip install -m opencv-python

If you are using LinuxOS, then you can type the following.

pip install opencv-python

If it is still not working, then you can use the following command.

sudo apt install python-opencv

If you are using Windows OS, then you can install OpenCV-Python using the following code.

pip install opencv-python

The next step is to open the Code editor like Visual Studio Code and import the cv2 module in the Python code file.

import cv2

Now, if you run the Python file, you will get an error like this: Python cv2 module not found.

The cv2 module not found error has occurred because somehow, the Python interpreter does not find the path of the opencv-python module.

Resolved: Python cv2 module not found.

This error occurs when Python cannot refer to your default site-packages folder where you have kept the required python files or libraries.

To resolve this error:  add the following code at the start of your file.

import sys 

sys.path.append('/usr/local/lib/python2.7/site-packages')

or before running the Python command in bash move to /usr/local/lib/python3.8/site-packages directory. This is not the best way since it looks like unnecessary code to our program. But there is a workaround. You use this workaround if you don’t want to add anything to the code.

If you are using Anaconda Navigator, then you can resolve the cv2 module not found error using the following command.

conda update anaconda-navigator  
conda update navigator-updater

If your Anaconda does not contain OpenCV at all, then you can install it using the following command.

conda install opencv

If you want to install opencv in a virtual environment, then run the following command in the terminal for getting a virtual environment list.

conda env list

Or Jupyter Notebook command is the following.

!conda env list

Then update your Anaconda.

conda update anaconda-navigator 
conda update navigator-updater

Install opencv in your selected environment path.

conda install -c ['environment path'] opencv

That is it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.