ModuleNotFoundError: No module named 'cv2'
Posted in Python by Dirk - last update: Feb 06, 2024
Python raises the ModuleNotFoundError: No module named 'cv2
when it is unable to find the cv2
library. The most likely cause is that you didn’t install cv2 in the environment where you are running your code. Quick fix: install cv2 using: the pip install opencv-python
command
Possible causes and solutions
OpenCV is an open-source computer vision and image processing library that provides tools and functions for a wide range of computer vision tasks. The cv2
module is the Python interface for OpenCV, allowing developers to use OpenCV functionalities in Python scripts.
Sample code:
import cv2
# Read an image from file
image = cv2.imread('example.jpg')
# Display the image
cv2.imshow('Image', image)
# Wait for a key press and close the window
cv2.waitKey(0)
cv2.destroyAllWindows()
And when trying to run the code you get the ’no module named error':
Traceback (most recent call last):
File "myfile.py", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
Luckily, it is easy to fix. Below you can find the most common causes and how to fix them:
‘cv2’ Library is not installed
In your code you are importing the webdriver module from cv2
which generates the ModuleNotFoundError: No module named 'cv2
’. The the most common cause for this error: the environment where you are running your code doesn’t have the cv2
library installed.
You can use the following command in the terminal or command prompt to check if the cv2
library is installed:
If cv2
is not installed you will get this result:
WARNING: Package(s) not found: cv2
Solution:
pip install opencv-python
‘cv2’ Module installed in the Wrong Directory
It is possible that you did install the cv2 module, but that the directory where you installed it is not in the Python path.
You can check the Python path in a few different ways:
1. Using sys module in Python:
Open a Python script or the Python interpreter and run the following code:
import sys
print(sys.path)
This will print a list of directories that make up the Python path.
2. Using Command Line
Open a command prompt or terminal and run:
If the PYTHONPATH
environment variable is set, it will display the directories in the Python path.
Solution: Move the cv2 module to a directory that is included in the Python path or add the cv2 module’s directory to the sys.path
using sys.path.append()
.
‘cv2’ not installed in Virtual Environment
Even if you have cv2 installed in on your computer, if you are using a virtual environment you cannot access it from this environment.
If you are using virtual environments, ensure that the virtual environment is activated, and that the cv2 modules is installed within this environment.
Installation is simple:
pip install opencv-python
File Naming Conflicts:
It is not really likely - but if you happen to have a ```cv2.py`` file in your environment and you also installed the standard cv2 library it will cause a conflict. Rename your script/module to avoid conflicts with existing module names.
Other articles