LOGIX4U.NET

...

  • Increase font size
  • Default font size
  • Decrease font size

How to create Windows executable (.exe) from Python script

E-mail Print PDF

Python is a simple and powerful language for scripting and even application development. Various GUI packages available for Python makes it suitable for developing full fledged applications in python. Ok that is fine, but ever thought of creating an executable file from the python script you wrote? This seems to be a nice idea, there are many reasons why! You can redistribute your application without python. The end user needn't to install python on his machine. You can make your application closed source (unfortunate) etc... Read on this article to find how you can create win32 executables from your Python script.

Introduction

Python is a simple and powerful language for scripting and even application development. Various GUI packages available for Python makes it suitable for developing full fledged applications in python. Ok that is fine, but ever thought of creating an executable file from the python script you wrote? This seems to be a nice idea, there are many reasons why!. You can redistribute your application without python. The end user needn't to install python on his machine. You can make your application closed source (unfortunate) etc... Read on this article to find how you can create win32 executables from your Python script.

This tutorial will give step by step instruction on how to create Win32 executable from Python script. Make sure that the following are installed on your system.


A console application

The following code prints a header and numbers 1 to 10 to console output.

 

test.py
print "Python script to exe test program"


count = 0

while count < 10:
print "count = " + str(count) +"\n"
count = count + 1

 

Save this code in to test.py (or any name with .py extension) file. Make sure that the code works fine by running it using python. To do this, on command prompt type "python test.py". You should see the output as on console as shown bellow.

 

 

Our Python script is ready. Now we need to create a setup script. The setup script is nothing but another Python script where we import py2exe package and setup script from distutils package. Besides this we specify in this file which script is to be used as the entry point for the created executable. Create a new file with name setup.py and paste the following code in to it.

 

setup.py

 

from distutils.core import setup
import py2exe

setup(console=['test.py'])

 

The code is simple and straight forward. Importing setup and py2exe to setup.py and call setup()  function with the name of entry point script as argument. Now it is time to run the script and create the executable. To build the executable, run "python setup.py py2exe" on the command prompt. You can see lots of output on the console. In the end you can see the output as in the picture below.

 

 

Building the executable is finished. Now you can find test.exe in the \dist sub folder. Move to dist sub folder and run test.exe, you can see output in console as shown below.

 

output3.jpg

 

 

A GUI Application

We have successfully created a console application executable from a Python script. Let's now try to create a GUI application executable from Python script. To create GUI with Python, we will use Tkinter (Tk Interface) as the GUI toolkit. Creating executable from a Tkinter-Python GUI script is straight forward. By just following the same steps we did for console application, we can create GUI executable.

Create a new file paste the following code and save it with name " gui.py".

gui.py

 

from Tkinter import *

frmMain = Tk()

label = Label(frmMain, text="Welcome to py2exe!")
label.pack()

frmMain.mainloop()

 

Again the code is straight forward, we are importing Tkinter tool kit package, creating the main window, creating a label resizing it to the size of contents and entering to the application main loop. try to run this script bye entering the command "python gui.py" on command line. If everything is fine, you should see a window as shown below.

 

gui1.jpg

 

So far so good.. Now let us see how we can build windows executable from our script. Create a new file with name setup.py and paste the following code in to it.

 

setup.py

 

from distutils.core import setup
import py2exe

setup(console=['gui.py'])

 

 

To build the executable, run "python setup.py py2exe" on the command prompt. Once building process is finished, move to \dist subfolder and run the executable by entering "gui.exe" on command prompt. Now you should see a window same as that of created from Python script yet it is an executable.

Downloads

Console executable source code

GUI  executable source code