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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Create Python Executables Automatically : Page 2

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.




Application Security Testing: An Integral Part of DevOps

You need to extract the file name (minus the extension) from the file path that the user has specified. The code below assigns that file name to variable package, which you need to create the setup file. For example, if the specified file path is python/somePyFile.py, then at the end of the code fragment, package will hold the value somePyFile.

package = re.split(":",fileName) package = re.split("/",package[len(package) - 1]) package = re.split(".py",package[len(package) - 1]) package = package[0]

The next task is to write a setup file containing the commands required to create the executable. The code asks the user for the name of the setup file. If the user simply presses the Enter key, the setup file name defaults to setup.py.

def getSetupName(): global setupName setupName = raw_input("Enter name of setup file (or <enter> for default): ") if(setupName == ''): setupName = "setup.py" try: fp = open(setupName) fp.close() flag = raw_input("Setup file exists! Rewrite (0=no; else <enter>)? ") if(flag == "1"): getSetupName() except IOError: setupName = setupName getSetupName()

The following code writes that information into the setup file. If the file does not exist, the code creates it. If the file exists, the code asks the user to confirm that it should overwrite the file.

fp = open(setupName,"w") temp = """from distutils.core import setup import py2exe setup(name = "%s", scripts = ["%s"], )""" % (package,fileName)

For example, if a user enters the file path python/fader.py, the variable package gets set to fader, and then the setup.py file will contain the following text:

from distutils.core import setup import py2exe setup(name="fader", scripts=["python/fader.py"], )

Note that the above data remains in the file only during this stage.

You could run the setup.py file manually, but it's much more convenient to automate that as well. To do that, you need to be able to add py2exe to the command-line for the setup.py file, and then launch it.

You can manipulate the command-line via the Python globals() dictionary, which contains information about a number of objects, among which is the sys object. The sys object exposes the argv list, which contains the list of command-line arguments. Using the sys.argv list, you can manipulate (and modify) command-line arguments. The following line appends py2exe to the command-line arguments list; that command-line argument is required to successfully create an executable.


To create the executable, you dynamically execute the file setup.py created earlier using Python's execfile() method.


The execfile(fileName) method basically performs the following steps:

fp = open(fileName,'r') exec f f.close()

Calling the execfile() method is equivalent to the preceding three sequential steps; in other words, executing setup.py programmatically is equivalent to running the file setup.py from the command-line with a command-line argument of py2exe, for example:

D:\python22>python setup.py py2exe

Finally, the last lines in Listing 1 clear the file setup.py. Thus, after the script executes, setup.py becomes a blank file, ready for the next cycle.

When you run py2exe it creates the output executable in the /dist/<package> directory.

Save Yourself Some Time
Without using makeExe.py, assuming you already have a setup file template ready, you will first need to edit the file, add the required information, and then save it. Next, you must go to the command line and then type the string to create the executable. But using makeExe.py, you just need to double-click on it and then enter the file path for the Python file from which you want to make an executable, and it creates the executable and saves it for you. Without any doubt, the latter task is less far irksome and time-consuming, particularly when you need to create a number of Python executables.

Future Work
This article explains the underlying technique involved in automating the creation of Python executables. Enhancements in the form of a graphical user interface, using Tk or wxPython, would be desirable, as would batch creation of executables.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



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