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
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
setupName = raw_input("Enter name of setup file (or <enter> for default): ")
if(setupName == ''):
setupName = "setup.py"
fp = open(setupName)
flag = raw_input("Setup file exists! Rewrite (0=no; else <enter>)? ")
if(flag == "1"):
setupName = setupName
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
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
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 basically performs the following steps:
fp = open(fileName,'r')
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>
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.
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.