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.
- First create a Python script.
- Create a setup file that includes the name of your setup and also the name of the file whose executable you want to create.
- Run your Python script from the command line, for example, python yourFile.py py2exe.
- 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.
![]() |
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.
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.
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.
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:
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:
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:
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/ Save Yourself Some Time Future Work A Windows Batch File to Automate the creation of Python Executables 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:
If you look at the two text files, you’ll see they couldn’t be simpler: config/1.txt
config/2.txt
You’ll see how these files are used later. Here’s the content of the makeExe.bat batch file.
How MakeExe.bat Works
Note that the batch process creates all temporary files in the /config directory, and that py2exe creates the executable in the /dist/
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. Related ArticlesCharlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience. Related PostsAbout Our Editorial ProcessAt DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere. See our full editorial policy. ![]() Your Website Starts With a Name: What You Need to Know Before You Buy a Domain
Marcus Whitfield
September 28, 2026
1:24 PM
![]() What Is Microsoft Copilot Autopilot? Home, Code, and Agent Billing Explained
Noah Nguyen
September 28, 2026
11:38 AM
![]() Evvy Raises $40 Million for Women’s Health
Sumit Kumar
September 25, 2026
4:57 PM
![]() Prince Harry Warns of AI Chatbot Risks
Deanna Ritchie
September 25, 2026
4:33 PM
![]() Amazon Adds AI Agents for Sellers
Sumit Kumar
September 25, 2026
4:19 PM
![]() Claude Opus 5.5: Lower Pricing and Higher Coding Benchmarks Explained
Johannah Lopez
September 25, 2026
3:55 PM
![]() US Housing Market Tilts Toward Buyers
Steve Gickling
September 25, 2026
3:25 PM
![]() Virtual Character Sidesteps Politics in Movie Promotion
Steve Gickling
September 25, 2026
2:09 PM
![]() Google Safety Founder Urges AI Slowdown
Kirstie Sands
September 25, 2026
10:23 AM
![]() C2C Links AI Models Through KV Caches
Rashan Dixon
September 25, 2026
9:29 AM
![]() The SaaS Metrics That Actually Predict Growth
Rashan Dixon
September 25, 2026
8:00 AM
![]() Researchers Claim Hacks of Meta and Slack
Deanna Ritchie
September 24, 2026
5:04 PM
![]() More Americans Turn to Chatbots for Support
Sumit Kumar
September 24, 2026
4:38 PM
![]() AI Leaders Clash Over Development Pace
Rashan Dixon
September 24, 2026
4:31 PM
![]() What Is Karmada? The Kubernetes Multi-Cluster Tool Explained
Priya Nandakumar
September 24, 2026
1:32 PM
![]() Apple Faces Leadership Test Under John Ternus
Rashan Dixon
September 24, 2026
12:48 PM
![]() Senator Urges Independent Tests for Frontier AI
Kirstie Sands
September 24, 2026
9:39 AM
![]() Silent Data Change Skews Audience Counts
Steve Gickling
September 24, 2026
9:15 AM
![]() Revenant Ventures Closes $78.5 Million Debut Fund
Sumit Kumar
September 24, 2026
9:11 AM
![]() How to Become a Software Engineer Without a CS Degree
Rashan Dixon
September 24, 2026
8:00 AM
Opus 5.5 Won the Latest AI Contest
Joe Rothwell
September 24, 2026
7:51 AM
![]() Enterprises Struggle to Control AI Agent Costs
Steve Gickling
September 23, 2026
4:35 PM
![]() Ohio Data Center Boom Pressures Residents
Sumit Kumar
September 23, 2026
3:29 PM
![]() Venture Gains Lift San Francisco Pension Returns
Sumit Kumar
September 23, 2026
1:43 PM
![]() Walter Torous to Lead MIT Real Estate Center
Deanna Ritchie
September 23, 2026
12:41 PM
|


























