Tag Archives: cx freeze

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.

This is the setup.py file.


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.