Connecting Tech Pros Worldwide Help | Site Map

Simple guide to using py2exe

  #1  
Old December 17th, 2006, 10:05 PM
true911m's Avatar
Member
 
Join Date: Dec 2006
Location: Baltimore, MD
Posts: 95
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



  #2  
Old December 22nd, 2006, 07:50 AM
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400

re: Simple guide to using py2exe


This is so good, I had to get it back out into the open.
  #3  
Old December 22nd, 2006, 02:21 PM
true911m's Avatar
Member
 
Join Date: Dec 2006
Location: Baltimore, MD
Posts: 95

re: Simple guide to using py2exe


Quote:
Originally Posted by bartonc
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.
  #4  
Old January 30th, 2007, 11:15 AM
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400

re: Simple guide to using py2exe


Quote:
Originally Posted by true911m
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.
  #5  
Old March 12th, 2007, 09:59 AM
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400

re: Simple guide to using py2exe


Quote:
Originally Posted by bartonc
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.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple guide to using PyInstaller true911m insights 8 June 17th, 2009 10:47 PM
problem with py2exe and microsoft speech SDK 5.1 Dave Lim answers 1 May 6th, 2007 12:15 AM
problem with py2exe and microsoft speech SDK 5.1 Dave Lim answers 1 May 3rd, 2007 07:55 PM
WooHoo! py2exe Works! true911m answers 13 January 30th, 2007 05:02 AM