1

Hi i am facing issues When i import cv2 in my python code and convert the python code to C using Cython and then using the .c and .h file in Visual studio and using it in the main.cpp .It says no module named cv2 occurs when i execute the Main.cpp . I am able to create the openCVTestcode.c and openCVTestcode.h file and i have added to the Visual studio project and calling it from main.cpp. it works fine while importing numpy and it prints the result also. The issue is when i use openCv. i get no module named cv2 error. Should we add openCV path or so in Visual studio project? But this was not needed for Numpy.

When i use cimport cv2 it says cimported module has no attribute 'imread'. I have a openCVTestcode.pyx file and i am compiling usingpython -m cython openCVTestcode.pyxfrom the anaconda prompt. From this i get the .c and .h files which i am adding to a visual studio project and calling the function in the .h files from the main.cpp

The code for openCVtestcode.pyx is :

import cv2
cdef public void openCVtest():
 image = cv2.imread("Test.png")
 cv2.imshow("test",image)
 cv2.waitKey(0)

The code for Main.cpp is

int main(void) {
Py_Initialize(); //Needed!
PyInit_openCVTestcode(); //Needed! called PyInit_hello() for Python3
openCVtest();
Py_Finalize(); //Needed!
return 0;
}

I am able to build it. But the .exe created says

Traceback (most recent call last):
File "openCVTestcode.pyx", line 2, in init openCVTestcode
ModuleNotFoundError: No module named 'cv2'
Exception ignored in: 'openCVTestcode.openCVtest'
Traceback (most recent call last):
File "openCVTestcode.pyx", line 2, in init openCVTestcode
ModuleNotFoundError: No module named 'cv2'
asked May 17, 2018 at 3:05
4
  • The python path isn’t set in the environment in which you start your program and thus the packages cannot be found. Compare the python-paths in your python installation and in the exe Commented May 17, 2018 at 18:33
  • Thanks a lot it worked Commented May 18, 2018 at 16:18
  • You might describe what you have done to solve the problem in an answer, so others can learn from it. Commented May 24, 2018 at 17:30
  • i installed cv2 from command prompt and then i was able to solve the issue Commented May 28, 2018 at 1:51

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.