Introduction

The following will explain how to freeze a Python/wxPython application using the py2exe tool.

What Objects are Involved

You will need a working development environment including

* Python

* wxPython

* py2exe

* Gui2Exe - by Andrea Gavana

Process Overview

A tiny sample wxPython "Hello world" application will be used to demonstrate the process. I created the tiny application using Boa Constructor but you could use any other IDE you use for your wxPython development. The creation of the setup.py file I did using Gui2Exe, but again one could create this by hand.

Special Concerns

Python 2.5x

Python 2.6x

The setup.py

Save the following code in your working folder as a file called 'setup.py'.

To "freeze" the application you run from the command line in your working folder the following command.

\python26\python setup.py py2exe

Toggle line numbers
   1 # ======================================================#
   2 # File automagically generated by GUI2Exe version 0.3
   3 # Andrea Gavana, 01 April 2007
   4 # ======================================================#
   5 
   6 # Let's start with some default (for me) imports...
   7 
   8 from distutils.core import setup
   9 import py2exe
  10 import glob
  11 import os
  12 import zlib
  13 import shutil
  14 
  15 # Remove the build folder
  16 shutil.rmtree("build", ignore_errors=True)
  17 
  18 class Target(object):
  19     """ A simple class that holds information on our executable file. """
  20     def __init__(self, **kw):
  21         """ Default class constructor. Update as you need. """
  22         self.__dict__.update(kw)
  23         
  24 
  25 # Ok, let's explain why I am doing that.
  26 # Often, data_files, excludes and dll_excludes (but also resources)
  27 # can be very long list of things, and this will clutter too much
  28 # the setup call at the end of this file. So, I put all the big lists
  29 # here and I wrap them using the textwrap module.
  30 
  31 data_files = []
  32 
  33 includes = []
  34 excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
  35             'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
  36             'Tkconstants', 'Tkinter']
  37 packages = []
  38 dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll',
  39                 'tk84.dll']
  40 icon_resources = []
  41 bitmap_resources = []
  42 other_resources = []
  43 
  44 
  45 # This is a place where the user custom code may go. You can do almost
  46 # whatever you want, even modify the data_files, includes and friends
  47 # here as long as they have the same variable name that the setup call
  48 # below is expecting.
  49 
  50 #
  51 # The following will copy the MSVC run time dll's
  52 # (msvcm90.dll, msvcp90.dll and msvcr90.dll) and
  53 # the Microsoft.VC90.CRT.manifest which I keep in the
  54 # "Py26MSdlls" folder to the dist folder
  55 #
  56 # depending on wx widgets you use, you might need to add
  57 # gdiplus.dll to the above collection
  58 
  59 py26MSdll = glob.glob(r"c:\dev\Py26MSdlls\*.*")
  60 
  61 data_files += [("", py26MSdll),
  62                           ]
  63 
  64 
  65 # Ok, now we are going to build our target class.
  66 # I chose this building strategy as it works perfectly for me :-D
  67 
  68 
  69 GUI2Exe_Target_1 = Target(
  70     # what to build
  71     script = "simplewx.py",
  72     icon_resources = icon_resources,
  73     bitmap_resources = bitmap_resources,
  74     other_resources = other_resources,
  75     dest_base = "simplewx",    
  76     version = "0.1",
  77     company_name = "No Company",
  78     copyright = "No Copyrights",
  79     name = "Py2Exe Sample File"
  80     )
  81 
  82 
  83 
  84 # That's serious now: we have all (or almost all) the options py2exe
  85 # supports. I put them all even if some of them are usually defaulted
  86 # and not used. Some of them I didn't even know about.
  87 
  88 setup(
  89 
  90     data_files = data_files,
  91 
  92     options = {"py2exe": {"compressed": 2, 
  93                           "optimize": 2,
  94                           "includes": includes,
  95                           "excludes": excludes,
  96                           "packages": packages,
  97                           "dll_excludes": dll_excludes,
  98                           "bundle_files": 3,
  99                           "dist_dir": "dist",
 100                           "xref": False,
 101                           "skip_archive": False,
 102                           "ascii": False,
 103                           "custom_boot_script": '',
 104                          }
 105               },
 106 
 107     zipfile = r'lib/library.zip',
 108     console = [],
 109     windows = [GUI2Exe_Target_1]
 110     )
 111 
 112 
 113 # And we are done. That's a setup script :-D
 114 
 115 # Run setup standalone...
 116 if __name__ == "__main__":
 117     print 'just running setup'
 118     setup()
 119     print 'setup done'
 120 
 121 print 'clean up'
 122 
 123 # This is a place where any post-compile code may go.
 124 # You can add as much code as you want, which can be used, for example,
 125 # to clean up your folders or to do some particular post-compilation
 126 # actions.
 127 
 128 # for some reason some files remain sometimes in dist, but they
 129 # are already present in dist/lib, so lets remove them
 130 # 
 131 
 132 cwdFolder = os.getcwd()
 133 libFile = os.path.join(cwdFolder, 'dist\\library.zip')
 134 if os.path.exists(libFile):
 135         print 'deleting duplicate files'
 136         os.remove(libFile)
 137         os.remove(os.path.join(cwdFolder, 'dist\\bz2.pyd'))
 138         os.remove(os.path.join(cwdFolder, 'dist\\select.pyd'))
 139         os.remove(os.path.join(cwdFolder, 'dist\\unicodedata.pyd'))

The MS manifest

