# cx_Freeze

## Abstract

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 requires Python 2.3 or higher since it makes use of the zip import facility which was introduced in that version.

## Using cx_Freeze

There are three different ways to use cx_Freeze. The first is to use the included cxfreeze script which works well for simple scripts. The second is to create a distutils setup script which can be used for more complicated configuration or to retain the configuration for future use. The third method involves working directly with the classes and modules used internally by cx_Freeze and should be reserved for complicated scripts or extending or embedding. Each of these methods is described in greater detail below.

There are three different options for producing executables as well. The first option is the only one that was available in earlier versions of cx_Freeze, that is appending the zip file to the executable itself. The second option is creating a private zip file with the same name as the executable but with the extension .zip. The final option is the default which is to create a zip file called library.zip and place all modules in this zip file. The final two options are necessary when creating an RPM since the RPM builder automatically strips executables. These options are described in greater detail below as well.

### cxfreeze script

The cxfreeze script is included with other Python scripts. On Windows and the Mac this is in the Scripts subdirectory of your Python installation whereas on Unix platforms this in the bin directory of the prefix where Python is installed.

Assuming you have a script called hello.py which you want to turn into an executable, this can be accomplished by this command:

cxfreeze hello.py --target-dir dist


Further customization can be done using the following options:

option name description
--version show version number and exit
-h, --help show this help message and exit
-O optimize generated bytecode as per PYTHONOPTIMIZE; use -OO in order to remove doc strings
-c, --compress compress byte code in zip files
-s, --silent suppress all output except warnings
--base-name file on which to base the executable; if the name of the file is not an absolute file name, the subdirectory bases inside the cx_Freeze package will be searched for a file matching the name, without regard to the extension
--init-script script which will be executed upon startup; if the name of the file is not an absolute file name, the subdirectory initscripts inside the cx_Freeze package will be searched for a file matching the name, without regard to the extension
--target-dir directory in which to place the executable and any dependent files
--target-name the name of the file to create; the default is the base name of the script and the extension of the base executable name
--shared-name the name of the file to create; the default is the base name of the script and the extension of the base executable name
--no-copy-deps do not copy any dependent files (extensions, shared libraries, etc.) to the target directory; this also modifies the default init script to ConsoleKeepPath and means that the target executable requires a Python installation to execute properly
--default-path list of paths separated by the standard path separator for the platform which will be used to initialize the search path instead of sys.path
--include-path list of paths separated by the standard path separator for the platform which will be used to add to the search path
--replace-paths replace all the paths in modules found in the given paths with the given replacement string; multiple values are separated by the standard path separator for the platform and each value is of the form <search>=<replace>; <search> can be * which means all paths not already specified
--include-modules comma separated list of names of modules to include
--exclude-modules comma separated list of names of modules to exclude
--ext-list-file name of file in which to place the list of dependent files which were copied to the target directory
-z, --zip-include name of file to add to the zip file or a specification of the form <name>=<arcname> which will specify the archive name to use; multiple --zip-include arguments can be used

### distutils setup script

In order to make use of distutils a setup script must be created. This is called setup.py by convention although it need not be called that. A very simple script might use the following:

from cx_Freeze import setup, Executable

setup(
name = "hello",
version = "0.1",
description = "the typical 'Hello, world!' script",
executables = [Executable("hello.py")])


The script is invoked as follows:

python setup.py build


This command will create a subdirectory called build with a further subdirectory starting with the letters exe. and ending with the typical identifier for the platform that distutils uses. This allows for multiple platforms to be built without conflicts.

cx_Freeze creates two new commands and subclasses four others in order to provide the ability to both build and install executables. In typical distutils fashion they can be provided in the setup script, on the command line or in a setup.cfg configuration file. They are described in further detail below.

#### distutils commands

##### build

This command is a standard command which has been modified by cx_Freeze to build any executables that are defined. The following options were added to the standard set of options for the command:

option name description
build-exe (-b) directory for built executables and dependent files
##### build_exe

This command performs the work of building an executable or set of executables. It can be further customized:

