advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
How often do you build Python executables? Did this article meet your needs for doing so? Do you think it will save you time? Why or why not? Would you rather see such a utility with a visual interface or is the command-line version described here sufficient? Let us know in the Open Source discussion forum.
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 3.7/5 | Rate this item | 3 users have rated this item.
 

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. 


advertisement
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.

 
Figure 1. A Simple UI: The makeExe.py file running in a command window requests a file path to the file for which the user wants to create an executable.
First, the code imports the modules sys and re. You need the sys module to manipulate the command-line arguments, and the re module to extract the filename (without the .py extension) from the file path entered by the user.

   import sys
   import re
Next, it asks the user for the file name from which to create the executable. The code accepts relative file paths as well as full file names. The code also ensures that the specified file exists.

   def getFileName():
      global fileName
      fileName = raw_input(
         "Enter file name (rel or abs path, eg., 
         python/file.py): ")
      try:
         fp = open(fileName)
         fp.close()
      except IOError:
         print "File does not exist!"
         getFileName()
   
   getFileName()

  Next Page: How the MakeExe.py File Works (cont.)
Page 1: IntroductionPage 3: A Windows Batch File to Automate the creation of Python Executables
Page 2: How the MakeExe.py File Works (cont.) 
Please rate this item (5=best)
 1  2  3  4  5
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs