Python || How To Create An Executable Using Cx_Freeze

Print Friendly, PDF & Email

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.

Was this article helpful?
👍 YesNo

17 Responses to Python || How To Create An Executable Using Cx_Freeze

  1. Avatar 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. Avatar kaniska says:

    You are great……

  3. Avatar VIRkid says:

    THANK you sir Learned alot

  4. Avatar vimal says:

    thanks..!!

  5. Avatar 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.

      • Avatar 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. Avatar Justik says:

    Thank you, Helps a lot.

  7. Avatar 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. Avatar cobus says:

    thank you so muck

  9. Avatar sundeep says:

    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?

  10. great help after a lot of struggle. thanks a lot

  11. Avatar presea nagoya says:

    Hello,

    I received the followwing error:

    TypeError: __init__() got an unexpected keyword argumente ‘copyDependentFiles’

    Could you please tell mme the possible solutions,

    thanks

Leave a Reply to cobus Cancel reply