Introduction

This page will provide a basic setup.py for Python 2.6 for a simple wxPython based script (e.g. the one shown on the previous page).

Notes to the setup.py

I use GUI2Exe to create the inital version of my setup.py, I then export them and maintain them with my standard IDE.

If you want to reduce the number of files in your "dist" folder then it is recommended to use the "bundle=3" and the "zipfile = "lib/library.zip" options. "bundle=2" will reduce this further by including more of the files in the library.zip, however it might cause problems with more complex applications, "bundle=1" is not recommended as it causes to often problems.

In the dll_excludes option the following dll's "'mswsock.dll', 'powrprof.dll', 'uxTheme.dll'" are listed to force py2exe to ignore them. Should they be included you would get problems on systems other then the one you frooze your application on.

In the dll_excludes option you also need to add "'MSVCP90.dll'," as otherwise py2exe will throw an exception when you try to freeze the application.

Notes to the manifest

The manifest included in the following setup.py ensures that your application has a "nicer" look, by forcing the application to use version 6 of the Microsoft common controls.

It is a bit different then for Python 2.5 and earlier as there is an additional section in it for the MS C Run time version 9 stuff (sometimes refered to as SxS (Side by Side), the files needed for this are MSVCR90.dll and some application might also need MSVCP90.dll, they are included with your Python 2.6 installation (either in your Python 2.6 folder if you installed "for you only", otherwise they are in the "SxS" folder "C:\Windows\winsxs" on most systems), they are also in the Visual Studio redistribution folder or they can be obtained from the Microsoft web site. I keep them in a folder "Py26MSdlls-9.0.21022.8" and include them from there, which is shown in the following.

freezing your application

To "freeze" the application you run the following command from the command line in your working folder, or use the appropriate option in your IDE.

\python26\python setup.py py2exe

With py2exe version 0.6.9 you will see a deprecation warning for the "sets" module, which you can savely ignore.

   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 # do the same for dist folder
  19 shutil.rmtree("dist", ignore_errors=True)
  20 
  21 MANIFEST_TEMPLATE = """
  22 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  23 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  24   <assemblyIdentity
  25     version="5.0.0.0"
  26     processorArchitecture="x86"
  27     name="%(prog)s"
  28     type="win32"
  29   />
  30   <description>%(prog)s</description>
  31   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
  32     <security>
  33       <requestedPrivileges>
  34         <requestedExecutionLevel
  35             level="asInvoker"
  36             uiAccess="false">
  37         </requestedExecutionLevel>
  38       </requestedPrivileges>
  39     </security>
  40   </trustInfo>
  41   <dependency>
  42     <dependentAssembly>
  43       <assemblyIdentity
  44             type="win32"
  45             name="Microsoft.VC90.CRT"
  46             version="9.0.21022.8"
  47             processorArchitecture="x86"
  48             publicKeyToken="1fc8b3b9a1e18e3b">
  49       </assemblyIdentity>
  50     </dependentAssembly>
  51   </dependency>
  52   <dependency>
  53     <dependentAssembly>
  54         <assemblyIdentity
  55             type="win32"
  56             name="Microsoft.Windows.Common-Controls"
  57             version="6.0.0.0"
  58             processorArchitecture="X86"
  59             publicKeyToken="6595b64144ccf1df"
  60             language="*"
  61         />
  62     </dependentAssembly>
  63   </dependency>
  64 </assembly>
  65 """
  66 
  67 class Target(object):
  68     """ A simple class that holds information on our executable file. """
  69     def __init__(self, **kw):
  70         """ Default class constructor. Update as you need. """
  71         self.__dict__.update(kw)
  72         
  73         
  74 # Ok, let's explain why I am doing that.
  75 # Often, data_files, excludes and dll_excludes (but also resources)
  76 # can be very long list of things, and this will clutter too much
  77 # the setup call at the end of this file. So, I put all the big lists
  78 # here and I wrap them using the textwrap module.
  79 
  80 data_files = []
  81 
  82 includes = []
  83 excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
  84             'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
  85             'Tkconstants', 'Tkinter']
  86 packages = []
  87 dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll',
  88                 'tk84.dll', 
  89                 'MSVCP90.dll', 'mswsock.dll', 'powrprof.dll']
  90 icon_resources = []
  91 bitmap_resources = []
  92 other_resources = []
  93 other_resources = [(24, 1, MANIFEST_TEMPLATE % dict(prog="MyAppName"))]
  94 
  95 
  96 # This is a place where the user custom code may go. You can do almost
  97 # whatever you want, even modify the data_files, includes and friends
  98 # here as long as they have the same variable name that the setup call
  99 # below is expecting.
 100 
 101 
 102 #
 103 # The following will copy the MSVC run time dll's
 104 # (msvcm90.dll, msvcp90.dll and msvcr90.dll) and
 105 # the Microsoft.VC90.CRT.manifest which I keep in the
 106 # "Py26MSdlls" folder to the dist folder
 107 #
 108 # depending on wx widgets you use, you might need to add
 109 # gdiplus.dll to the above collection
 110 
 111 py26MSdll = glob.glob(r"c:\Dev\Py26MSdlls-9.0.21022.8\msvc\*.*")
 112 
 113 # install the MSVC 9 runtime dll's into the application folder
 114 data_files += [("", py26MSdll),]
 115 
 116 # I found on some systems one has to put them into sub-folders.
 117 ##data_files += [("Microsoft.VC90.CRT", py26MSdll),
 118 ##               ("lib\Microsoft.VC90.CRT", py26MSdll)]
 119 
 120 
 121 
 122 # Ok, now we are going to build our target class.
 123 # I chose this building strategy as it works perfectly for me :-D
 124 
 125 
 126 GUI2Exe_Target_1 = Target(
 127     # what to build
 128     script = "simplewx.py",
 129     icon_resources = icon_resources,
 130     bitmap_resources = bitmap_resources,
 131     other_resources = other_resources,
 132     dest_base = "simplewx",    
 133     version = "0.1",
 134     company_name = "No Company",
 135     copyright = "No Copyrights",
 136     name = "Py2Exe Sample File"
 137     )
 138 
 139 
 140 
 141 # That's serious now: we have all (or almost all) the options py2exe
 142 # supports. I put them all even if some of them are usually defaulted
 143 # and not used. Some of them I didn't even know about.
 144 
 145 setup(
 146 
 147     data_files = data_files,
 148 
 149     options = {"py2exe": {"compressed": 2, 
 150                           "optimize": 2,
 151                           "includes": includes,
 152                           "excludes": excludes,
 153                           "packages": packages,
 154                           "dll_excludes": dll_excludes,
 155                           "bundle_files": 2,
 156                           "dist_dir": "dist",
 157                           "xref": False,
 158                           "skip_archive": False,
 159                           "ascii": False,
 160                           "custom_boot_script": '',
 161                          }
 162               },
 163 
 164     zipfile = "lib\library.zip",
 165     console = [],
 166     windows = [GUI2Exe_Target_1]
 167     )
 168 
 169 # This is a place where any post-compile code may go.
 170 # You can add as much code as you want, which can be used, for example,
 171 # to clean up your folders or to do some particular post-compilation
 172 # actions.
 173 
 174 # And we are done. That's a setup script :-D

Additional notes by users

Please use this section to add a note about your experience with py2exe and wxPython.

py2exe-python26 (last edited 2010-08-08 15:50:28 by 113)