No module named cv2 although opencv has been installed already

I wanted to try the simple code which was supposed to convert text to video like this

import cv2
import numpy as np

cap = cv2.VideoCapture(0) 
#more lines of code follow

I tried moving cv2.so around in my source code folder and everywhere. I tried even uninstalling and re-installing opencv then running this code over and over again. My terminal on VSCode gets terminated by error No module named cv2 - none of my efforts helped. I am about to bang off my keyboard out of frustration.

Could anyone kindly tell me what went wrong here?

What does pip list | grep opencv return?

If it’s empty, run pip install opencv-python.

1 Like

This command line gave me this output opencv-python 4.12.0.88
I just tried running a python code with cv2 but still got no module named cv2.. No idea what went wrong here

Try import sys and print(sys.path). See whether the path to OpenCV is listed in the output. Carefully check the list of paths.

After putting cv2.so back, I’d also check if you’re using the right python executable to run your script. Are you using a miniconda environment? That’s always the recommended way of managing dependencies like opencv and torch.