473,394 Members | 1,817 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,394 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 34748
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.