Sythra

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.

vaibhavkothari· Aug 26, 2026· 7 min readBeginner
ShareXLinkedIn
ModuleNotFoundError: No module named 'cv2' — how to fix it cover

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\something or .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

  1. Press Ctrl+Shift+P (Cmd+Shift+P on macOS)
  2. Type Python: Select Interpreter
  3. Pick the path you saw in Step 1
  4. 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:

PackageUse it when
opencv-pythonNormal desktop use — webcam, windows, image files. Start here.
opencv-contrib-pythonYou need extra algorithms (SIFT, tracking, ArUco markers)
opencv-python-headlessServers, 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 seeWhat it meansFix
No module named 'cv2' after a successful installInstalled into a different PythonUse python -m pip install opencv-python
No module named 'opencv'Wrong import nameThe import is always import cv2
pip is not recognizedpip is not on your PATHSee our pip is not recognized fix
Works in terminal, fails in VS CodeEditor uses another interpreterPython: Select Interpreter
Works in VS Code, fails in JupyterNotebook kernel is a different Python!{sys.executable} -m pip install opencv-python
ImportError: DLL load failed on WindowsMissing Visual C++ runtimeInstall 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.executable tells you the truth; guessing does not
  • python -m pip install always 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

Keep reading

Related articles

Newsletter

Get new articles

Free essays on learning, Python, and ML — no account required. We’ll only email when there’s something worth reading.