After writing an application, you may want to distribute it to users who do not have Python and wxPython installed.

Packaging your Application for Windows Users

For converting your application to Windows executable format, look no further than py2exe (http://www.py2exe.org/).

  1. Make sure that you have distutils installed (if you have a recent Python version, you most likely do). DO NOT install distutils if you already have it, because then you will get cryptic errors later on.
  2. Download and install py2exe.
  3. Navigate to your application directory (the one with your main Python file), and create a file called 'setup.py'.
  4. Copy and paste the following into your setup.py:

   1 #!/usr/bin/python
   2 
   3 from distutils.core import setup
   4 import py2exe
   5 
   6 manifest = """
   7 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   8 <assembly xmlns="urn:schemas-microsoft-com:asm.v1"
   9 manifestVersion="1.0">
  10 <assemblyIdentity
  11     version="0.64.1.0"
  12     processorArchitecture="x86"
  13     name="Controls"
  14     type="win32"
  15 />
  16 <description>Your Application</description>
  17 <dependency>
  18     <dependentAssembly>
  19         <assemblyIdentity
  20             type="win32"
  21             name="Microsoft.Windows.Common-Controls"
  22             version="6.0.0.0"
  23             processorArchitecture="X86"
  24             publicKeyToken="6595b64144ccf1df"
  25             language="*"
  26         />
  27     </dependentAssembly>
  28 </dependency>
  29 </assembly>
  30 """
  31 
  32 """
  33 installs manifest and icon into the .exe
  34 but icon is still needed as we open it
  35 for the window icon (not just the .exe)
  36 changelog and logo are included in dist
  37 """
  38 
  39 setup(
  40     windows = [
  41         {
  42             "script": "yourapplication.py",
  43             "icon_resources": [(1, "yourapplication.ico")],
  44             "other_resources": [(24,1,manifest)]
  45         }
  46     ],
  47       data_files=["yourapplication.ico"]
  48 )

Looking at the examples on the py2exe website, you may wonder why there is so much more text in this setup.py file. When you use wxPython and py2exe on Windows, you need to include the manifest file - that's what the manifest thing is.

  1. I'm sure your main Python file isn't called "yourapplication.py", so change the names in setup.py accordingly. If you don't have an icon for your application, you can delete the "icon_resources" and data_files fields. However, I suggest that you find and use an icon, because then your executable looks more inviting.
  2. Using the command line, navigate to your application directory and type in "python setup.py py2exe". py2exe detects all the dependencies and creates the executable, so you need not worry about specifying the libraries you used.
  3. After it's done, look in your application directory for a directory called "dist", generated by py2exe. In it should be your executable file, if all went well.

It's so... big!

Yes, the generated files tend to be somewhat bulky, but that's the price you pay for giving your users the bare bones of wxPython, Python, and all the other libraries you've used. However, you can significantly cut your application down to size using certain techniques, which are contained in the following batch file:

@echo off 

"C:\Python24\python.exe" build.py py2exe

cd dist

"C:\Program Files\7-Zip\7z.exe" -aoa x library.zip -olibrary\ 
del library.zip 
 
cd library\ 
"C:\Program Files\7-Zip\7z.exe" a -tzip -mx9 ..\library.zip -r 
cd.. 
rd library /s /q 
 
"C:\devel\upx201w\upx.exe" --best *.* 

I'm assuming that you have 7-Zip (http://www.7-zip.org/) and UPX (http://upx.sourceforge.net/) installed. Change the paths in the batch file to the corresponding paths on your computer. Using 7-Zip's superior compression and UPX's executable size reduction, I managed to strip my 10.5 megabyte application down to 4.5 megabytes.

Creating the Installer

I use NSIS (http://nsis.sourceforge.net/Main_Page) to generate my installers, but you don't have to. The other major installer generator is Inno Setup (http://www.jrsoftware.org/isinfo.php). Combined with NSIS, I use HM NIS Edit (http://hmne.sourceforge.net/) to generate my scripts from a plug-in. Both of these installer tools further compress your application - use LZMA compression, as it generally works the best.

You are intelligent if you got this far, so I'll leave learning how to use these excellent installer tools to you.

DistributingYourApplication (last edited 2008-03-11 10:50:29 by localhost)