py2exe turns Python programs into packages than can be run on other Windows computers without needing to install Python on those computers. There are a few simple steps needed to use py2exe once you've installed it:
- [#Step1 Create/test your program]
- [#Step2 Create your setup script (setup.py)]
- Run your setup script
- Test your executable
- Build an installer if applicable
Create/test your program
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.
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!
The first example we'll use here is our old friend...
1 print "Hello World!"
We need to make sure it's working...
C:\Tutorial>python hello.py Hello World C:\Tutorial>
Create your setup script (setup.py)
py2exe extends [http://www.python.org/doc/current/dist/ 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:
C:\>python setup.py install
"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).
"py2exe" is a new Distutils command that is added when you install 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 who's simplicity is appropriate for our sample program...
Notice that this is ordinary Python. Let's go through it line by line...
- 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.
- Once Distutils is loaded, we need to load py2exe so that it can add its command.
- Whitespace is good!
- Call setup and tell it that we want a single console application and the main entry point is "hello.py".
MORE TO COME!!!