Python || How To Create An Executable Using Cx_Freeze
Python is a simple and powerful programming language for scripting and application development. Various GUI packages available for Python makes it a great choice for developing applications. But what if you want to create an executable file from the Python script you just created? This page demonstrates that process using a free third party module called “cx_Freeze.” This module is ideal for use because it supports Python 2.7 or higher, including Python 3.
Contents
1. Introduction
2. Installing cx_freeze
3. Verify Installation
4. Create The Setup Script
5. Begin Compilation
6. Output Directory
1. Introduction
cx_Freeze is a set of scripts and modules for freezing Python scripts into executables, in much the same way that py2exe and py2app do. Unlike these two tools, cx_Freeze is cross platform and should work on any platform that Python itself works on. It supports Python 2.7 or higher, including Python 3.
2. Installing cx_freeze
In the terminal (cmd), install by running the following command:
pip install cx-Freeze
– OR –
python -m pip install cx-Freeze
If this process completes with no errors, skip to the next step!
Note: If using Windows and you get an error that says 'python' is not recognized as an internal or external command
, a simple solution is to navigate in the terminal (cmd) to the directory of your Python installation folder, and then follow the steps below.
The default Python installation directory is the following:
C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Your-Python-Version\
Once you have navigated to the Python folder for the version you are using, in the terminal (cmd), run the following command.
python.exe -m pip install cx-Freeze
Tip: If you want to run the Python command from the terminal without having to navigate to the installation folder, it is recommended that you add your Python directory to the Windows Path. Click here for information on how this is done.
3. Verify Installation
Once the cx_Freeze installation is complete, you should see the newly installed files for this module located in the following directory (Windows):
C:\Path-To-Your-Python-Version\Lib\site-packages\cx_Freeze
If everything installed correctly, you should now be ready to create executable Python programs!
4. Create The Setup Script
To use cx_Freeze we need to create a setup.py file which tells the cx_Freeze “compiler” what you want to do. Here’s a sample setup.py file whose simplicity is appropriate for our sample “Hello World” program.
This is the sample Python file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# ============================================================================= # Author: Kenneth Perkins # Date: Jul 25, 2013 # Taken From: http://programmingnotes.org/ # File: hello.py # Description: Simple hello world program # ============================================================================= def main(): print("Hello World, created using Python") input("\nPlease press ENTER to continue...") if __name__ == "__main__": main() # http://programmingnotes.org/ |
This is the setup.py file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# ============================================================================= # Author: Kenneth Perkins # Date: Jul 25, 2013 # Updated: Feb 12, 2021 # Taken From: http://programmingnotes.org/ # File: setup.py # Description: This is the cx_Freeze setup file for creating an exe program # ============================================================================= from cx_Freeze import setup, Executable # NOTE: you can include any other necessary external imports here aswell includefiles = [] # include any files here that you wish excludes = [] packages = [] exe = Executable( # what to build script = "hello.py", # the name of your main python script goes here init_script = None, base = None, # if creating a GUI instead of a console app, type "Win32GUI" target_name = "Hello World.exe", # this is the name of the executable file icon = None # if you want to use an icon file, specify the file name here ) setup( # the actual setup & the definition of other misc. info name = "Hello World", # program name version = "0.1", description = 'A general enhancement utility', author = "Kenneth Perkins", author_email = "admin@programmingnotes.org", options = {"build_exe": {"excludes":excludes,"packages":packages, "include_files":includefiles}}, executables = [exe] ) # http://programmingnotes.org/ |
5. Begin Compilation
In order to start compiling the hello.py file, we need to open the terminal (cmd).
Next, once you have created the setup.py file, save the setup.py file into the same directory as your project, and run the following command in the terminal (cmd) from your project directory:
python setup.py build
This command essentially starts compiling the hello.py file.
If this process completes with no errors, skip to the next step!
Note: If using Windows and you get an error that says 'python' is not recognized as an internal or external command
, a simple solution is to copy and paste the setup.py file and ALL of the Python files associated with your project into the directory of your Python installation before running the command.
The default Python installation directory is the following:
C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Your-Python-Version\
Note: I have Python installed in the following directory, so the steps below will use this path.
C:\Python39
Here is a sample screenshot of the instructions after copy and pasting the files into the Python installation directory: (click to enlarge)
Next, copy and paste these commands into the terminal (cmd).
#1. Change directories into your Python installation folder
cd C:\Python39
#2. Start compiling the hello.py program using the setup.py file
python.exe setup.py build
6. Output Directory
If the cx_Freeze build finished with no errors, then the process should be complete!
By default, the Python executable should be located in a folder titled ‘build‘, located in the same directory as where your setup.py resides.
If you followed the steps above to copy and paste your files during the build process, it should be located at the following:
C:\Path-To-Your-Python-Version\build\exe.win-3.9
Here is a sample screenshot demonstrating the directory where the executable file resides: (click to enlarge)
Note: In order for the executable file to run, the other files within that folder that are generated by cx_Freeze must be bundled with the executable file at all times.
And there you have it! Here is a sample screenshot of the running ‘Hello World’ executable: (click to enlarge)
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
thank you,sir
i searched all over the web but could not get a reliable answer.again thank you very very much.
You are great……
THANK you sir Learned alot
thanks..!!
hello sir, i created the exe file and it works fine….but if i try to run the exe by copying it in a folder in the same path an error pops up saying cannot get zipimporter instance…can u assist me how to get rid of it.
Hello
When you move the executable file, make sure you also move all of the files that was generated by cx_freeze too, or else the executable file will not run.
In your case, the executable was looking for the file “library.zip” but could not find it, thus you got the error message stating “cannot get zipimporter instance”
In order for the executable to run, the files generated by cx_freeze must be placed in the same directory as the executable file at all times.
Refer to screenshot #2 on this page for reference.
thnku sir,
in some other sites it was suggested to disable DEP(Data execution prevention) from system properties…does that work to remove this error…
one more thing , will the exe file work on a system which does not have python install cause i need to send it to different people(i have used cx_freeze )
Hello again,
To answer your questions
(1) Unfortunately I dont know of any fix associated with disabling the DEP so I cannot help you there, but my previous reply should probably fix your issue
(2) Assuming you are using cx_freeze with Windows, If the people you send the exe to also has Windows computers, then no they do not need to have Python installed to run your program
Thanks for visiting!
Thank you, Helps a lot.
Hello sir,
I might be making some mistakes but whenever I run my exe file, it closes right away. Is there anything that I’m doing wrong?
Thanks
Hello
Is your exe a console program, or does it have a GUI?
If its a console program, you probably need to place an input statement in the code before the program ends, that way the program will halt unless you press enter.
Look at the example hello world file listed on this page for ideas!
Hello Sir..
May be if you have an email or anything, you can take a look at my code.. I did exactly what you asked me to do but still no solution..
Thank you for the help
Hello again.
You can contact me here and I’ll try to help.
thank you so muck
hello sir,
i used cx_freeze on windows and made an app using python. will i able to run the same .exe file on linux based OS?
great help after a lot of struggle. thanks a lot
Hello,
I received the followwing error:
TypeError: __init__() got an unexpected keyword argumente ‘copyDependentFiles’
Could you please tell mme the possible solutions,
thanks