🚀 Think you’ve got what it takes for a career in Data? Find out in just one minute!

Cx Freeze: compile and distribute your Python program

-
2
 m de lecture
-
Cx Freeze may have additional configuration options depending on the complexity of your project and any external dependencies it relies on.

Cx Freeze is a Python program that transforms a script into a stand-alone executable on Linux, MacOS or Windows. Making a program executable, i.e. compiling the code, is important for easy distribution.

Cx Freeze is the program that compiles the Python script and all its dependencies into an executable file.

So how does Cx Freeze work?

Why use it?

How do you install and use it?

Answers to your questions in this article.

How does Cx Freeze work?

A Python script depends on several modules. It needs the libraries and files of the Python system. Cx Freeze analyzes the script’s dependencies to create a stand-alone executable.

The name Cx Freeze is used because this program “freezes” the Python script and its dependencies in an executable file.

The program analyzes the dependencies to identify the imported Python modules, resources and external libraries required to run the file.

It then creates a virtual environment in which to copy the necessary files and modules.

Finally, Cx Freeze creates the stand-alone executable: no external Python installation is required.

 

💡Related articles:

Folium: Discover the open source Python library
Matplotlib: Master Data Visualization in Python
Python Crash Course: Get started
Mastering Machine Learning in Python: Data-Driven Success
Python Programming for Beginners – Episode 3
Django: All about the Python web development framework

Why use it?

The Python program cx_Freeze offers several advantages (it exists for a reason!). Need to share a Python script? Your colleague or partner may find it tedious to install Python and all its dependencies.

The cx_Freeze program allows you to bypass this installation thanks to a single executable file that’s easy to distribute.

Another strong point is the confidentiality of the Python script. You don’t need to disclose the source code: by creating an executable, you simply share the file rather than your code.

Finally, cx_Freeze is easy for developers to use. Although you’ll need to test the executable to make sure it’s working properly, creating it is quick and intuitive.

How do I install and use Cx Freeze?

You can install the latest version of cx_Freeze using the pip command:

pip install --upgrade cx_Freeze

Some operating systems may require you to use Python3 and pip3 rather than other versions.

You can also use the pipenv command to install and update cx_Freeze :

pipenv install cx_Freeze
pipenv update cx_Freeze

If you’re using Anaconda, it’s best to install cx_Freeze via the conda command:

conda install -c conda-forge cx_freeze

Once cx_Freeze has been installed, we need to create a python file to compile the program: we’ll call it setup.py. The code is as follows (modify the parameters if you wish to customize the executable):

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but may need to be adjusted.
build_exe_options = {
“excludes”: [“tkinter”, “unittest”],
“zip_include_packages”: [“encodings”, “PySide6”],
}

# base=”Win32GUI” should only be used with the Windows GUI app
base = “Win32GUI” if sys.platform == “win32” else None

setup(
name=”guifoo”,
version=”0.1″,
description=”My GUI application!”,
options={“build_exe”: build_exe_options},
executables=[Executable(“guifoo.py”, base=base)],
)

Replace “guifoo” and “guifoo.py” with the file you wish to transform.

Next, run the command:

python setup.py build

This command will create a subfolder called “build” with another subfolder beginning with the letters “exe.” and ending with the identification of the platform or your version of Python. This allows multiple platforms to be created without problems.

Cx_Freeze: the Python program in a nutshell

The cx_Freeze program is a great asset for developers: it compiles code and makes it executable on its own. The program identifies the modules required to execute the file and copies them into a virtual environment.

Cx_Freeze offers simplified distribution, unrivalled portability and source code confidentiality. To install and use Cx_Freeze, simply follow the user guide above.

Facebook
Twitter
LinkedIn

DataScientest News

Sign up for our Newsletter to receive our guides, tutorials, events, and the latest news directly in your inbox.

You are not available?

Leave us your e-mail, so that we can send you your new articles when they are published!
icon newsletter

DataNews

Get monthly insider insights from experts directly in your mailbox