473,897 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple guide to using py2exe

true911m
92 New Member
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 34965
bartonc
6,596 Recognized Expert Expert
This is so good, I had to get it back out into the open.
Dec 22 '06 #2
true911m
92 New Member
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 Recognized Expert Expert
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 Recognized Expert Expert
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
2998
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 "import numarray" allows the exe to run. Left in, I get "4.exe has generated errors" etc. I'm going around and around and there isn't much on Google. py2exe output is last.
5
9118
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 distutils.core import setup import py2exe
6
3959
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 Python 2.3) - PIL 1.1.4 - Installer or py2exe : latest versions.
5
3993
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 problems. I want to be able to distribute this program using py2exe, but I am having some problems. I am using a simple setup.py like in the documentation for the py2exe tool and everything compiles ok, except that when I run the exe, I get an...
11
15816
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 foo.ico That generates a usage error: error: --icon not recognized
15
3237
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 direction euler 65% winpy Enthought Edition build 1057 Python 2.3.3 (#51, Feb 16 2004, 04:07:52) on win32 Type "help", "copyright", "credits" or "license" for more information. py> from distutils.core import setup
27
3997
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
110386
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 it also integrates UPX (Ultimate Packer for eXecutables) into the build process, if you have it installed. It also claims functionality on linux, as a bonus (I didn't test this). I highly recommend the UPX options, and will cover how to get it...
3
4734
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 on other comps... i used py2exe... i wrote a setup.py whose contents are : from distutils.core import setup import py2exe import matplotlib setup(console=, options={ 'py2exe': { 'packages' : ,
0
11250
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10850
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10478
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8032
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7185
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5873
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3300
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.