473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,320 developers and data experts.

Simple guide to using py2exe

true911m
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
4 34714
bartonc
6,596 Expert 4TB
This is so good, I had to get it back out into the open.
Dec 22 '06 #2
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
6,596 Expert 4TB
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
6,596 Expert 4TB
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

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: RJS | last post by:
Hi all, I can't get a py2exe compiled app to run with numarray (numarray-0.5.win32- py2.2). Also wxPythonWIN32-2.3.3.1-Py22 and ActivePython-2.2.1-222. In the sample below, commenting out...
5
by: Giles Brown | last post by:
I'm feeling quite dumb this morning. I'm trying to build a COM server DLL using py2exe and it ain't working. Here's what ain't working... setup_dll.py based on py2exe sample: """from...
6
by: Luc Saffre | last post by:
Hello, I had a strange problem when freezing (using either py2exe or McMillan installer) a script that imports reportlab (which imports PIL (which imports FixTk))). - Python 2.3.3c (also with...
5
by: Brian Hlubocky | last post by:
I'm have a fairly simple (in terms of COM) python program that pulls info from an Access database and creates Outlook contacts from that information. It uses wxPython for gui and works without...
11
by: Grant Edwards | last post by:
I'm trying in vain to set the icon for the executable generated by py2exe. According to various sources there are two answers: 1) Do it on the command line: python setup.py py2exe --icon...
15
by: James Stroud | last post by:
Hello, My department has switched from vmware to wine/cxoffice. I have been playing with this all morning, and I've gotten this far. If someone has done this, could you point me in the right...
27
by: Julien Fiore | last post by:
Do you wand to install Pyrex on Windows ? Here is a step-by-step guide explaining: A) how to install Pyrex on Windows XP. B) how to compile a Pyrex module. Julien Fiore, U. of Geneva
10
true911m
by: true911m | last post by:
This is a simple walkthrough to get PyInstaller up and running. I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and...
3
by: vajratkarviraj | last post by:
i hav python2.5, matplotlib0.90.1, and py2exe for python 2.5 all on windows xp... i hav a python program(letsc.py) which uses the matplotlib package... and i want 2 make an exe of it for distribution...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.