Categories

Convert .py files to .exe using cx_Freeze

cx_Freeze is used for converting .py files(or programs) to .exe files(or executables). It is similar to py2exe and pyinstaller, the good thing about this module is that it is cross platform and it works on Python2.4 and higher versions. It even works in Python3.


Based on the cx_Freeze documenation, there are three ways to use cx_Freeze. This guide is about using cx_Freeze by creating disutils setup script.

To use cx_Freeze by creating disutils setup script, follow the procedure below(detailed documentation is located here):

1. Create a script
    Below is a sample script taken from cx_Freeze documentation. Minor change was done to change the icon of the .exe file.

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "code",
        version = "0.1",
        description = "the python code",
        options = {"build_exe": build_exe_options},
        executables = [Executable("code.py", icon="icon.ico", base=base)])

2. Save the script as setup.py

3. Move the python code(ex. code.py) and the script(setup.py) inside the Python folder. (ex. C:\Python26)

4. Open command prompt and change the directory to the python directory. (Ex. cd C:\Python26)

5. Enter the command: python setup.py build

A new folder named build will be created inside the Python directory. The folder containing the executable file will also be created inside the build folder.

5 comments:

  1. I have checked and know i have typed the code here exactly. When i start building the exe file it gives the error return before exception set. I dont know why

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete