'import cv2' works fine in spyder and in command prompt BUT gives error when a program is run from Anaconda Prompt

33 views Asked by At

I am trying to use opencv in my code. It works fine when I run it directly from spyder or from command prompt using import cv2. I have a small code in file 'test_opencv_installation.py' that contains import cv2 as following

import cv2

image = cv2.imread("logo.png")

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cv2.imshow("OpenCV logo", image)

cv2.imshow("OpenCV logo gray format", gray_image)

cv2.waitKey(0)

cv2.destroyAllWindows()

However, The following error occurred when running the file "C:\Anaconda\envs\my_opencv\test_opencv_installation.py":

The module 'cv2' could not be found or imported.

This is the full error message:

Traceback (most recent call last):
File "C:\Anaconda\envs\my_opencv\test_opencv_installation.py", line 6, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'

enter image description here

Please share your experience and kindly suggest me any solution to this. Thanks....

0

There are 0 answers