Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import "cv2" could not be resolved [duplicate]

I've installed python and opencv by using the correct commands in the cmd, but when I type in 'import cv2' in a python then I get the error in visual studio code and I can't find any solution online how to fix this

Import "cv2" could not be resolved Pylance (reportMissingImports)

More of the error in the terminal

ModuleNotFoundError: No module named 'cv2'
like image 875
HelloHello Avatar asked Nov 19 '25 16:11

HelloHello


2 Answers

It just happened to me and I solved it installing both opencv-python and opencv-python-headless with pip and reloading the Visual Studio Code window right after it.

To install the needed packages, just run this command in the terminal:

$ pip install opencv-python opencv-python-headless
like image 68
Sergio Tabares Avatar answered Nov 22 '25 05:11

Sergio Tabares


I had a similar issue so I installed opencv-python-headless (install opencv-python if not earlier) and reloaded the VScode window.

pip install opencv-python-headless

However, some methods like imshow() kept failing:

cv2.error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'

This was resolved by installing opencv-contrib-python. You can install it by running the following code in your terminal:

pip install opencv-contrib-python
like image 24
Shailuch20 Avatar answered Nov 22 '25 05:11

Shailuch20



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!