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)

14 Responses 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.

  2. kaniska says:

    You are great……

  3. VIRkid says:

    THANK you sir Learned alot

  4. vimal says:

    thanks..!!

  5. Maxxie says:

    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.

    • admin admin says:

      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.

      • Maxxie says:

        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 )

        • admin admin says:

          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!

  6. Justik says:

    Thank you, Helps a lot.

  7. Tanjid says:

    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

    • admin admin says:

      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!

  8. cobus says:

    thank you so muck

Leave a Reply