如何调试 py2exe“应用程序未能正确初始化” 错误?
一般来说,我对 Python 很陌生,但我用 Python 2.6 / wxPython 2.8 制作了一个应用程序,当我通过 Python 运行它时,它可以完美运行。 但我想更进一步,能够将其部署为 Windows 可执行文件,因此我一直在尝试 py2exe。 但我还没能让它发挥作用。 它总是会编译一个 exe,但是当我实际尝试运行它时,它会发出一些神秘的错误消息。 起初它们是简单的消息,说它找不到某些 DLL,但即使在给了它想要的所有 DLL 之后,它现在还是返回这样的消息:
The application failed to initialize properly (0xc0000142).
Click OK to terminate the application.
所以我把事情分解了,只是利用 wxPython 制作了一个非常非常简单的应用程序,只是为了看看如果这可行,或者我原来的应用程序的一些更复杂的功能是否妨碍了。 但即使我的简单测试也返回了相同的错误。 这是简单测试脚本的代码:
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, wx.ID_ANY, title, style=wx.DEFAULT_FRAME_STYLE ^ wx.MAXIMIZE_BOX)
panel = wx.Panel(self, -1, style = wx.TAB_TRAVERSAL | wx.CLIP_CHILDREN | wx.FULL_REPAINT_ON_RESIZE)
main_sizer = wx.BoxSizer(wx.VERTICAL)
testtxt = wx.StaticText(panel, -1, label='This is a test!')
main_sizer.Add(testtxt, 0, wx.ALIGN_CENTER)
panel.SetSizerAndFit(main_sizer)
self.Show(1)
return
app = wx.PySimpleApp()
frame = MainWindow(None, -1, 'Test App')
app.MainLoop()
这是我使用的 py2exe 安装脚本:)
#!/usr/bin/python
from distutils.core import setup
import py2exe
manifest = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1"
manifestVersion="1.0">
<assemblyIdentity
version="0.64.1.0"
processorArchitecture="x86"
name="Controls"
type="win32"
/>
<description>Test Program</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
"""
setup(
windows = [
{
"script": "testme.py",
"icon_resources": [(1, "testme.ico")],
"other_resources": [(24,1, manifest)]
}
],
data_files=["testme.ico"]
然后
我运行 python setup.py py2exe,它生成 EXE 文件,警告一些 DLL 文件(我随后复制到 dist 目录中),但是当我尝试运行 EXE 时,我立即收到上面引用的错误。
I'm very new to Python in general, but I made an app in Python 2.6 / wxPython 2.8 that works perfectly when I run it through Python. But I wanted to go a step further and be able to deploy it as a Windows executable, so I've been trying out py2exe. But I haven't been able to get it to work. It would always compile an exe, but when I actually try to run that it barks some cryptic error message. At first they were simple messages saying it couldn't find certain DLLs, but even after giving it all the DLLs it wanted, it now returns this:
The application failed to initialize properly (0xc0000142).
Click OK to terminate the application.
So I broke things down and just made a very, very simple application utilizing wxPython just to see if that would work, or if some of the more complicated features of my original app were getting in the way. But even my simple test returned the same error. Here's the code for the simple test script:
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, wx.ID_ANY, title, style=wx.DEFAULT_FRAME_STYLE ^ wx.MAXIMIZE_BOX)
panel = wx.Panel(self, -1, style = wx.TAB_TRAVERSAL | wx.CLIP_CHILDREN | wx.FULL_REPAINT_ON_RESIZE)
main_sizer = wx.BoxSizer(wx.VERTICAL)
testtxt = wx.StaticText(panel, -1, label='This is a test!')
main_sizer.Add(testtxt, 0, wx.ALIGN_CENTER)
panel.SetSizerAndFit(main_sizer)
self.Show(1)
return
app = wx.PySimpleApp()
frame = MainWindow(None, -1, 'Test App')
app.MainLoop()
And here's the py2exe setup script I used:
#!/usr/bin/python
from distutils.core import setup
import py2exe
manifest = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1"
manifestVersion="1.0">
<assemblyIdentity
version="0.64.1.0"
processorArchitecture="x86"
name="Controls"
type="win32"
/>
<description>Test Program</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
"""
setup(
windows = [
{
"script": "testme.py",
"icon_resources": [(1, "testme.ico")],
"other_resources": [(24,1, manifest)]
}
],
data_files=["testme.ico"]
)
Then I run python setup.py py2exe
, it generates the EXE file, warns about some DLL files (which I subsequently copy into the dist directory), but then when I try to run the EXE, I get the error I quoted above immediately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请注意,有更高版本的 Visual C++ 2008 可再发行组件包:
您可能会在
%WINDIR%\WinSxS
文件夹中找到这些文件,而不是在路径中。我认为您需要做的是合并相关 DLL 的清单信息(在% WINDIR%\WinSxS\Manifests
) 到您的setup.py
中。 我添加以下部分:
紧接在现有
部分之后,并重建 exe:它运行没有问题。 注意:根据您安装的 Visual C++ 文件的具体版本,上述信息可能不完全正确。 查看系统上的清单并使用正确的版本
、publicKeyToken
等。或者,查看此答案了解如何使用应用程序部署 DLL(而不是假设它们已存在于目标系统上) 。 哦...我看到你问了原来的问题;-)
Note that there is a later version of the Visual C++ 2008 Redistributable package: SP1. However, both the SP1 and the earlier release don't install the DLLs into the path. As the download page says (my emphasis):
You will probably find these files in the
%WINDIR%\WinSxS
folder and not in the path.What I think you need to do is incorporate the manifest information for the relevant DLLs (found in%WINDIR%\WinSxS\Manifests
) into yoursetup.py
. Iadded the following section:
immediately after the existing
<dependency>
section, and rebuilt the exe: it ran without problems. Note: depending on exactly what version of the Visual C++ files you installed, the above info may not be exactly correct. Look at the manifests on your system and use the correctversion
,publicKeyToken
etc.Alternatively, look at this answer for how to deploy the DLLs with your application (as opposed to assuming they already exist on the target system). Oh ... I see you asked that original question ;-)
我以前使用过 py2exe,从来没有遇到过这样的情况......
但是,听起来缺少依赖项是你的问题......
打包的程序是否可以在你的机器上运行,但不能在其他机器上运行?
如果是这样,请在两台计算机上的 DEPENDS(依赖项遍历器)中运行打包的应用程序,并进行比较,以期找出哪些包未包含在内。
祝你好运
I've used py2exe before and I've never run across a situation like this....
However, it sounds like missing dependencies are your problem.....
Does the packaged program work on your machine but not on others?
If so, run the packaged application within DEPENDS (dependency walker) on both machines and compare to hopefully discern which packages didn't get included.
Good Luck
您不应该复制它所抱怨的所有 .dll! 其中一些是 Windows 系统文件,并且它们存在于系统上的正确位置。 如果将它们复制到 dist 文件夹中,事情将无法正常工作。
一般来说,您只想复制特定于您的应用程序的 .dll。 不是系统 .dll。 在某些情况下,您可能需要在安装程序中附带 vcredist_xx.exe 以获取系统上的 MSVC 运行时。 您永远不应该尝试自己“原始”发送这些 .dll 文件。 使用 redist 包,它将节省您的时间和挫败感。
您是否尝试按照此处的说明进行操作:http://wiki.wxpython.org/SmallApp?
You are not supposed to copy ALL of the .dlls it complains about! Some of them are Windows system files, and they are present in the correct places on the system. If you copy them into the dist folder, things won't work correctly.
In general, you only want to copy .dlls which are specific to your application. Not system .dlls. In some situations you may need to ship vcredist_xx.exe in your installer to get the MSVC runtime on the system. You should never try to ship those .dlls files "raw" by yourself. Use the redist package, it will save you time and frustration.
Have you tried following the directions here: http://wiki.wxpython.org/SmallApp ?
您确定您提供的 dll 与 wxPython 使用的 dll 相同吗?
wxpython使用的vc++ dll可以从wxpython下载页面下载。 你尝试过这些吗?
Are you sure that you give the same dlls that the one used by wxPython.
The vc++ dlls used by wxpython can be downloaded from the wxpython download page. Did you try these one?