python Py2exe - win32api.pyc ImportError DLL 加载失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1979486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:28:31  来源:igfitidea点击:

Py2exe - win32api.pyc ImportError DLL load failed

pythonpy2exe

提问by Wim

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:

我正在尝试使用 py2exe 来分发我编写的 python 应用程序。一切似乎都很顺利,但是当我在另一台机器上运行它时,它失败并出现以下错误:

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 -

在 win32api 之前导入 pywintypes 和 pythoncom(在 py2exe 的 setup.py 和主应用程序中)向 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

乔恩

回答by Wim

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.dlland mswsock.dllto 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.

当包是在 Vista 上构建但在 XP 上执行时,我已经看到了这个问题。原来,这个问题是该py2exe误加powrprof.dll,并mswsock.dll在包装上。但是,Windows XP 包含自己的这些文件的副本,并且无法加载随您的应用程序一起安装的 Vista 文件。

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

将它们从包中删除就可以了,您可以通过将其添加到optionsdict 中来轻松做到这一点setup.py

 'dll_excludes': [ "mswsock.dll", "powrprof.dll" ]

回答by tullaman

@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:

@Wim,我发现“将其添加到 setup.py 中的选项字典”有点令人困惑。如果像我一样,您在现有的 setup 调用中没有 options arg 这可能会使事情变得更清楚:

setup(name='myprog',     
      ...
      options={"py2exe":{"dll_excludes":[ "mswsock.dll", "powrprof.dll" ]}},
      ...
      )

回答by fragmint

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.

就像添加评论一样。使用 Py2exe 重建程序时,请务必删除旧的“dist”目录。我坐了 3 个多小时不明白为什么我的应用程序在我的开发环境中运行而不是在生产环境中运行。删除了 dist 并使用 py2exe 重建,它工作正常。

回答by Ryan Ginstrom

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

尝试在选项字典中将 win32api 添加到您的包中。

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 ...