Attachment 'Tutorial.txt'


   1 Tutorial
   2 py2exe turns Python programs into packages that can be run on other Windows computers without needing to install Python on those computers. Python is needed on the computer where py2exe itself is run because py2exe is a Python program and it includes parts of Python in the package that is built. 
   4 To successfully complete this tutorial you'll need to know the basics of Python (you can get started at's getting started page). You'll also need to know how to run Python programs from the command prompt. 
   6 There are a few simple steps needed to use py2exe once you've installed it: 
   8 Create/test your program 
  10 Create your setup script ( 
  12 Run your setup script 
  14 Test your executable 
  16 Build an installer if applicable 
  19 Create/test your program
  20 The biggest step is almost always the first one. The good news is that py2exe typically has little or no impact on this step. The vast majority of things you can do with Python will work with py2exe. Many modules just work seamlessly with py2exe, but some third party modules will require a little extra work. Luckily there is help available at WorkingWithVariousPackagesAndModules. 
  22 It's important that you make sure everything is working before you use py2exe. If py2exe fixes a broken program, then that's probably a bug in py2exe that needs to be fixed! 
  24 The first example we'll use here is our old friend... 
  27 Toggle line numbers 
  28    1 print "Hello World!"
  31 We need to make sure it's working... 
  34 C:\Tutorial>python
  35 Hello World
  37 C:\Tutorial>
  38 Looks good! 
  41 Create your setup script (
  42 py2exe extends Distutils with a new "command". If you've installed third party Python modules then there's a good chance you've seen at least one distutils command: 
  45 C:\Tutorial>python install
  46 "install" is a Distutils command that installs something (typically a Python module or package). The details Distutils needs to do that installation are contained in (and sometimes other associated files). 
  48 "py2exe" is a new Distutils command that is added when you import py2exe. To use py2exe you need to create a file to tell Distutils and py2exe what you want to do. Here's a whose simplicity is appropriate for our sample program... 
  51 Toggle line numbers 
  52    1 from distutils.core import setup
  53    2 import py2exe
  54    3 
  55    4 setup(console=[''])
  58 Notice that this is ordinary Python. Let's go through it line by line... 
  60 When working with py2exe the only part of Distutils we'll typically need to reference directly is the setup function, so that's all we'll import. 
  61 Once Distutils is loaded, we need to load py2exe so that it can add its command. 
  62 Whitespace is good! 
  63 Call setup and tell it that we want a single console application and the main entry point is "". 
  65 Run your setup script
  66 The next step is to run your setup script. Make sure to give the py2exe command and expect to see lots and lots of output: 
  69 C:\Tutorial>python py2exe
  70 running py2exe
  71 *** searching for required modules ***
  72 *** parsing results ***
  73 creating python loader for extension 'zlib'
  74 creating python loader for extension 'unicodedata'
  75 creating python loader for extension 'bz2'
  76 *** finding dlls needed ***
  77 *** create binaries ***
  78 *** byte compile python files ***
  79 byte-compiling C:\Tutorial\build\bdist.win32\winexe\temp\ to bz2.pyc
  80 byte-compiling C:\Tutorial\build\bdist.win32\winexe\temp\ to unicodedata.pyc
  81 byte-compiling C:\Tutorial\build\bdist.win32\winexe\temp\ to zlib.pyc
  82 skipping byte-compilation of c:\Python24\lib\ to StringIO.pyc
  84 [skipping many lines for brevity]
  86 skipping byte-compilation of c:\Python24\lib\ to warnings.pyc
  87 *** copy extensions ***
  88 *** copy dlls ***
  89 copying c:\Python24\lib\site-packages\py2exe\run.exe -> C:\Tutorial\dist\hello.exe
  91 *** binary dependencies ***
  92 Your executable(s) also depend on these dlls which are not included,
  93 you may or may not need to distribute them.
  95 Make sure you have the license if you distribute any of them, and
  96 make sure you don't distribute files belonging to the operating system.
  98    ADVAPI32.dll - C:\WINDOWS\system32\ADVAPI32.dll
  99    USER32.dll - C:\WINDOWS\system32\USER32.dll
 100    SHELL32.dll - C:\WINDOWS\system32\SHELL32.dll
 101    KERNEL32.dll - C:\WINDOWS\system32\KERNEL32.dll
 103 C:\Tutorial>
 104 Two directories will be created when you run your setup script... build and dist. The build directory is used as working space while your application is being packaged. It is safe to delete the build directory after your setup script has finished running. The files in the dist directory are the ones needed to run your application. If you are using Python 2.4 or newer then MSVCR71.dll will be included in dist - this is a Microsoft DLL and you need to determine if you have the legal right to distribute it. 
 107 Test your executable
 108 Now that the package has been created it is ready to test: 
 111 C:\Tutorial>cd dist
 113 C:\Tutorial\dist>hello.exe
 114 Hello World
 115 Excellent, it works!!! 
 118 Build an installer if applicable
 119 py2exe is not an installer builder - it merely assembles the files needed to run your Python program. There are plenty of good installer builders out there including some that are open source (e.g., NSIS) and some that are free (e.g., Inno Setup). 
 121 Tutorial (last edited 2008-03-25 13:13:35 by MarioFrasca

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.

Unable to edit the page? See the FrontPage for instructions.