Python || How To Create An Executable Using Cx_Freeze

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.3 or higher.

=== 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.3 or higher (including Python 3), since it makes use of the zip import facility which was introduced in 2.3.

Note: Binary packages must be compatible with the Python installation on your system or you will get errors in your frozen executables about missing builtin modules or missing symbols. This is not likely to be a problem on Windows (since most installations are from python.org) but on Linux it is more likely (since Linux distributions often package Python in different ways). If you get such errors, download the source package and build the binaries yourself.

=== 2. INSTALLING CX_FREEZE ===

First, download the cx_Freeze installer which corresponds to the python version that is installed on your system. I have Python 3.2 installed on my Windows computer, so I will select the cx_Freeze version 3.2 installer to download.

Next, follow the command prompts from the installer and wait!

When the cx_Freeze installer is completed, you should see the newly installed files which correspond to this module located in the following directory:


C:\Python32\Lib\site-packages\cx_Freeze

If everything installed correctly, you should now be ready to create executable Python programs!

=== 3. CREATE THE SETUP SCRIPT ===

To use cx_Freeze you 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 file.

=== 4. BEGIN COMPILATION ===

After you have created the setup.py file and inserted the data that corresponds to your specific python script within the setup file, copy and paste the setup.py file and ALL of the python files associated with your current python program into the following directory:


C:\Python32

Here is a sample screenshot demonstrating the above instructions: (click to enlarge)

In order to start compiling the hello.py file, we need to open the command prompt (CMD). Click here if you dont know how to do this.

Next, copy and paste these commands into the command prompt (CMD).

#1. Change directories into the python folder


cd C:\Python32

#2. Start compiling the hello.py program using the setup.py file


python.exe setup.py build

If cx_Freeze finished with no errors, then the process should be complete!

The python executable should be located in the following directory:


C:\Python32\build\exe.win32-3.2

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 was 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 executable: (click to enlarge)

One Response to Python || How To Create An Executable Using Cx_Freeze

  1. pywriter says:

    thank you,sir
    i searched all over the web but could not get a reliable answer.again thank you very very much.

Leave a Reply