opencv / opencv-python

Automated CI toolchain to produce precompiled opencv-python, opencv-python-headless, opencv-contrib-python and opencv-contrib-python-headless packages.
https://pypi.org/project/opencv-python/
MIT License
4.57k stars 855 forks source link

ImportError: DLL load failed while importing cv2: The specified module could not be found. #856

Open 11philip22 opened 1 year ago

11philip22 commented 1 year ago

Hi, I installed opencv-contrib-python 4.7.0.72 using pip for python 3.11.4 on Windows 10 x86-64 But when I run I get this exceptiom DLL load failed while importing cv2: The specified module could not be found.

Expected behaviour

I expect the module to import and not trow an error.

Actual behaviour

This exception gets trown

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python311\Lib\site-packages\cv2\__init__.py", line 181, in <module>
    bootstrap()
  File "C:\Python311\Lib\site-packages\cv2\__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: DLL load failed while importing cv2: The specified module could not be found.

Steps to reproduce

Issue submission checklist
GenericAccount2 commented 1 year ago

I have exactly this issue. Is there a simple way to see which DLL failed to load, that sounds like a natural starting point for investigation.

GenericAccount2 commented 1 year ago

After some investigation I see it fails on init.py in the cv2 folder on the line native_module = importlib.import_module("cv2") No idea why yet

encouver commented 1 year ago

Is there a solution to this?

kker4m commented 1 year ago

Same problem, is there a solution ?

GenericAccount2 commented 1 year ago

The instructions are a little out of date, but I believe they would solve the issue Go to Windows -> Settings -> Uninstall/Install Apps Go to Optional Features Go to Add button Look for Windows Media Pack

aBelnades commented 1 year ago

@GenericAccount2 This was the solution. Thank you.

GenericAccount2 commented 1 year ago

I think that OpenCV should consider updating their documentation. This is a frustrating error to encounter, and if documentation is out of date, and cannot be followed, nobody is normally going to self-solve their issue.

opencv-alalek commented 1 year ago

No idea which documentation you want to read.

https://pypi.org/project/opencv-python/

Q: Import fails on Windows: ImportError: DLL load failed: The specified module could not be found.? A: If the import fails on Windows, make sure you have Visual C++ redistributable 2015 installed. If you are using older Windows version than Windows 10 and latest system updates are not installed, Universal C Runtime might be also required. Windows N and KN editions do not include Media Feature Pack which is required by OpenCV. If you are using Windows N or KN edition, please install also Windows Media Feature Pack. ...

And this is a copy of top level README of this project:

https://github.com/opencv/opencv-python

lanfengc commented 1 year ago

In your Python environment, execute this code and install the required packages. For example, I created a virtual environment with conda, I executed "conda install numpy protobuf hdf5 cudnn cudatoolkit". I don't know if "cudnn cudatoolkit" has to be installed. Since I compiled OpenCV with CUDA, I need to install it.

dcarrion87 commented 9 months ago

Trying to run https://github.com/cudawarped/opencv-python-cuda-wheels on python 3.11 on Windows Server compiled by @cudawarped and getting import DLL error. All DLLs seem to be there. Not an issue when using python 3.7 on the same system.

cudawarped commented 9 months ago

Trying to run https://github.com/cudawarped/opencv-python-cuda-wheels on python 3.11 on Windows Server compiled by @cudawarped and getting import DLL error. All DLLs seem to be there. Not an issue when using python 3.7 on the same system.

This is not an officially supported release. Please raise an issue on that repo with more details regarding your setup and which wheel you are using and I'll take a look.

dcarrion87 commented 9 months ago

@cudawarped solved as part of: https://github.com/opencv/opencv-python/issues/955

201811017 commented 8 months ago

I have tried the solution of Windows Media pack and still I have the same issue. But i have the following paths in the environment variables: image

cudawarped commented 8 months ago

I have tried the solution of Windows Media pack and still I have the same issue

