Monday, December 5, 2011

py2exe, Windows 7 & Vista

I don't use py2exe very often, but it can be a useful tool for environments that may not have an existing Python installation.

I recently used py2exe on my Windows 7 PC to build a small tasktray tool. The resulting executable ran fine on my PC (doesn't it always?), but threw an exception on any Vista PC it was run on.

File "win32com\__init__.pyo", line 5, in File "win32api.pyo", line 12, in 
File "win32api.pyo", line 10, in __load
ImportError: DLL load failed: The specified module could not be found.
After more online searching than I'd like to admit, I found a post that said py2exe may be including W7-specific DLLs, when instead it should be leaving those out, forcing Vista to go find its native builds of those DLLs.

I was able to fix the problem by adding two DLLs to the "dll_excludes" list in my py2exe setup script:
options = {
   "bundle_files": 3,
   "compressed": 1,
   "optimize": 1,
   "excludes": excludes,
   "packages": packages,
   'dll_excludes': [ 'mswsock.dll', 'powrprof.dll' ]
The tool now runs on both Vista and Windows 7.