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
Answer from Sergio Tabares on Stack OverflowIt 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
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
opencv - import cv2 in python in vs code not working - Stack Overflow
Unable to Import cv2 when Setting up YOLOv5 in Visual Studio Code
Error: No module named 'cv2' despite installing OpenCV?
[Bug] - opencv can't work at vscode
Videos
» pip install opencv-python
So I installed OpenCV and pip via cmd, and I installed the newest Python version 3.9. I also write my code on Visual Studio Code. Basically whenever I try to run it in VS Code, this error shows up, meanwhile when I open it outside of VS Code, only a window pops up and disappears after 1 second.
What can be the reason? I also should mention that I have the Python file and OpenCV files in different places
I'm new to opencv - python, the opencv is installed and it works well on Jupyter notebook and terminal I have tried. I always use vscode to code, so I turn to vscode instead. However, I can't import cv2 on it. I have tried everything i can find online, but it still doesn't work.
ImportError: No module named cv2
does anyone could help me figure out this problem.