Article
ModuleNotFoundError: No module named 'cv2' — how to fix it
Python cannot find OpenCV because the package was installed into a different Python than the one running your script. Here is how to find the right Python and fix it in under five minutes.
On this page0%
- What you need
- Step 1 — Ask Python which Python it is
- Step 2 — Install OpenCV into that exact Python
- Step 3 — Verify the install
- Step 4 — Fix your editor if the terminal works but the editor does not
- Step 5 — Pick the right OpenCV package
- Common mistakes (and fixes)
- Make it never happen again
- What you learned
- FAQ
- Why does Python say No module named 'cv2' when OpenCV is installed?
- What is the correct pip command to install OpenCV?
- Should I install opencv-python or opencv-contrib-python?
- How do I fix No module named cv2 in Jupyter Notebook?
- Why does import cv2 fail with DLL load failed on Windows?
- Next reading on Sythra Articles
ModuleNotFoundError: No module named 'cv2' almost never means OpenCV is broken. It means the Python running your script is not the Python you installed OpenCV into.
Most computers have several Pythons: the system one, one from the Microsoft Store, one from Anaconda, and one inside every virtual environmentA private folder holding its own copy of Python and its own packages you ever made. pip install opencv-python puts the package in exactly one of them. If your editor runs a different one, Python looks around, finds no cv2, and raises this error.
This guide shows you how to find which Python is actually running, install into that one, and stop the error from coming back.
What you need
- Python 3.8 or newer
- A terminal (Command Prompt / PowerShell on Windows, Terminal on macOS and Linux)
- Five minutes
Step 1 — Ask Python which Python it is
Do not guess. Make Python tell you. Create a file called whoami.py:
import sys
print("Executable:", sys.executable)
print("Version:", sys.version)
Run it the same way you run your real script — same editor, same button, same terminal:
python whoami.py
You get something like:
Executable: C:\Users\You\AppData\Local\Programs\Python\Python312\python.exe
Version: 3.12.3
That path is the only Python that matters. Copy it somewhere — you will use it in the next step.
If the path contains
envs\somethingor.venv, you are inside a virtual environment. That is fine, and it is usually the real cause: the environment is new and empty.
Step 2 — Install OpenCV into that exact Python
Now install using that interpreter directly instead of the bare pip command. This is the single most reliable fix, because it removes all guessing:
python -m pip install opencv-python
python -m pip means "use the pip that belongs to this Python". It is safer than plain pip, which may belong to a completely different install.
On macOS and Linux, if python points at an old Python 2, use:
python3 -m pip install opencv-python
If you copied a full path in Step 1, you can be even more explicit:
"C:\Users\You\AppData\Local\Programs\Python\Python312\python.exe" -m pip install opencv-python
Step 3 — Verify the install
Check it from the command line so no editor setting can lie to you:
python -c "import cv2; print(cv2.__version__)"
A version number like 4.10.0 means OpenCV is installed and importable. If you still get the error here, the install went to another Python — go back to Step 1 and read the path again carefully.
You can also confirm where the package landed:
python -m pip show opencv-python
The Location: line should sit inside the same folder tree as the executable path from Step 1.
Step 4 — Fix your editor if the terminal works but the editor does not
This is the most common leftover problem: python -c "import cv2" works in the terminal, but VS Code or Jupyter still shows the error. The editor is running a different interpreterThe specific python.exe your editor uses to run code.
VS Code
- Press
Ctrl+Shift+P(Cmd+Shift+Pon macOS) - Type Python: Select Interpreter
- Pick the path you saw in Step 1
- Close and reopen the terminal panel inside VS Code
Jupyter Notebook or JupyterLab
Run this inside a notebook cell — it installs into the kernel the notebook is actually using:
import sys
!{sys.executable} -m pip install opencv-python
Then restart the kernel. This trick works for any package, not just OpenCV.
Google Colab
OpenCV is already installed. If you still see the error, you almost certainly typed import cv or import opencv instead of import cv2.
Step 5 — Pick the right OpenCV package
There are several OpenCV packages on PyPI and installing two of them at once causes strange errors. Install one:
| Package | Use it when |
|---|---|
opencv-python | Normal desktop use — webcam, windows, image files. Start here. |
opencv-contrib-python | You need extra algorithms (SIFT, tracking, ArUco markers) |
opencv-python-headless | Servers, Docker, CI — no GUI windows available |
If you already installed more than one, clean up and reinstall a single package:
python -m pip uninstall -y opencv-python opencv-contrib-python opencv-python-headless
python -m pip install opencv-python
Common mistakes (and fixes)
| What you see | What it means | Fix |
|---|---|---|
No module named 'cv2' after a successful install | Installed into a different Python | Use python -m pip install opencv-python |
No module named 'opencv' | Wrong import name | The import is always import cv2 |
pip is not recognized | pip is not on your PATH | See our pip is not recognized fix |
| Works in terminal, fails in VS Code | Editor uses another interpreter | Python: Select Interpreter |
| Works in VS Code, fails in Jupyter | Notebook kernel is a different Python | !{sys.executable} -m pip install opencv-python |
ImportError: DLL load failed on Windows | Missing Visual C++ runtime | Install the Microsoft Visual C++ Redistributable, then reinstall |
Make it never happen again
Use one virtual environment per project. It sounds like extra work; it removes this entire class of bug:
python -m venv .venv
Activate it:
# Windows PowerShell
.venv\Scripts\Activate.ps1
# macOS / Linux
source .venv/bin/activate
Then install as usual. Everything you install now belongs to this project only, and sys.executable will point inside .venv. If you are unsure which environment tool to use, we compared them in venv vs conda vs uv.
What you learned
- The error is about which Python, not about OpenCV
sys.executabletells you the truth; guessing does notpython -m pip installalways installs into the running Python- Editors and notebooks have their own interpreter setting
- One virtual environment per project prevents repeats
FAQ
Why does Python say No module named 'cv2' when OpenCV is installed?
Because the Python running your script is not the Python that has OpenCV. Print sys.executable to see which interpreter is running, then install with python -m pip install opencv-python using that same interpreter.
What is the correct pip command to install OpenCV?
Use python -m pip install opencv-python. The python -m prefix guarantees the package goes into the Python you are currently running, which plain pip cannot promise.
Should I install opencv-python or opencv-contrib-python?
Install opencv-python for normal webcam and image work. Choose opencv-contrib-python only if you need extra algorithms such as SIFT or ArUco markers, and never install both at the same time.
How do I fix No module named cv2 in Jupyter Notebook?
Run import sys then !{sys.executable} -m pip install opencv-python inside a notebook cell and restart the kernel. This installs into the exact kernel the notebook uses instead of some other Python on your machine.
Why does import cv2 fail with DLL load failed on Windows?
The OpenCV binary needs the Microsoft Visual C++ Redistributable. Install it, then run python -m pip install --force-reinstall opencv-python.
Next reading on Sythra Articles
- Build a virtual mouse with OpenCV and MediaPipe — the project this fix usually unblocks
- Your first Python program — if you are brand new
- Real-time face detection in Python