Modulenotfounderror: No Module Named 'CV2' in Python
Last Updated :
22 Apr, 2025
If you are working with image processing using OpenCV becomes important for it. But sometimes we face a error saying:
ModuleNotFoundError: No module named 'cv2'
In this article we will see why it happens and how to fix it step by step.
The "No module named 'cv2'" error is encountered in python when we are trying to import the OpenCV library. This error specifies that the Python interpreter cannot find the OpenCV module in the current environment. To resolve this issue we needs to install OpenCV library using pip command. Let's understand how to resolve this issue step by step:
Step 1: Check if OpenCV is Installed
Before doing anything check if OpenCV is already installed. Open your terminal or command prompt and type:
pip show opencv-python
If OpenCV is installed you’ll see details like its version and location. If nothing shows up it means OpenCV isn’t installed.
Step 2: Install OpenCV
To install OpenCV use the following command:
pip install opencv-contrib-python
Step 3: Verify the Installation
After installation verify that if OpenCV is working. Open Python in your terminal and try importing cv2
:
import cv2
print(cv2.__version__)
If no errors appear and the version number is printed OpenCV is successfully installed.
Step 4: Check Your Python Environment
If you’re using a virtual environment like venv
or conda
ensure that OpenCV is installed in the same environment where your project is running. Activate your virtual environment and repeat the installation steps. To activate a virtual environment in Python:
# On Windows
.\venv\Scripts\activate
# On macOS/Linux
source venv/bin/activate
Then install OpenCV again using pip
.
Step 5: Fix Multiple Python Versions
If you have multiple Python versions installed you might be installing OpenCV in one version but running your code in another. To avoid confusion specify the Python version explicitly when installing:
python3 -m pip install opencv-python
Step 6: Check for Typos
Double-check your code to ensure you’re importing cv2
correctly. The correct syntax is:
import cv2
If you accidentally wrote something like import cv
or import CV2
Python won’t recognize it.