opencv-python icon indicating copy to clipboard operation
opencv-python copied to clipboard

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

Open 11philip22 opened this issue 1 year ago • 28 comments

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

  • example code import cv2
  • operating system Windows 10, i7-12700H with Intel Iris Xe Graphics and a NVIDIA RTX A1000
  • architecture (e.g. x86) x86-64
  • opencv-python version opencv-contrib-python-4.7.0.72
Issue submission checklist
  • [x] This is not a generic OpenCV usage question (looking for help for coding, other usage questions, homework etc.)
  • [x] I have read the README of this repository and understand that this repository provides only an automated build toolchain for OpenCV Python packages (there is no actual OpenCV code here)
  • [x] The issue is related to the build scripts in this repository, to the pre-built binaries or is a feature request (such as "please enable this additional dependency")
  • [x] I'm using the latest version of opencv-python

11philip22 avatar Jun 15 '23 07:06 11philip22

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 avatar Aug 18 '23 14:08 GenericAccount2

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

GenericAccount2 avatar Aug 18 '23 15:08 GenericAccount2

Is there a solution to this?

encouver avatar Aug 30 '23 16:08 encouver

Same problem, is there a solution ?

kker4m avatar Sep 10 '23 21:09 kker4m

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

GenericAccount2 avatar Sep 11 '23 05:09 GenericAccount2

@GenericAccount2 This was the solution. Thank you.

aBelnades avatar Sep 15 '23 11:09 aBelnades

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.

GenericAccount2 avatar Sep 15 '23 11:09 GenericAccount2

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

opencv-alalek avatar Sep 16 '23 00:09 opencv-alalek

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.

lanfengc avatar Oct 12 '23 03:10 lanfengc

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.

dcarrion87 avatar Feb 15 '24 05:02 dcarrion87

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.

cudawarped avatar Feb 15 '24 05:02 cudawarped

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

dcarrion87 avatar Feb 15 '24 08:02 dcarrion87

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

201811017 avatar Mar 15 '24 09:03 201811017

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

cudawarped avatar Mar 15 '24 09:03 cudawarped

I got the same problem.

jason660519 avatar Mar 26 '24 14:03 jason660519

I have the same problem (Python 3.11)

fluviotect avatar Mar 26 '24 23:03 fluviotect

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

cudawarped avatar Mar 27 '24 08:03 cudawarped

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.

catequalsgood avatar Apr 10 '24 13:04 catequalsgood

@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.

cudawarped avatar Apr 10 '24 13:04 cudawarped

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.

catequalsgood avatar Apr 10 '24 15:04 catequalsgood

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

cudawarped avatar Apr 10 '24 15:04 cudawarped

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

catequalsgood avatar Apr 11 '24 12:04 catequalsgood

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

@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?

cudawarped avatar Apr 11 '24 13:04 cudawarped

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.

Raknash3 avatar May 05 '24 23:05 Raknash3

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

quyq avatar Jun 23 '24 02:06 quyq

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.

JLin-45655 avatar Jul 13 '24 02:07 JLin-45655