You are on page 1of 3

Related Searches

1. Python Training Videos

2. Python Example Programs

4. Python Programming Tutorial

5. Python development

py2exe: Python to exe Introduction


py2exe is a simple way to convert Python scripts into Windows .exe applications. It is an
utility based in Distutils that allows you to run applications written in Python on a Windows
computer without requiring the user to install Python. It is an excellent option when you
need to distribute a program to the end user as a standalone application. py2exe currently
only works in Python 2.x.
First you need to download and install py2exe from the official sourceforge site
Now, in order to be able to create the executable we need to create a file called setup.pyin
the same folder where the script you want to be executable is located:
1
2
3
4

# setup.py
from distutils.core import setup
import py2exe
setup(console=['myscript.py'])

In the code above, we are going to create an executable for myscript.py. The setup
function receives a parameter console=['myscript.py'] telling py2exe that we have a
console application called myscript.py.
Then in order to create the executable just run python setup.py py2exefrom the Windows
command prompt (cmd). You will see a lot of output and then two folders will be created:
dist and build. The build folder is used by py2exe as a temporary folder to create the

files needed for the executable. The dist folder stores the executable and all of the files
needed in order to run that executable. It is safe to delete the build folder. Note: Running
python setup.py py2exe assumes that you have Python in your path environment

variable. If that is not the case just use C:\Python27\python.exe setup.py py2exe.
Now test if your executable works:
1 cd dist
2 myscript.exe

GUI Applications
Now it is time to create a GUI application. In this example we will be using Tkinter:
1
2
3
4

# tkexample.py
'''A very basic Tkinter example. '''
import Tkinter
from Tkinter import *

5
6
7
8

root = Tk()
root.title('A Tk Application')
Label(text='I am a label').pack(pady=15)
root.mainloop()

Then create setup.py, we can use the following code:


1
2
3
4
5

# setup.py
from distutils.core import setup
import py2exe

setup(windows=['tkexample.py'])

The setup function now is receiving a parameter windows=['tkexample.py'] telling


py2exe

that

this

is

GUI

application.

Again

create

the

executable

running

python setup.py py2exe in the Windows command prompt. To run the application just

navigate to the distfolder in the Windows Explorer and double-click tkexample.exe.

Using External Modules


The previous examples were importing modules from the Python Standard Library. py2exe
includes the Standard Library Modules by default. However if we installed a third party
library, py2exe is likely not to include it. In most of the cases we need to explicitly include
it. An example of this is an application using the ReportLablibrary to make PDF files:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# invoice.py
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import mm

if __name__ == '__main__':
name = u'Mr. John Doe'
city = 'Pereira'
address = 'ELM Street'
phone = '555-7241'
c = canvas.Canvas(filename='invoice.pdf', pagesize= (letter[0], letter[1]/2))
c.setFont('Helvetica', 10)
# Print Customer Data
c.drawString(107*mm, 120*mm, name)
c.drawString(107*mm, 111*mm, city)
c.drawString(107*mm, 106*mm, address)
c.drawString(107*mm, 101*mm, phone)
c.showPage()
c.save()

In order to include the ReportLab module, we create a setup.py file, passing a options
dictionary to the setupfunction:
1
2
3
4
5
6
7
8
9
10

# setup.py
from distutils.core import setup
import py2exe

setup(
console=['invoice.py'],
options = {
'py2exe': {
'packages': ['reportlab']
}

11 }
12 )

The final step to be able to run the executable on other computers, is that the computer
running the executable needs to have the Microsoft Visual C++ 2008 Redistributable
package installed. A good guide explaining how to do this can be found here. Then just
copy the distfolder to the other computer and execute the .exe file.
Finally please take into account the following recommendations:
The executable created most of the time is forward compatible: If you create the
executable in Windows XP, it will run in Vista and 7. However it is not backwardscompatible: if you create the executable in Windows 7 it is not going to run on
Windows XP.
If you have imported third party libraries, make sure to test all of the applications
functionality before you ship the software, because sometimes the executable is
created, but some libraries are missing. In that case you will get a runtime error when
you try to access a functionality that uses the external library.
py2exe hasn't been updated since 2008, so it is not going to work with Python 3.

Related Searches

If you need more information about py2exe, visit the official site.
1. Python Training Videos
2. Python Example Programs
3. Python GUI
4. Python Programming Tutorial
5. Python development

environment

6. Ask a Python Programmer


ads by Yahoo!