DISCOVER
×

How to Make Python Script Executable

Updated February 21, 2017

One reason for Python's increased popularity as a high-level programming language is its portability. Scripts written on a Mac, for example, will run on a Windows machine and vice versa. As long as the destination machine has Python installed on it, the script will run without problems. Most Python scripts are saved as scripts; they are not self-contained, and they are normally executed by typing "python xxx.py" in a command prompt. However, this method of running the scripts requires a Python installation. It is possible to make a Python script executable -- with no Python installation required -- by turning it into a "frozen binary."

Download and install a frozen binary system. Three systems are commonly used. Py2exe (for Windows) is available at www.py2exe.org. Py2app (a Mac version of Py2exe) is available at svn.pythonmac.org/py2app. PyInstaller (for Windows, Mac and Linux) is available at www.pyinstaller.org.

To install py2exe, download the .ZIP file and unpack it. No installation is required.

To install py2app, download the .TAR.GZ file from Python.org and unpack it. Open a terminal and navigate to the directory in which you unpacked the files. Type:

python setup.py install

and the program will install itself.

To install PyInstaller, download the .ZIP file from the main page and unpack it. Open a terminal or command line and navigate to the unpacked contents directory. Type

python Configure.py

and the program will install and configure itself.

Write your Python script that you wish to save as an executable. For example:

print "Hello, world!"

can be saved as hello.py.

Use Py2exe or Py2app to create an executable. Because Py2app is the Mac version of Py2exe, the sequences to use them are similar. Both require a setup.py script. In Py2exe:

from distutils.core import set-up

import py2exe

set-up(console=['hello.py'])

will import the setup function from distutils, import the py2exe module, and then setup the console application hello.py.

In Py2app, you can use the Py2applet script to create the setup.py:

py2applet --make-setup hello.py

This creates setup.py.

Run your set-up script:

Python setup.py Py2exe

This will create the hello.exe file, which is a self-contained executable.

For Py2app, type

python setup.py py2app -A

This will create the hello.app file, which is a self-contained app file that can be run from the terminal.

Create a spec file in PyInstaller by navigating to your script folder and typing

python Makespec.py [--onefile] hello.py

Build your project with:

python Build.py specfile

This creates a standalone .EXE file.

bibliography-icon icon for annotation tool Cite this Article

About the Author

Wolfram Donat is an information technology professional and writer currently finishing a degree in computer systems engineering. He has had success writing for both on- and offline audiences, and moonlights as a professional copywriter as studies permit. He is currently shopping his first novel to agents and publishers, and is hard at work on his second.