option name description
build-exe (-b) directory for built executables and dependent files
optimize (-o) optimization level, one of 0 (disabled), 1 or 2
excludes (-e) comma separated list of names of modules to exclude
includes (-e) comma separated list of names of modules to include
packages (-p) comma separated list of packages to include, which includes all submodules in the package
replace-paths comma separated list of paths to replace in modules in the form <search>=<replace>, using the value * in the search portion of the directive will cause all paths not mentioned by any other directive to be replaced with the replacement value
path comma separated list of paths to search; the default value is sys.path
init-script (-i) the name of the script to use during initialization which, if given as a relative path, will be joined with the initscripts subdirectory of the cx_Freeze installation; the default value is "Console"
base the name of the base executable to use which, if given as a relative path, will be joined with the bases subdirectory of the cx_Freeze installation; the default value is "Console"
compressed (-c) create a compressed zip file
copy-dependent-files copy all dependent files
create-shared-zip create a shared zip file called library.zip which will contain all modules shared by all executables which are built
append-script-to-exe append the script module to the executable
include-in-shared-zip include the script module in the shared zip file
icon include the icon in the frozen executables on the Windows platform and alongside the frozen executable on other platforms
constants comma separated list of constant values to include in the constants module called BUILD_CONSTANTS in form <name>=<value>
include-files list containing files to be copied to the target directory; it is expected that this list will contain strings or 2-tuples for the source and destination; the source can be a file or a directory (in which case the tree is copied except for .svn and CVS directories); the target must not be an absolute path
zip-includes list containing files to be included in the zip file directory; it is expected that this list will contain strings or 2-tuples for the source and destination
bin-includes list of names of files to include when determining dependencies of binary files that would normally be excluded
bin-excludes list of names of files to exclude when determining dependencies of binary files that would normally be included
silent (-s) suppress all output except warnings
##### install

This command is a standard command which has been modified by cx_Freeze to install any executables that are defined. The following options were added to the standard set of options for the command:

option name description
install-exe directory for installed executables and dependent files
##### install_exe

This command performs the work installing an executable or set of executables. It can be used directly but most often is used when building Windows installers or RPM packages. It can be further customized:

option name description
install-dir (-d) directory to install executables to; this defaults to a subdirectory called <name>-<version> in the "Program Files" directory on Windows and <prefix>/lib on other platforms; on platforms other than Windows symbolic links are also created in <prefix>/bin for each executable.
build-dir (-b) build directory (where to install from); this defaults to the build_dir from the build command
force (-f) force installation, overwriting existing files
skip-build skip the build steps
##### bdist_msi

This command is a standard command in Python 2.5 and higher which has been modified by cx_Freeze to handle installing executables and their dependencies. The following options were added to the standard set of options for the command:

option name description
add-to-path add the target directory to the PATH environment variable; the default value is True if there are any console based executables and False otherwise
upgrade-code define the upgrade code for the package that is created; this is used to force removal of any packages created with the same upgrade code prior to the installation of this one
##### bdist_rpm

This command is a standard command which has been modified by cx_Freeze to ensure that packages are created with the proper architecture for the platform. The standard command assumes that the package should be architecture independent if it cannot find any extension modules.

#### cx_Freeze.Executable

The options for the build_exe command are the defaults for any executables that are created. The options for the Executable class allow specification of the values specific to a particular executable. The arguments to the constructor are as follows:

argument name description
script the name of the file containing the script which is to be frozen
initScript the name of the initialization script that will be executed before the actual script is executed; this script is used to set up the environment for the executable; if a name is given without an absolute path the names of files in the initscripts subdirectory of the cx_Freeze package is searched
base the name of the base executable; if a name is given without an absolute path the names of files in the bases subdirectory of the cx_Freeze package is searched
path list of paths to search for modules
targetDir the directory in which to place the target executable and any dependent files
targetName the name of the target executable; the default value is the name of the script with the extension exchanged with the extension for the base executable
includes list of names of modules to include
excludes list of names of modules to exclude
packages list of names of packages to include, including all of the package's submodules
replacePaths list of 2-tuples containing search values and replacement values for paths of modules that are included; a search value of "*" will cause all paths not otherwise specified to be replaced with that directive's replacement value
compress boolean value indicating if the module bytecode should be compressed or not
copyDependentFiles boolean value indicating if dependent files should be copied to the target directory or not
appendScriptToExe boolean value indicating if the script module should be appended to the executable itself
appendScriptToLibrary boolean value indicating if the script module should be appended to the shared library zipfile
icon name of icon which should be included in the executable itself on Windows or placed in the target directory for other platforms