Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug a py2exe 'application failed to initialize properly' error?

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.

like image 555
soapergem Avatar asked Jul 20 '09 13:07

soapergem


1 Answers

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

This package installs runtime components of C Runtime (CRT), Standard C++, ATL, MFC, OpenMP and MSDIA libraries. For libraries that support side-by-side deployment model (CRT, SCL, ATL, MFC, OpenMP) they are installed into the native assembly cache, also called WinSxS folder, on versions of Windows operating system that support side-by-side assemblies.

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 your setup.py. I added the following section:

<dependency>
    <dependentAssembly>
        <assemblyIdentity
            type="win32"
            name="Microsoft.VC90.CRT"
            version="9.0.30729.4918"
            processorArchitecture="X86"
            publicKeyToken="1fc8b3b9a1e18e3b"
            language="*"
        />
    </dependentAssembly>
</dependency>

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 correct version, 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 ;-)

like image 116
Vinay Sajip Avatar answered Sep 20 '22 18:09

Vinay Sajip