Problem with importing cv in python IDLE

Hello,

Im a complete newbie in OpenCV and have a very basic question.

I followed the tutorial from

https://docs.opencv.org/4.x/d5/de5/tutorial_py_setup_in_windows.html

for getting OpenCV run with python and im already stuck at
"
5. Goto opencv/build/python/2.7 folder.
6. Copy cv2.pyd to C:/Python27/lib/site-packages.
"

above in the tutorial it is said, that newer versions of Python should work fine, so I have 3.10 installed.

Now when I follow these steps and copy cv2.pyd to C:/Python310/lib/site-packages and try to import it in IDLE it gives me following error

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    import cv2 as cv
ImportError: DLL load failed while importing cv2: Das angegebene Modul wurde nicht gefunden.

Last line in german says “the declared module was not found”.

Obviously I can imagine, that a file made for python 2.7 like in the opencv/build/… directory won’t work with python 3.10. But I don’t see any alternative, considering there is no cv2.pyd file for Python 3.10 in the openCV directories.

I hope the solution is as obvious as I suppose. Any help is appreciated.

Kind regards,
Kintaro

don’t do any of that. best remove that cv2.pyd from site-packages before proceeding.

instead, just pip3 install opencv-contrib-python and you’ll get everything installed.

what you tried to do would have required changes to the PATH environment variable. that cv2.pyd would have been for python3, if you downloaded a current binary build.

Unfortunately it still doesnt work.

I still get the error message

import cv
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    import cv
ModuleNotFoundError: No module named 'cv'
import cv2
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    import cv2
ImportError: DLL load failed while importing cv2: Das angegebene Modul wurde nicht gefunden.

when I try to import with

import cv2

or

import cv

In my site-packages directory, I now have the folder opencv_contrib_python-4.5.5.64.dist-info

but none without the .dist-info as I have one of both with numpy for example.

ok so clean out everything in site-packages that has “cv2” in the name.

“ImportError: DLL load failed” usually only happens when you install stuff manually. I’ve never had that happen with the package from PyPI (pip install …).

if that still doesn’t work, you should try rebooting the computer.

if that still doesn’t work, post your PATH variable

Thank you for your help.

It still didn’t work, though. I’ll post the path-variables tonight, when I am back from work.

Im not quite sure which path-variable you mean exactly, but I figure the only ones making sense in this context are the ones of python.

So in my user-variables I have under path:

C:\Users\User\AppData\Local\Programs\Python\Python310
C:\Users\K\AppData\Local\Programs\Python\Python310\Scripts
C:\Users\K\AppData\Local\Microsoft\WindowsApps
C:\Users\K.dotnet\tools

and in my system-variables under path I have:

%SystemRoot%\system32
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
%SystemRoot%
%SystemRoot%\System32\Wbem
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0
%SYSTEMROOT%\System32\OpenSSH\

Is it possible, that the whole problem has something to do with opencv being for 32 bit and python 3.10.4 is 64 bit so it has problems with compatibility?

Otherwise Im not sure which mistakes I could have done since I simply followed the steps in the tutorials.

no, OpenCV is 64-bit.

that tutorial is outdated.

if you just want OpenCV in python, there’s nothing to build and nothing to “download”. just pip install opencv-contrib-python and make sure to install just one of the packages. opencv-contrib-python conflicts with opencv-python. they both contain the base modules.

that “pyshell” (IDLE?) stuff makes me wonder… try without IDLE, do this:

  • type Win+R
  • enter python
  • click OK

image

then type import cv2 into the terminal and see what that does. does that also complain about not finding DLLs, like IDLE does?

I tried that before and it gave me the same error.

I figured that I probably made some non-obvious rookie mistake when setting up python (even though I can not imagine what that might have been, since I didn’t do anything special like changing directories or such) and just uninstalled python 3.10.x and installed the stable version 3.9.13, even though Im not 100 % sure what stable means and if that has any benefits in my regards.

Nevertheless, after that I again installed opencv using your recommended command and it worked. Now I can import opencv and access its methods in the IDLE, as well as in other IDE’s.

Sorry if I wasted your time, but in the end your help was the key to getting it run and beyond that I learned a thing or two. So, again, thank you very much. Great to have guys like you, to help out guys like me :).

PS: If you have any tips, where I could find a (free) consistent opencv/python-tutorial, that’d be great. I mean there are lots on youtube of course, but maybe there is one you could recommend.