Fix error

Fix “import cv2” error on Windows — ModuleNotFoundError & DLL fix

Fix all common import cv2 errors on Windows: ModuleNotFoundError no module named cv2, DLL load failed, NumPy version conflicts and wrong Python environment.

Common import cv2 errors on Windows

ModuleNotFoundError: No module named 'cv2'
OpenCV not installed in the active Python environment
pip install opencv-python
ImportError: DLL load failed while importing cv2
Missing Visual C++ Redistributable or wrong Python bitness
Install vc_redist.x64.exe then pip install opencv-python --force-reinstall
ImportError: cannot import name 'cv2' from partially initialized module
Local file named cv2.py conflicts with the package
Delete or rename any file called cv2.py in your project
ImportError: numpy.core.multiarray failed to import
NumPy version incompatible with installed OpenCV
pip install --upgrade numpy

OpenCV not installed in your Python environment

cmd.exe
# Check if OpenCV is installed:
C:\> pip show opencv-python
WARNING: Package(s) not found: opencv-python
# Install it:
C:\> pip install opencv-python
Successfully installed opencv-python-4.12.0.86
# Verify:
C:\> python -c "import cv2; print(cv2.__version__)"
4.12.0

Wrong Python environment

Windows often has multiple Python installations. OpenCV may be installed in one Python but you are running another. Diagnose:

cmd.exe
# Check which Python is active:
C:\> where python
C:\Users\name\AppData\Local\Programs\Python\Python311\python.exe
C:\Python39\python.exe
# First result is the one used by default
# Install to the correct Python explicitly:
C:\> C:\Users\name\AppData\Local\Programs\Python\Python311\python.exe -m pip install opencv-python
Use python -m pip install opencv-python instead of just pip install to ensure you install to the Python you are actually running.

Local file named cv2.py

If you have a file called cv2.py in your project directory, Python imports it instead of the OpenCV package. Check:

cmd.exe
# List Python files in current directory:
C:\> dir *.py
# Look for cv2.py or any file that might shadow the package

Rename or delete any cv2.py file in your project directory. Also check for cv2 folders.

NumPy compatibility issue

cmd.exe
# Check NumPy version:
C:\> python -c "import numpy; print(numpy.__version__)"
# Update NumPy:
C:\> pip install --upgrade numpy
# Or pin to a compatible version:
C:\> pip install "numpy<2.0"
OpenCV 4.9 and older may not be compatible with NumPy 2.0. If you see NumPy errors, downgrade: pip install "numpy<2.0"

Import error questions

Works in cmd.exe but not in VS Code / PyCharm
Your IDE is using a different Python interpreter. In VS Code: Ctrl+Shift+P → "Python: Select Interpreter" → choose the Python where OpenCV is installed. In PyCharm: File → Settings → Project → Python Interpreter.
pip install succeeds but import still fails
Run python -m pip install opencv-python (with the explicit python -m) to ensure pip installs to the Python you are running. Then test with the same Python: python -c "import cv2".
import cv2 works in script but fails in Jupyter Notebook
Jupyter uses a different kernel Python. In the notebook run: import sys; print(sys.executable) to see which Python. Then install OpenCV in that Python: !pip install opencv-python from a notebook cell.

DLL load failed specifically?

Full fix guide for WinError 126 and missing DLLs.

DLL error guide