Fix ModuleNotFoundError: No module named 'cv2' (OpenCV)

Quick answer

ModuleNotFoundError: No module named 'cv2' means OpenCV isn't installed in the Python you're running. You import cv2, but the pip package is opencv-python — and there are four variants. On a desktop use pip install opencv-python; on a server, Docker, or CI use pip install opencv-python-headless. Install exactly one. If import cv2 then fails with libGL.so.1, either use the headless build or apt-get install -y libgl1 libglib2.0-0.

You install OpenCV, import it, and Python still says:

ModuleNotFoundError: No module named 'cv2'

Two things make OpenCV trickier than a normal "just pip install it." First, the import name (cv2) doesn't match the package name (opencv-python) — and there are four packages to choose from, not one. Second, on a server you often fix the cv2 error only to hit a second one, libGL.so.1: cannot open shared object file, which almost no one connects to the first.

This guide picks the right package for your environment, installs it into the right interpreter, and preempts the libGL follow-on before it bites you in Docker.

Quick Answer

No module named 'cv2' means OpenCV isn't installed in the Python you're running. You import cv2, but the pip package is opencv-python. On a desktop: pip install opencv-python. On a server/Docker/CI: pip install opencv-python-headless. Install exactly one. If import cv2 then fails with libGL.so.1, use the headless build or apt-get install -y libgl1 libglib2.0-0.

TL;DR

  • Import cv2, install opencv-python — names differ.
  • Four variants — pick one; never mix them.
  • Servers/Docker/CI → opencv-python-headless (no GUI deps).
  • opencv-contrib-python only if you need contrib (SIFT, etc.).
  • libGL.so.1 follow-on → headless, or apt-get install libgl1 libglib2.0-0.
  • Installed but not found? Wrong interpreter → python -m pip.

Pick the Right Package

Which OpenCV package to install: base vs contrib, desktop vs headlessPick one package. Use opencv-python for a desktop app with GUI windows, opencv-contrib-python if you also need the extra contrib modules like SIFT. On a server, Docker, or CI, use the headless variants which drop the GUI dependencies. Never install more than one.Which do you need?Base modules+ contrib (SIFT, …)Desktop / GUI(cv2.imshow)opencv-pythonopencv-contrib-pythonServer / Docker/ CIopencv-python-headlessopencv-contrib-python-headlessInstall exactly ONE — mixing opencv-* packages conflicts
Which OpenCV package to install: base vs contrib, desktop vs headless

There isn't one OpenCV package — there are four, and they all provide the same cv2 import. Choose by two questions: do you open GUI windows, and do you need the contrib modules?

PackageUse when
opencv-pythonDesktop app that shows windows (cv2.imshow)
opencv-contrib-pythonDesktop and you need contrib (SIFT, trackers)
opencv-python-headlessServer, Docker, CI, or any headless runtime
opencv-contrib-python-headlessHeadless and you need contrib
# Desktop
pip install opencv-python
 
# Server / Docker / CI (recommended for backends)
pip install opencv-python-headless
⚠️

Install exactly one. All four ship the same cv2 module, so installing two (for example opencv-python and opencv-python-headless) causes conflicts and flaky behavior. If unsure, clear them all first:

pip uninstall -y opencv-python opencv-contrib-python opencv-python-headless opencv-contrib-python-headless
pip install opencv-python-headless   # then install the one you want

There's also no package literally named cv2pip install cv2 won't work. The package is opencv-python (or a variant above).

