Sometimes, we may face ModuleNotFoundError: No module named ‘cv2’ error occurs especially when you tried to import the Python OpenCV module without installation or the import of the module was not successful. In this short article, we will learn how we can get rid of ModuleNotFoundError: No module named ‘cv2’ problem by installing OpenCV on various platforms. We will also cover how we can update the existing OpenCV module on our system. Moreover, we will understand and interpret errors in Python taking ModuleNotFoundError: No module named ‘cv2’ as an example which will help us to understand such errors in the future.
ModuleNotFoundError: No module named ‘cv2’
We know that the error we are getting is because the Python script can’t import the OpenCV module. The reason for the failure of the import can be either the module is not installed or there is a problem in the path. One of the simplest methods to get rid of the error if this error is because the module is not installed, then we can install the module using the pip command:
pip install opencv-python
Here we will discuss various reasons and solutions to solve ModuleNotFoundError: No module named ‘cv2’ error.
Solution-1: Installing OpenCV Python
As we know that the simplest way to solve the issue is to install OpenCV Python on your system. Here we will list different methods to install OpenCV on your system.
You can use the pip command to install the OpenCV shown below:
# using Python 2 pip install opencv-python # (could also be pip3.10 depending on your version) pip3 install opencv-python
If you get a permission error, then you can use the following commands:
# on linux operating system sudo pip3 install opencv-python # on windows pip install opencv-python --user
Another case might be if you don’t have pip in your Path environment, then you can use the following commands:
# using pip version python -m pip install opencv-python #using pip3 version python3 -m pip install opencv-python
If you are using conda version or jupyter notebook, then you can use the following commands:
# for Anaconda conda install -c conda-forge opencv # for Jupyter Notebook !pip install opencv-python
Another alternative method to install OpenCV on your windows is:
# using py alias (Windows) py -m pip install opencv-python
Hopefully, these methods will help you to install OpenCV on your system.
Solution-2: In case you are using a Virtual environment
If you are using a virtual environment, then make sure you had installed the OpenCV module on the virtual environment. Here are the steps to create a virtual environment and install the module:
First, create the virtual environment:
# create virtual environment python3 -m venv venv
python3 -m venv venv does not work, then you can use the following alternative commands:
python -m venv venv py -m venv venv
The second step is to activate the virtual environment:
# activate on Unix or MacOS source venv/bin/activate # activate on Windows (cmd.exe) venv\Scripts\activate.bat # activate on Windows (PowerShell) venv\Scripts\Activate.ps1
Now, you can install the Python OpenCV using any of the given commands:
pip install opencv-python
Hopefully, now you will have OpenCV installed on your system.
Solution-3: Uninstall the Open CV
If you already have installed OpenCV but still getting ModuleNotFoundError: No module named ‘cv2’ error when trying to import the module, then you need to uninstall the installed version and then again install the OpenCV module.
First check if the OpenCV is installed or not using the following commands:
# if you have opencv-python installed pip3 show opencv-python # if you don't have pip set up in PATH python3 -m pip show opencv-python
Here is how you can uninstall it:
# uninstall opencv-python pip3 uninstall opencv-python # if you don't have pip set up in PATH python3 -m pip uninstall opencv-python
Now again you can use any of the methods given in solution-1 to install the OpenCV module:
Solution-4: Upgrading the OpenCV module
Another solution is to upgrade the module without uninstalling and installing it again.
You can use any of the given commands to upgrade the module:
# using pip3 command pip3 install opencv-python --upgrade # if you don't have pip set up in PATH python3 -m pip install opencv-python --upgrade
This will upgrade the open-cv to the newest version available and you will no more get the ModuleNotFoundError: No module named ‘cv2’ error.
Solution-5: Alternative methods to install the OpenCV module
Here we will list other alternative methods to install the OpenCV module on your system.
# using conda install conda install -c https://conda.binstar.org/menpo opencv # using conda install conda install opencv # using conda install conda install -c conda-forge opencv # using pip install pip install opencv-contrib-python # using sudo version sudo apt install python-opencv # for python 3 pip install opencv-python3 # for python 2 pip install opencv-python #using pip install pip install cv2
Hopefully, you will not get rid of ModuleNotFoundError: No module named ‘cv2’ error:
Understanding the ModuleNotFoundError: No module named ‘cv2’ error
If you are still stuck or want to explore the error and understand it deeply, then you can continue reading this section:
The error in Python tells a lot so that it is easy to figure out and solve. The problem, itself has two parts: The first part shows the category of the problem which in this case is ModuleNotFoundError which means there is a problem with the modules and Python couldn’t import the module: Then the second part of the problem gives more specific information saying which module was not imported:
What is ModuleNotFoundError in Python?
Sometimes, Python throws the ModuleNotFoundError afterward. What does this error mean in Python? As the name implies, this error occurs when you’re trying to access or use a module that cannot be found. In the case of the title, the “module named Python” cannot be found. Python here can be any module.
What is CV2 in Python?
cv2 is the module import name for opencv-python, “Unofficial pre-built CPU-only OpenCV packages for Python”. The traditional OpenCV has many complicated steps involving building the module from scratch, which is unnecessary. I would recommend remaining with the OpenCV-python library
Reasons for getting ModuleNotFoundError: No module named ‘cv2’ error
Here are some possible reasons for getting this error:
- The module is not installed
- You might have installed the package in a different version of Python and then you are using a different version
- You might have installed the module on your virtual system which is not globally available.
- Your IDE might have an incorrect version of Python
In this short article, we learned how we can solve ModuleNotFoundError: No module named ‘cv2’ error using various methods. We went through five different methods to solve the issue by solving examples. Moreover, we also discussed the reasons for getting ModuleNotFoundError: No module named ‘cv2’ error.
- TypeError: not all arguments converted during string formatting
- Typeerror: ‘float’ object is not iterable
- Importerror: DLL load failed: the specified procedure could not be found
- TypeError: ‘builtin_function_or_method’ object is not subscriptable
- ModuleNotFoundError: No module named ‘bs4’
- Typeerror: string indices must be integers
- TypeError: can’t multiply sequence by non-int of type ‘numpy.float64’
- TypeError: only integer scalar arrays can be converted to a scalar index
- ‘numpy.ndarray’ object has no attribute ‘append’
- numpy.core.multiarray failed to import
- Attributeerror: module matplotlib has no attribute subplots