ImportError for cv2 with lackey in a virtualenv

Question:

Big picture is

In Windows 7, lackey installed with pip in a python3.6.1 virtualenv created with a python2.7.2 interpreter from an unpacked .tar.gz of virtualenv15.1.0 can’t import the cv2 module.

Context / Environment

In Windows 7 (x64) with C:Usersuser1>C:Python27python.exe C:VirtualenvToolsvirtualenv-15.1.0virtualenv.py --python=C:VirtualenvToolsPython36Python.exe C:virtualenvsvirtualenv (all one line) I create a virtualenv and set its C:virtualenvsvirtualenvnameScriptsPython.exe file as the main interpreter for a PyDev project in Eclipse.

I also installed lackey after entering the virtualenv with activate and then the pip install lackey command @ the C:virtualenvsvirtualenvnameScripts directory without any listed errors (installation worked).

The problem

When I import lackey with from lackey import * in my PyDev project, there is an ImportError from Eclipse’s console.

The console’s stackTrace:
Traceback (most recent call last):
File "C:Usersuser1workspacesikulixframework0.1testManager.py", line 4, in <module>
from lackey import *
File "C:virtualenvsvirtualenvnamelibsite-packageslackey__init__.py", line 31, in <module>
from .RegionMatching import Pattern, Region, Match, Screen, ObserveEvent
File "C:virtualenvsvirtualenvnamelibsite-packageslackeyRegionMatching.py", line 17, in <module>
import cv2
File "C:virtualenvsvirtualenvnamelibsite-packagescv2__init__.py", line 7, in <module>
from . import cv2
ImportError: DLL load failed: Le module spécifié est introuvable.

(Le module spécifié est introuvable. = The specified module could not be found.)

When using the same interpreter (C:virtualenvsvirtualenvnameScriptsPython.exe) directly, if I write from lackey import * I get the same stackTrace

My resolution tries

I tried the same things (from lackey import *) outside a virtual environment and it worked. Like suggested in an answer to the stackoverflow’s question ‘Can’t import cv2; “DLL load failed”’, I downloaded the Visual C++ 2015 redistributable package but nothing of my complications changed.

Asked By: Dominic Paré

||

Answers:

I added, in the Windows PATH environment variable, the path to the original Python3.6.1 from the pathtooriginalpython36python.exe in C:Usersuser1>C:Python27python.exe C:VirtualenvToolsvirtualenv.py --python=C:VirtualenvToolsPython36Python.exe C:virtualenvsvirtualenv command for the virtualenv’s creation that contained that python3.dll file and added that same python3.dll file in the C:virtualenvsvirtualenvScripts directory and it worked!

Summary if you use virtualenv and an ImportError occurs (for cv2 at least)

  1. Make sure that the path to the executable (which contains the python3.dll file in python3.6.1 at least) that is referenced by the virtualenv (after that --python= part of the creation command) is in the “PATH” Windows environment variable.
  2. Add that same python3.dll file to the Scripts directory where the virtualenv is.

The lackey project git owner(glitchassassin)’s answer helped me resolve this issue. He also states that this problem is caused by virtualenv:

Looks like this is actually also an issue in virtualenv.

On the issue of virtualenv, some people found the same solution:

[N]o need to download DLL files from untrusted random Internet sites, just copy the one from c:/Python3.5/ (or wherever you installed Python 3.5) into any directory on your %PATH%.

Answered By: Dominic Paré