The libGL Follow-On (Docker's Favorite)

After installing opencv-python on a server, import cv2 fails with libGL.so.1 — use headless or install libgl1On a headless server, opencv-python imports but needs system OpenGL libraries, so import cv2 raises ImportError libGL.so.1 cannot open shared object file. Fix it by switching to opencv-python-headless, which needs no GUI libraries, or by installing libgl1 and libglib2.0-0 with the system package manager.pip install opencv-python(the GUI build, on a server)import cv2ImportError: libGL.so.1: cannotopen shared object filerecommendedFix A (servers):pip installopencv-python-headlessFix B (keep full opencv):apt-get install -ylibgl1 libglib2.0-0
After installing opencv-python on a server, import cv2 fails with libGL.so.1 — use headless or install libgl1

Here's the sequence that catches everyone deploying to a container or server. You fix No module named 'cv2' by installing opencv-python, then import cv2 throws:

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

This isn't a Python problem — the GUI build of OpenCV links against system OpenGL libraries that a headless server doesn't have. Two fixes:

# ✅ Fix A — recommended for servers/Docker: use the headless build
pip uninstall -y opencv-python
pip install opencv-python-headless
 
# ✅ Fix B — if you truly need the full (GUI) build: install the system libs
apt-get update && apt-get install -y libgl1 libglib2.0-0
  • Fix A removes the need for system GL entirely — best for backends and CI.
  • Fix B keeps GUI features; libglib2.0-0 also covers a common companion error (libgthread-2.0.so.0).
🐳

In a Dockerfile, prefer headless and you'll never touch apt:

RUN pip install opencv-python-headless

If you must use the GUI build:

RUN apt-get update && apt-get install -y libgl1 libglib2.0-0 && rm -rf /var/lib/apt/lists/*
RUN pip install opencv-python

Still "No module named cv2"? Check the Interpreter

If the package installed but the import still fails, you installed it into a different Python than the one running — the general cause of every ModuleNotFoundError. The same triage as the dotenv guide applies:

pip show opencv-python                       # installed? where?
python -c "import sys; print(sys.executable)"  # which Python runs my code?
 
# install into THIS interpreter to remove ambiguity
python -m pip install opencv-python-headless
  • Virtual env: activate it first, then install inside it.
  • Jupyter/Colab: %pip install opencv-python-headless, then restart the kernel.
  • conda: conda install -c conda-forge opencv (installs the cv2 module).

Verify

import cv2
print(cv2.__version__)     # e.g. 4.x — installed in this interpreter
 
# quick end-to-end check (no GUI needed)
import numpy as np
img = np.zeros((10, 10, 3), dtype=np.uint8)
print(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).shape)   # (10, 10)

Green state: import cv2 succeeds, cv2.__version__ prints, and a simple cvtColor runs — with no libGL.so.1 error on your server or in your container.

Prevention

  • Import cv2, install opencv-python (or a variant) — never cv2.
  • Use opencv-python-headless for servers, Docker, and CI by default.
  • Install exactly one opencv-* package.
  • Pin your chosen variant in requirements.txt.
  • In Docker, prefer headless; otherwise install libgl1 libglib2.0-0.
  • Use python -m pip and check sys.executable when an install "doesn't take".

Troubleshooting Matrix

SymptomLikely causeFix
No module named 'cv2'Not installedpip install opencv-python(-headless)
pip install cv2 failsWrong package nameInstall opencv-python
Installed but not foundWrong interpreterpython -m pip install ...
libGL.so.1 on importGUI build on headless hostHeadless build, or libgl1
libgthread-2.0.so.0Missing glibapt-get install -y libglib2.0-0
Odd crashes / double cv2Multiple opencv-* installedUninstall all, install one
Missing SIFT/trackerBase build lacks contribUse opencv-contrib-python(-headless)
Fails in Jupyter onlyDifferent kernel%pip install, restart kernel

External References

FAQs

Why is the import cv2 and not opencv? Historical: OpenCV's Python bindings have always exposed the module as cv2 (the 2 is from the C++ API generation, not the version). The pip distribution is named opencv-python. Import name and package name simply differ, like opencv-pythoncv2 and python-dotenvdotenv.

Is headless missing any image processing? No. Headless has the same image/video processing functions — it only omits the GUI/window functions (imshow, waitKey, namedWindow). For servers and pipelines you never use those anyway, so headless is a pure win.

Do I need NumPy too? opencv-python depends on NumPy and installs it automatically. If you see a NumPy error instead, it's usually a version mismatch — upgrade both: pip install -U numpy opencv-python-headless.

Which variant for a Flask/FastAPI backend? opencv-python-headless. Backends don't open windows, and headless avoids the libGL dependency entirely — fewer system packages, smaller image, no surprise runtime error.

How do I switch from the GUI build to headless? Uninstall the GUI build first, then install headless — don't layer them: pip uninstall -y opencv-python && pip install opencv-python-headless. Having both is what causes conflicts.

Key takeaways

  • You import cv2, but the pip package is opencv-python — the names differ.
  • There are four variants; install exactly one — mixing opencv-* packages conflicts.
  • On servers, Docker, or CI use opencv-python-headless to avoid GUI dependencies.
  • Use opencv-contrib-python only if you need the extra contrib modules like SIFT.
  • The classic follow-on error is libGL.so.1 — fix with headless or apt-get install libgl1.
  • If it's installed but not found, you're on a different interpreter — use python -m pip.

Frequently asked questions

What is the correct pip install for cv2?

pip install opencv-python. You import it as cv2, but the PyPI package is named opencv-python. There is no package called cv2 on PyPI, so pip install cv2 will not work. On a server or in Docker, install opencv-python-headless instead.

What's the difference between opencv-python and opencv-python-headless?

opencv-python includes GUI features like cv2.imshow and needs system OpenGL libraries. opencv-python-headless drops the GUI dependencies, so it installs cleanly on servers, Docker, and CI and avoids the libGL.so.1 error. Use headless anywhere you don't open windows.

When should I use opencv-contrib-python?

Only when you need the extra contrib modules — patented or experimental algorithms like SIFT, SURF, and some trackers. If you don't use those, plain opencv-python is smaller and sufficient. There are headless contrib builds too (opencv-contrib-python-headless) for servers.

How do I fix libGL.so.1: cannot open shared object file with cv2?

It means opencv-python needs system OpenGL libraries the server lacks. Either switch to opencv-python-headless (pip uninstall opencv-python; pip install opencv-python-headless), which needs no GUI libs, or install the libraries: apt-get install -y libgl1 libglib2.0-0. Headless is the cleaner fix for servers.

I installed opencv-python but import cv2 still fails — why?

You most likely installed it into a different Python than the one running your code. Run python -m pip install opencv-python so pip targets the exact interpreter, and check that sys.executable matches where pip installed the package. This is the general cause of ModuleNotFoundError.

Can I have both opencv-python and opencv-python-headless installed?

No. The opencv-* packages all provide the same cv2 module and conflict if installed together, causing unpredictable behavior. Uninstall all of them (pip uninstall opencv-python opencv-contrib-python opencv-python-headless), then install exactly one.


Related Posts