Use process monitor to track down the missing dll. See ImportError: DLL load failed while importing cv2: The specified module could not be found

jason660519 commented 8 months ago

I got the same problem.

fluviotect commented 8 months ago

I have the same problem (Python 3.11)

cudawarped commented 8 months ago

@jason660519 and @fluviotect when you ran process monitor which dll's couldn't it find?

catequalsgood commented 7 months ago

At least for me the issue seems to be __init__.cp311-win_amd64.pyd and __init__.pyd. For everything else Process Monitor eventually displays a SUCCESS-message. I am using Windows 11 Enterprise, OpenCV 4.6.0 and Python 3.11.8. Using 3.10.14 does not change the behaviour. I tried looking for the Windows Media Pack but is it neither offered as an optional feature nor does it show up as an installed feature. So maybe my issues is something completely different.

cudawarped commented 7 months ago

@catequalsgood I am presuming you are using a script to parse the process monitor output which is how you managed to filter it down to only those two entries, making the result extreemly odd.

Specifically I can't understand why python would be outputing an error that it can't find all the dll's when it has found them.

catequalsgood commented 7 months ago

I was not using a script but I changed that and looked at the log again. I did indeed miss one. intl.dll also never shows SUCCESS.

cudawarped commented 7 months ago

You may find it much easier to parse the exported list, see https://forum.opencv.org/t/dll-load-failed-when-importing-a-self-built-cv2-library-in-python/16942/15

catequalsgood commented 7 months ago

The output seems equivalent. More DLLs like VERSION.dll are returned because only loading version.dll is successful. __init__.cp311-win_amd64.pyd as well as __init__.pyd are of course missing from the output. Leaving only intl.dll

SubhamBurnwal-BiswaGames commented 7 months ago

this is such a growing issue.. I am unable to import cv2 for days and my whole project is stuck!

cudawarped commented 7 months ago

@SubhamBurnwal-BiswaGames Did you build it yourself? If so did you check for the missing dll using process explorer and if so what was missing?

Raknash3 commented 6 months ago

I too was facing the same issue. I have python 3.11.7 in my conda env and I used conda to install opencv and I faced the same import error. I reinstalled the Visual C++ redistributables, checked if opencv package was present in anaconda and even then I couldn't solve the issue. Then I noticed that conda by default installs opencv 4.7 and the lastest version was opencv 4.9. So I used pip to install the latest release of opencv 4.8.1.78 and now I can import the cv2 module successfully.

quyq commented 5 months ago

For me, gstream runtime dll are not in site-packages\cv2 folder. I'm using the way mentioned at here:, i.e. add following before import cv2: import os gst_root = os.getenv('GSTREAMER_1_0_ROOT_MSVC_X86_64', 'C:/gstreamer/1.0/msvc_x86_64/') os.add_dll_directory(gst_root+'bin') import cv2

JLin-45655 commented 4 months ago

I too was facing the same issue. I have python 3.11.7 in my conda env and I used conda to install opencv and I faced the same import error. I reinstalled the Visual C++ redistributables, checked if opencv package was present in anaconda and even then I couldn't solve the issue. Then I noticed that conda by default installs opencv 4.7 and the lastest version was opencv 4.9. So I used pip to install the latest release of opencv 4.8.1.78 and now I can import the cv2 module successfully.

I used Anaconda Navigator to install opencv and got version 4.6.6. Then I had this "DLL load failed while importing cv2" issue. I tried to use "Process Monitor" suggested by other link and found that "intl.dll" was not found. After seeing Raknash3's comment, I removed the opencv with Anaconda Navigator and tried "conda install -c conda-forge open.cv" and had 4.7.0 installed. Now it works. Thanks.

sina-razaghi commented 3 months ago

encountered this error on Windows 11 Enterprise N. To fix it, I installed the Media Feature Pack by going to Settings > System > Optional Features > Add an Optional Feature. If the issue persists after installing the Media Feature Pack, try reinstalling OpenCV."