A 'frozen application' in the context of Python refers to a standalone executable that bundles a Python script, the Python interpreter itself, and all necessary libraries and modules into a single distributable package. This allows the application to run on a target system even if Python is not installed or configured. The primary benefits of freezing an application include:
1. Portability: The application can be distributed and run on any compatible machine without requiring the end-user to install Python or specific dependencies.
2. Ease of Distribution: Users simply receive an executable file or a folder containing it, making installation and deployment straightforward.
3. Dependency Management: All required libraries are bundled, ensuring that the application runs with the correct versions of its dependencies, avoiding conflicts or missing modules on the target system.
4. User Experience: For non-technical users, it simplifies running the application as they don't need to understand Python environments or package managers.
5. Intellectual Property Protection (limited): While not full encryption, bundling the source code within an executable makes it less trivial to inspect and modify compared to distributing raw .py files.
cx-freeze is a popular, open-source tool for creating these standalone executables from Python scripts. It works by scanning your Python script for all imported modules and packages, collecting them along with a compatible Python interpreter, and packaging them into a designated build directory. When the generated executable is run, it uses the bundled interpreter and libraries to execute your application.
How cx-freeze works (typical workflow):
1. Your Python Application Script: You write your Python application (e.g., `my_app.py`).
2. `setup.py` Configuration File: You create a `setup.py` file, which is a standard Python build script, to configure `cx-freeze`. This script tells `cx-freeze` which Python file to freeze, what options to use (like including/excluding specific modules, specifying base executables for GUI applications, or including additional data files).
3. Execution: You run the `setup.py` script from your command line using `python setup.py build`. `cx-freeze` then performs the packaging.
4. Output: A `build` directory (or a platform-specific distribution like `.msi` for Windows) is created, containing your standalone executable and all its dependencies.
Key components in `setup.py` for cx-freeze:
- `from cx_Freeze import setup, Executable`: Imports necessary components.
- `build_exe_options`: A dictionary used to pass various options to `cx-freeze`, such as `packages` (modules to explicitly include), `includes` (other modules), `excludes` (modules to omit), `include_files` (non-Python files like images, data, etc.), `base` (specifies the type of executable, e.g., `"Win32GUI"` for Windows GUI apps to hide the console window, or `None` for console apps).
- `executables`: A list of `Executable` objects, each representing an application to be frozen. You specify the target script and its `base`.
Example Code
First, install cx-freeze:
pip install cx-freeze
--- 1. Create your Python application (e.g., hello_app.py) ---
This is a simple Tkinter GUI application.
Save this as `hello_app.py`
import tkinter as tk
def show_message():
label.config(text="Hello, Frozen World!")
root = tk.Tk()
root.title("My Frozen App")
root.geometry("300x200")
label = tk.Label(root, text="Click the button!")
label.pack(pady=20)
button = tk.Button(root, text="Say Hello", command=show_message)
button.pack()
root.mainloop()
--- 2. Create the cx-freeze setup script (e.g., setup.py) ---
This script configures cx-freeze to freeze your hello_app.py.
Save this as `setup.py` in the same directory as `hello_app.py`
import sys
from cx_Freeze import setup, Executable
Define the base executable type
"Win32GUI" is used for GUI applications on Windows to prevent a console window from opening.
Use None for console applications.
base = None
if sys.platform == "win32":
base = "Win32GUI"
Configuration options for the build_exe command
build_exe_options = {
"packages": [], List of packages to include (e.g., ["os", "sys"])
"includes": ["tkinter"], List of modules to include (tkinter is essential for GUI)
"excludes": [], List of modules to exclude (e.g., ["unittest"])
"include_files": [], List of additional files/folders to include
"optimize": 2, Optimization level (0, 1, or 2)
"silent": True, Suppress output during build
}
Create an Executable object
The first argument is the name of your main Python script.
The `base` argument determines the type of executable.
executables = [
Executable("hello_app.py", base=base, target_name="MyApp.exe", icon="your_icon.ico") Optionally add an icon
]
Call the setup function
setup(
name="MyFrozenApp",
version="1.0",
description="A simple frozen Tkinter application",
options={
"build_exe": build_exe_options
},
executables=executables
)
--- 3. How to run cx-freeze ---
Open your command prompt or terminal, navigate to the directory
where `hello_app.py` and `setup.py` are saved, and run:
python setup.py build
After execution, a `build` directory will be created (e.g., `build\exe.win-amd64-3.x`)
Inside this directory, you will find `MyApp.exe` (or `MyApp` on Linux/macOS)
which is your standalone frozen application.








Frozen Application + cx-freeze