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
Average Rating: 3.7/5 | Rate this item | 3 users have rated this item.
 Print Print
 
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()

Page 1 of 3
advertisement
  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.) 
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES