Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

py2exe fails to create EXE due to missing DLL when opencv is imported

Tags:

python

opencv

dll

I'm a Python and coding newbie.I have tried to search for posts,but seems like I can't find other facing with the same problem.

I'm using Python 2.7 and opencv3.0 on Windows 10.

I tried to convert my .py file to an EXE file using py2exe. However, the command window told me.

error: [Errno 2] No such file or directory: 'api-ms-win-core-registry-l1-1-0.dll'

This is my setup.py

from distutils.core import setup
import py2exe
import matplotlib
import FileDialog
setup(windows=['HW6.py'],
      options={
                'py2exe': { 
                "includes" : ["matplotlib.backends.backend_tkagg","cv2"],

                'excludes': ['_gtkagg', '_tkagg', '_agg2', '_cairo','_cocoaagg', 
                              "matplotlib.numerix.fft","sip", "PyQt4._qt",
                              "matplotlib.backends.backend_qt4agg",
                              "matplotlib.numerix.linear_algebra", 
                              "matplotlib.numerix.random_array",
                             '_fltkagg', '_gtk','_gtkcairo' ],

                'dll_excludes': ['libgdk-win32-2.0-0.dll',
                                 'libgobject-2.0-0.dll' ,
                                 'MSVCP90.dll']
              }

                },

      data_files=matplotlib.get_py2exe_datafiles(),) 

After I excluded 'api-ms-win-core-registry-l1-1-0.dll', another error popped up and said

error: [Errno 2] No such file or directory: 'api-ms-win-core-errorhandling-l1-1-1.dll'

After I excluded lots of DLLs, still some DLLs are missing. Here are what I have excluded

'api-ms-win-core-string-l1-1-0.dll',
'api-ms-win-core-registry-l1-1-0.dll',
'api-ms-win-core-errorhandling-l1-1-1.dll',
'api-ms-win-core-string-l2-1-0.dll',
'api-ms-win-core-profile-l1-1-0.dll',
'api-ms-win-core-processthreads-l1-1-2.dll',
'api-ms-win-core-libraryloader-l1-2-1.dll',
'api-ms-win-core-file-l1-2-1.dll',
'api-ms-win-security-base-l1-2-0.dll',
'api-ms-win-eventing-provider-l1-1-0.dll',
'api-ms-win-core-heap-l2-1-0.dll',
'api-ms-win-core-libraryloader-l1-2-0.dll',
'api-ms-win-core-localization-l1-2-1.dll',
'api-ms-win-core-sysinfo-l1-2-1.dll',
'api-ms-win-core-synch-l1-2-0.dll',
'api-ms-win-core-heap-l1-2-0.dll'

Here are things imported in 'HW6.py'

import matplotlib.pyplot as plt
from Tkinter import *
from PIL import ImageTk,Image
import numpy as np
import copy
import FileDialog
import warnings
import cv2

Without

import cv2

Everything works fine. I have no idea how to deal with those DLLs. Thanks!

like image 919
tsao Avatar asked Dec 29 '15 06:12

tsao


3 Answers

I think I have figured out what exactly what the issue is. This is because that the cv2.pyd is dependent on some system level dll (try "dumpbin /dependents cv2.pyd", if you have Visual Studio installed), and they are not ignored by py2exe, e.g.

MSVFW32.dll
AVIFIL32.dll
AVICAP32.dll
ADVAPI32.dll
CRYPT32.dll
WLDAP32.dll

And these dll replies on the "api-ms-win-core-*****". If you want to distribute your software to other machines, these dll should not be copied too.

So at last, a more sensible solution maybe to exclude the dlls above, rather than "api-ms-win-core-*****", and in this way, the problem shall also be solved,

"dll_excludes": ["MSVFW32.dll",
                 "AVIFIL32.dll",
                 "AVICAP32.dll",
                 "ADVAPI32.dll",
                 "CRYPT32.dll",
                 "WLDAP32.dll"]
like image 120
Saedrna Avatar answered Nov 15 '22 16:11

Saedrna


I'm not using opencv, but was stuck on same issue. After altering py2exe to tell me which images were importing which DLLs, I found that extending the list to include the below worked for my imported libs (even though some are hardcoded to be excluded in py2exe itself already - the code comments state it's not comprehensive):

"dll_excludes": [
'MSVCP90.dll',
'IPHLPAPI.DLL',
'NSI.dll',
'WINNSI.DLL',
'WTSAPI32.dll',
'SHFOLDER.dll',
'PSAPI.dll',
'MSVCR120.dll',
'MSVCP120.dll',
'CRYPT32.dll',
'GDI32.dll',
'ADVAPI32.dll',
'CFGMGR32.dll',
'USER32.dll',
'POWRPROF.dll',
'MSIMG32.dll',
'WINSTA.dll',
'MSVCR90.dll',
'KERNEL32.dll',
'MPR.dll',
'Secur32.dll',
]
like image 37
RuiDC Avatar answered Nov 15 '22 18:11

RuiDC


Hardly an insightful answer, but i had the same problem and can say that the full list of api-ms-core* dlls that need to be excluded is for me:

"dll_excludes": ["MSVCP90.dll","libzmq.pyd","geos_c.dll","api-ms-win-core-string-l1-1-0.dll","api-ms-win-core-registry-l1-1-0.dll","api-ms-win-core-errorhandling-l1-1-1.dll","api-ms-win-core-string-l2-1-0.dll","api-ms-win-core-profile-l1-1-0.dll","api-ms-win*.dll","api-ms-win-core-processthreads-l1-1-2.dll","api-ms-win-core-libraryloader-l1-2-1.dll","api-ms-win-core-file-l1-2-1.dll","api-ms-win-security-base-l1-2-0.dll","api-ms-win-eventing-provider-l1-1-0.dll","api-ms-win-core-heap-l2-1-0.dll","api-ms-win-core-libraryloader-l1-2-0.dll","api-ms-win-core-localization-l1-2-1.dll","api-ms-win-core-sysinfo-l1-2-1.dll","api-ms-win-core-synch-l1-2-0.dll","api-ms-win-core-heap-l1-2-0.dll","api-ms-win-core-handle-l1-1-0.dll","api-ms-win-core-io-l1-1-1.dll","api-ms-win-core-com-l1-1-1.dll","api-ms-win-core-memory-l1-1-2.dll","api-ms-win-core-version-l1-1-1.dll","api-ms-win-core-version-l1-1-0.dll"]

Everything worked as normal after that.

like image 38
bw4sz Avatar answered Nov 15 '22 17:11

bw4sz