7

I am trying to use py2exe to distribute a python application I have written. Everything seems to go OK, but when I run it on another machine it fails with the following error:

Traceback (most recent call last):
  File "application.py", line 12, in <module>
  File "win32api.pyc", line 12, in <module>
  File "win32api.pyc", line 10, in __load
ImportError: DLL load failed: The specified procedure could not be found.

I have googled for this and not found very much, but have tried the following suggestions to no avail:

Imported pywintypes and pythoncom before win32api (in the setup.py for py2exe and in the main application) Added some code to the setup.py -

# ModuleFinder can't handle runtime changes to __path__, but win32com uses them
import pywintypes
import pythoncom
import win32api
try:
# if this doesn't work, try import modulefinder
    import py2exe.mf as modulefinder
    import win32com
    for p in win32com.__path__[1:]:
        modulefinder.AddPackagePath("win32com", p)
    for extra in ["win32com.shell"]: #,"win32com.mapi"
        __import__(extra)
        m = sys.modules[extra]
        for p in m.__path__[1:]:
            modulefinder.AddPackagePath(extra, p)
except ImportError:
    # no build path setup, no worries.
    pass

I'm quite new to all this, so any help would be greatly appreciated

Thanks

Jon

4 Answers4

29

I've seen this problem when the package was built on Vista but executed on XP. The problem turned out to be that py2exe mistakenly added powrprof.dll and mswsock.dll to the package. Windows XP contains its own copies of these files though, and can't load the Vista ones which got installed with your app.

Removing them from the package did the trick, you can do this easy by adding this to the options dict in setup.py

 'dll_excludes': [ "mswsock.dll", "powrprof.dll" ]
Wim
  • 11,091
  • 41
  • 58
  • Thank you very much ! That did the trick, all working perfectly now –  Dec 30 '09 at 15:42
  • We've seen the same thing, and powrprof.dll showed up in the dependency list using the Dependency Walker tool. After adding it to 'dll_excludes', it works fine. – Ivo Mar 16 '10 at 13:56
  • 3
    Note that, by default, py2exe does not clear out the contents of the distribution directory, so if you previously tried to install your software, the 'mswsock.dll' and 'powrprof.dll' files will still be there, even after a fresh install (once you added the 'dll_excludes' line above). Just be sure to delete the distribution directory before re-running py2exe. – ishmael Jan 24 '12 at 18:08
2

@Wim, I found the bit about "adding this to the options dict in setup.py" a bit confusing. If like me you did not have an options arg in your existing call to setup this might make things clearer:

setup(name='myprog',     
      ...
      options={"py2exe":{"dll_excludes":[ "mswsock.dll", "powrprof.dll" ]}},
      ...
      )
tullaman
  • 176
  • 2
  • 8
0

Try adding win32api to your packages, in the options dictionary.

Here's an example:

excludes = ["pywin", "pywin.debugger"] # there will be more in real life...
options = dict(optimize=2,
           dist_dir="build",
           excludes=excludes,
           packages=["win32api"]) 
setup(
    name="MyCoolApp",
    options=dict(py2exe=options),
    # etc ...
Ryan Ginstrom
  • 13,915
  • 5
  • 45
  • 60
0

Just as an added comment. When rebuilding your program with Py2exe be sure to delete the old "dist" directory. I was sitting for over 3 hours not understanding why my app was working on my dev envirnoment and not in production. deleted dist and rebuild with py2exe and it worked.

fragmint
  • 322
  • 2
  • 6