3

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?

Paul Du Bois
  • 2,097
  • 1
  • 20
  • 31
  • Possible duplicate of [DLL load failed error when importing cv2](https://stackoverflow.com/questions/43184887/dll-load-failed-error-when-importing-cv2) – phd Jan 20 '18 at 03:06
  • I read that question before creating this one, and it's not applicable; conda is not involved, and the accepted answer to that question is incorrect. And additionally: this question also asks how to diagnose the general root problem. – Paul Du Bois Jan 22 '18 at 17:45

1 Answers1

6

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\"
Paul Du Bois
  • 2,097
  • 1
  • 20
  • 31