Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I diagnose ImportError: DLL load failed when loading native Windows python modules in a virtualenv?

I am using Python 3.x and a virtualenv -- not conda, just a plain virtualenv. I activate the venv and run pip install opencv-python. However, import cv2 gives me a DLL not found error:

(tf) C:\>python
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\src\venv\tf\lib\site-packages\cv2\__init__.py", line 4, in <module>
    from .cv2 import *
ImportError: DLL load failed: The specified module could not be found.
>>>

Is this a virtualenv bug? How do I figure out which module/dll is missing?

like image 634
Paul Du Bois Avatar asked Feb 11 '26 17:02

Paul Du Bois


1 Answers

On resolving "module could not be found" errors in general

Try using either Microsoft's Dependency Walker or lucasg's Dependencies on the module being loaded. Be sure to run Dependencies.exe from your virtualenv's command prompt, so it picks up your modified PATH.

The import line is from .cv2 import *, so the module being loaded is in the same directory as __init__.py (this is the leading .) and named cv2-SOMETHING.pyd (this is what native Python modules look like). Load that file into Dependencies.exe and it will show you the DLL that Windows wants but can't find.

In this case, the DLL is Python3.dll. Why is it missing? Because of a virtualenv bug that is fixed, but hasn't made its way into a release -- there hasn't been a release in more than a year.

On resolving this issue in particular

The github issue suggests a fix: use venv.

Alternatively you can copy the missing python3.dll into your virtualenv by hand. You'll have to do this for every virtualenv you create.

copy "c:\Program Files\Python36\python3.dll" "c:\src\venv\tf\Scripts\"
like image 146
Paul Du Bois Avatar answered Feb 13 '26 09:02

Paul Du Bois



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!