RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Create Python Executables Automatically

The py2exe project gave Python authors a way to create executable files, but using it can be time-consuming; particularly when you need to create executables repeatedly. Here's how to automate the process.

his article shows you how to automate the task of creating Python executables via py2exe using Python itself. I call the Python file containing the code makeExe.py. This technique requires only one file, makeExe.py; it creates all other necessary files at run-time. First, a short review on the process of creating a Python executable using py2exe.

Creating a Python Executable
Editor's Note: The author recently submitted another method for creating Python executables using a batch file, along with an explanation of how to use it. This new method has been added as the last page of this article, at his request..

The following four steps describe the process required to create a Python executable.

  1. First create a Python script.
  2. Create a setup file that includes the name of your setup and also the name of the file whose executable you want to create.
  3. Run your Python script from the command line, for example, python yourFile.py py2exe.
  4. Creating a new setup file or modifying an existing one each time you need to create an executable is onerous. Wouldn't it be better to have these steps integrated into one? That's exactly what the Python sample code for this article, makeExe.py does.
Author's Note: You can get py2exe here. You'll also find the full instructions for using py2exe via that link.

How the makeExe.py File Works
Listing 1 contains the full contents of the makeExe.py file. When you run this file (you should run it from the Python root directory), it asks the user for a file name, then creates an executable for the specified file (see Figure 1). The short code excerpts that follow explain how the code works.

Thanks for your registration, follow us on our social networks to keep up-to-date