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


Create Python Executables Automatically : Page 3

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.

A Windows Batch File to Automate the creation of Python Executables
As you've seen, the process of creating a Python executable is simple enough, but it could be simpler. If you use py2exe directly, every time you want to create an executable you must first create a new setup file or edit an existing setup file. The py2exe process simplifies things somewhat, but you can make the process even simpler.

You can integrate the setup steps using a simple DOS Batch file. The makeExe.bat file is a simple DOS batch file. The batch file makes use of two other files that I call configuration files. These files are simply called 1.txt and 2.txt. You place them in a directory named config. Note that all these files must be present in your Python root directory (the directory where your main Python binary exists), typically a folder such as C:\Python22, which is the folder that the following discussion assume is your directory name. Figure 2 shows how your directory structure should look:

Figure 2. Directory Structure: The figure shows the directory structure you need to set up for the makeExe.bat file to work properly.
If you look at the two text files, you'll see they couldn't be simpler:


   set fileName=


   set dirName=

You'll see how these files are used later. Here's the content of the makeExe.bat batch file.

   0 rem Batch file to automate creation of Python 
   1 rem ...using py2exe 
   2 rem by Premshree Pillai (http://www.qiksearch.com/)
   3 @echo off
   4 echo (After each input press return, ^z and return again)
   5 echo Enter the file name whose executable you want to 
   6 echo (...use relative or absolute path)
   7 copy config\1.txt + con config\temp1.bat
   8 echo Enter the setup name to be created:
   9 echo (...should be same as file name, without the 
      extension of course!)
   10 copy config\2.txt + con config\temp2.bat
   11 call config\temp1.bat
   12 del config\temp1.bat
   13 call config\temp2.bat
   14 del config\temp2.bat
   15 echo from distutils.core import setup > config\set.py
   16 echo import py2exe >> config\set.py
   17 echo setup(name="%dirName%", >> config\set.py
   18 echo      scripts=["%fileName%"], >> config\set.py
   19 echo ) >> config\set.py
   20 python config\set.py py2exe -w
   21 del config\set.py
   22 echo The executable has been created in \dist\%dirName%

Author's Note: The line numbers in the preceding listing exist for explanation only and are not part of the code.

How MakeExe.bat Works
The following explanation walks through the lines of the makeExe.bat file, giving a bit of explanation about each one.

  • It uses the COPY CON (copy from the keyboard console) command to get user input.
  • Lines 5-6: When you run the makeExe.bat file, it prompts you for the name of the Python script whose executable you want to create. You may enter the file name using a relative or an absolute path. Now press Enter, [Ctrl z] and then press Enter again.
  • Line 7: The batch file appends the name you entered to the 1.txt file and saves that as a new file named temp1.bat. For example, if you entered myScripts/myFile.py, the temp1.bat file would look like this:
   set fileName=myScripts/myFile.py

  • Lines 8-9: Next, the file prompts you to enter the setup file. Enter the name of your Python script, but without the .py extension. For example, if your file name is urn.py, just enter urn. Now press Enter, [Ctrl z] and then press Enter again.
  • Line 10: The batch file appends whatever you entered to the 2.txt file and writes the result to a new file called temp2.bat. For example, if you entered myFile (which would be the case if your file name is myFile.py), the temp2.bat file would look like this:
   set dirName=myFile

  • Lines 11-14: The batch process calls each of the newly created batch files, (temp1.bat and temp2.bat) using the call command. This sets the two environment variables %fileName% and %dirName%, after which it deletes the two batch files using the del command.
  • Now, the main batch process uses the file and directory names that the user entered by making use of the %fileName% and %dirName% variables respectively.
  • Lines 15-19: These lines create the temporary setup file, set.py, using the user-entered values.
  • Line 20: This line runs the newly created setup file, set.py using Python. Note that the -w switch is used to create Windows applications without the DOS window popping up.
  • Line 21: This line deletes the set.py file.
Note that the batch process creates all temporary files in the /config directory, and that py2exe creates the executable in the /dist/ directory (see Figure 3).

Figure 3. MakeExe.bat In Action: The figure shows an interactive session of the batch file with sample user input.
I hope this simple batch file makes things a wee bit easier. If you have suggestions for improvements, feel free to email me with them.

Premshree Pillai studies engineering in Information Technology at Mumbai University, Mumbai, India. He is a programming enthusiast and maintains a Web site, where he posts his scripts. He is also a freelance writer and has written for a range of popular Indian magazines.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date