422,023 Members | 1,005 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Simple guide to using py2exe

true911m
P: 92
Here's a little walkthrough to get py2exe up and running. I'm not an expert, so I can't help much with any problems you might have. This is what worked for me.

The result here will be to convert a simple python app into a single .exe file that can be copied and run on any Windows XP machine. It may work on many other Windows platforms, but I haven't tested it.

You'll need a working Python installation first, preferably v2.3 or later. If you haven't installed py2exe yet, or if you have, but you're having problems, I suggest that you start with one of the py2exe self-installing files available for these versions, which put everything where it needs to be. If you need to use the .zip for some reason (older Python, etc.), I can't help much, because that's where I had my initial difficulties.

You can get py2exe at this SourceForge link. Click on the Download link, and choose the package from the list that matches your Python installation.

Finally, create yourself a new directory somewhere to store the following files so they can stay together until you're comfortable manipulating them.

Let's start with a basic Hello World app:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2. for i in xrange(10000):
  3.     print "Hello, World!"
Use your favorite editor and save this as HelloWorld.py.

Run it by double-clicking on it to make sure it works, i.e. that your Python path is setup correctly and so forth.

Now, create a python setup app, again with your choice of editor, and save it as setup.py:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2. from distutils.core import setup
  3. import py2exe
  4.  
  5. setup(
  6.     console=["HelloWorld.py"],
  7.     zipfile=None
  8.      )
I like to use a batch file to run it all, because I hate trying to coordinate the directories between the cmd shell and my other working directories. Type this in your editor:

Expand|Select|Wrap|Line Numbers
  1. python setup.py py2exe --bundle 1
and save it as setup.bat.

At this point, you should have three files, HelloWorld.py, setup.py, and setup.bat, all in the same directory.

Double-click on setup.bat and watch the fireworks. Depending on your hardware, it could take from a couple seconds to a minute to complete.

When the cmd window closes, check the directory where you stored the three files. You should see two new directories. The build directory is just tempfiles used by setup, and can be deleted. The dist directory contains the grail, Helloworld.exe, plus a couple other files you shouldn't need (py2exe isn't perfect, yet).

Open dist, double-click on HelloWorld.exe, and you're in business. Copy HelloWorld.exe to another computer (without Python on it), and run it there. Voila.

A few notes:

py2exe just converted your 4k .py file into a 3.23MB .exe file, because it bundled a python interpreter, any required DLLs (few or none, in this case), and any referenced libraries (same) into this file, and it'll do so for every .exe you create.

The specific options I supplied in setup.py and setup.bat are there to cause py2exe to create a single file executable. By default, it creates multiple files in the dist directory, all of which must be copied together to another machine to ensure it will execute. There are reasons why this might be desirable, but they're beyond my reach to discuss at this point.

Visit www.py2exe.org to explore additional options.

Cheers,

- Mark
Dec 17 '06 #1
Share this Article
Share on Google+
4 Comments


bartonc
Expert 5K+
P: 6,596
This is so good, I had to get it back out into the open.
Dec 22 '06 #2

true911m
P: 92
This is so good, I had to get it back out into the open.
I'm already finding deficiencies in both of these 'compiler' tools.

One app that I'm intimately familiar with is a budget calculator I've rewritten a few times as I've tried new languages. (Remember when I was hunting for print functionality?)

The version I settled on "for now" uses reportlab to preview a PDF, which is then a one-button print if desired. Neither of these tools (py2exe or PyInstaller) will pick up and redistribute the reportlab dependencies correctly, and one of them won't even run the main body of the program -- produces a DrWatson or similar pop-up box - 'Sorry for the inconvenience.' :)

So I was all jazzed to write a front-end for PyInstaller (as a single .exe, of course), but when these problems started it dampened my motivation a bit.
Dec 22 '06 #3

bartonc
Expert 5K+
P: 6,596
I'm already finding deficiencies in both of these 'compiler' tools.

One app that I'm intimately familiar with is a budget calculator I've rewritten a few times as I've tried new languages. (Remember when I was hunting for print functionality?)

The version I settled on "for now" uses reportlab to preview a PDF, which is then a one-button print if desired. Neither of these tools (py2exe or PyInstaller) will pick up and redistribute the reportlab dependencies correctly, and one of them won't even run the main body of the program -- produces a DrWatson or similar pop-up box - 'Sorry for the inconvenience.' :)

So I was all jazzed to write a front-end for PyInstaller (as a single .exe, of course), but when these problems started it dampened my motivation a bit.
I use a lot of third party packages. numpy and scipy gave particular amount of trouble. After working out those dependancies, I, too, get an app has crashed message. It's quite annoying, but I'll keep working on it.
Jan 30 '07 #4

bartonc
Expert 5K+
P: 6,596
I use a lot of third party packages. numpy and scipy gave particular amount of trouble. After working out those dependancies, I, too, get an app has crashed message. It's quite annoying, but I'll keep working on it.
I'm getting the kinks worked out. I've adapted a really cool script which does everything form excluding Tkinter modules and calling py2exe by appending the arguments to sys.argv (if the module is run with no arguments). I've posted the entire script here in the Python Code Forum.
Mar 12 '07 #5