Following is the content of the Microsoft manifest file, note that the content of "version" and "publicKeyToken" are specific to the version of the dll files. Installing Python 2.6 on Windows 7rc1 with the option "for this user only" this manifest file is created in the Python26 folder. Note the Py26 installer does not offer the "for this user only" option on Vista.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Copyright (c) Microsoft Corporation.  All rights reserved. -->
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <noInheritable/>
    <assemblyIdentity
        type="win32"
        name="Microsoft.VC90.CRT"
        version="9.0.21022.8"
        processorArchitecture="x86"
        publicKeyToken="1fc8b3b9a1e18e3b"
    />
    <file name="msvcr90.dll" /> <file name="msvcp90.dll" /> <file name="msvcm90.dll" />
</assembly>

Sample wxPython application

Save the following code in your working folder as a file called 'simplewx.py'.

Toggle line numbers
   1 #Boa:Frame:Frame1
   2 
   3 import wx
   4 
   5 def create(parent):
   6     return Frame1(parent)
   7 
   8 [wxID_FRAME1, wxID_FRAME1PANEL1, wxID_FRAME1STATICTEXT1, 
   9 ] = [wx.NewId() for _init_ctrls in range(3)]
  10 
  11 class Frame1(wx.Frame):
  12     def _init_coll_bsPanel_Items(self, parent):
  13         # generated method, don't edit
  14 
  15         parent.AddWindow(self.staticText1, 0, border=10,
  16               flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL | wx.ALL | wx.EXPAND)
  17 
  18     def _init_coll_bsFrame_Items(self, parent):
  19         # generated method, don't edit
  20 
  21         parent.AddWindow(self.panel1, 1, border=2,
  22               flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL)
  23 
  24     def _init_sizers(self):
  25         # generated method, don't edit
  26         self.bsPanel = wx.BoxSizer(orient=wx.VERTICAL)
  27 
  28         self.bsFrame = wx.BoxSizer(orient=wx.VERTICAL)
  29 
  30         self._init_coll_bsPanel_Items(self.bsPanel)
  31         self._init_coll_bsFrame_Items(self.bsFrame)
  32 
  33         self.panel1.SetSizer(self.bsPanel)
  34         self.SetSizer(self.bsFrame)
  35 
  36     def _init_ctrls(self, prnt):
  37         # generated method, don't edit
  38         wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
  39               pos=wx.Point(642, 279), size=wx.Size(124, 86),
  40               style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
  41         self.SetClientSize(wx.Size(108, 50))
  42 
  43         self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
  44               pos=wx.Point(2, 2), size=wx.Size(104, 46),
  45               style=wx.TAB_TRAVERSAL)
  46 
  47         self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
  48               label=u'Hello world!', name='staticText1', parent=self.panel1,
  49               pos=wx.Point(10, 10), size=wx.Size(84, 13),
  50               style=wx.ALIGN_CENTRE)
  51 
  52         self._init_sizers()
  53 
  54     def __init__(self, parent):
  55         self._init_ctrls(parent)
  56         
  57 
  58 if __name__ == '__main__':
  59     app = wx.PySimpleApp()
  60     frame = create(None)
  61     frame.Show()
  62 
  63     app.MainLoop()

Sample wxPython application

Save the following code in your working folder as a file called 'simplewx.py'.

Toggle line numbers
   1 #Boa:Frame:Frame1
   2 
   3 import wx
   4 
   5 def create(parent):
   6     return Frame1(parent)
   7 
   8 [wxID_FRAME1, wxID_FRAME1PANEL1, wxID_FRAME1STATICTEXT1, 
   9 ] = [wx.NewId() for _init_ctrls in range(3)]
  10 
  11 class Frame1(wx.Frame):
  12     def _init_coll_bsPanel_Items(self, parent):
  13         # generated method, don't edit
  14 
  15         parent.AddWindow(self.staticText1, 0, border=10,
  16               flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL | wx.ALL | wx.EXPAND)
  17 
  18     def _init_coll_bsFrame_Items(self, parent):
  19         # generated method, don't edit
  20 
  21         parent.AddWindow(self.panel1, 1, border=2,
  22               flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL)
  23 
  24     def _init_sizers(self):
  25         # generated method, don't edit
  26         self.bsPanel = wx.BoxSizer(orient=wx.VERTICAL)
  27 
  28         self.bsFrame = wx.BoxSizer(orient=wx.VERTICAL)
  29 
  30         self._init_coll_bsPanel_Items(self.bsPanel)
  31         self._init_coll_bsFrame_Items(self.bsFrame)
  32 
  33         self.panel1.SetSizer(self.bsPanel)
  34         self.SetSizer(self.bsFrame)
  35 
  36     def _init_ctrls(self, prnt):
  37         # generated method, don't edit
  38         wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
  39               pos=wx.Point(642, 279), size=wx.Size(124, 86),
  40               style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
  41         self.SetClientSize(wx.Size(108, 50))
  42 
  43         self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
  44               pos=wx.Point(2, 2), size=wx.Size(104, 46),
  45               style=wx.TAB_TRAVERSAL)
  46 
  47         self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
  48               label=u'Hello world!', name='staticText1', parent=self.panel1,
  49               pos=wx.Point(10, 10), size=wx.Size(84, 13),
  50               style=wx.ALIGN_CENTRE)
  51 
  52         self._init_sizers()
  53 
  54     def __init__(self, parent):
  55         self._init_ctrls(parent)
  56         
  57 
  58 if __name__ == '__main__':
  59     app = wx.PySimpleApp()
  60     frame = create(None)
  61     frame.Show()
  62 
  63     app.MainLoop()

Comments

Please feel free to provide any feedback on this page either here, on the wxPython-user list or to werner.bruhin at free.fr.

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.