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. 3 4 To successfully complete this tutorial you'll need to know the basics of Python (you can get started at python.org's getting started page). You'll also need to know how to run Python programs from the command prompt. 5 6 There are a few simple steps needed to use py2exe once you've installed it: 7 8 Create/test your program 9 10 Create your setup script (setup.py) 11 12 Run your setup script 13 14 Test your executable 15 16 Build an installer if applicable 17 18 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. 21 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! 23 24 The first example we'll use here is our old friend... 25 26 27 Toggle line numbers 28 1 print "Hello World!" 29 hello.py 30 31 We need to make sure it's working... 32 33 34 C:\Tutorial>python hello.py 35 Hello World 36 37 C:\Tutorial> 38 Looks good! 39 40 41 Create your setup script (setup.py) 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: 43 44 45 C:\Tutorial>python setup.py 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 setup.py (and sometimes other associated files). 47 48 "py2exe" is a new Distutils command that is added when you import py2exe. To use py2exe you need to create a setup.py file to tell Distutils and py2exe what you want to do. Here's a setup.py whose simplicity is appropriate for our sample program... 49 50 51 Toggle line numbers 52 1 from distutils.core import setup 53 2 import py2exe 54 3 55 4 setup(console=['hello.py']) 56 setup.py 57 58 Notice that this is ordinary Python. Let's go through it line by line... 59 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 "hello.py". 64 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: 67 68 69 C:\Tutorial>python setup.py 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\bz2.py to bz2.pyc 80 byte-compiling C:\Tutorial\build\bdist.win32\winexe\temp\unicodedata.py to unicodedata.pyc 81 byte-compiling C:\Tutorial\build\bdist.win32\winexe\temp\zlib.py to zlib.pyc 82 skipping byte-compilation of c:\Python24\lib\StringIO.py to StringIO.pyc 83 84 [skipping many lines for brevity] 85 86 skipping byte-compilation of c:\Python24\lib\warnings.py to warnings.pyc 87 *** copy extensions *** 88 *** copy dlls *** 89 copying c:\Python24\lib\site-packages\py2exe\run.exe -> C:\Tutorial\dist\hello.exe 90 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. 94 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. 97 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 102 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. 105 106 107 Test your executable 108 Now that the package has been created it is ready to test: 109 110 111 C:\Tutorial>cd dist 112 113 C:\Tutorial\dist>hello.exe 114 Hello World 115 Excellent, it works!!! 116 117 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). 120 121 Tutorial (last edited 2008-03-25 13:13:35 by MarioFrasca
Attached FilesTo 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.