Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Using CX_Freeze in Python
CX_Freeze is a Python library that converts Python scripts into standalone executables. This allows you to share your Python programs with others without requiring them to install Python or additional modules on their machines.
Installing CX_Freeze
First, install CX_Freeze using pip ?
pip install cx_Freeze
Note: The correct package name is cx_Freeze, not CX_Frezze.
Creating a Sample Python Program
Let's create a simple Python program that we'll convert to an executable. This program fetches and parses content from a website ?
import urllib.request
import urllib.parse
import re
import time
my_url = 'https://httpbin.org/get'
my_req = urllib.request.Request(my_url)
my_resp = urllib.request.urlopen(my_req)
my_respData = my_resp.read()
print("Response received:")
print(my_respData.decode('utf-8'))
time.sleep(5)
Save this as web_parser.py. The program includes a 5-second sleep so you can see the output before the window closes.
Creating the Setup File
Create a setup file named setup.py in the same directory ?
from cx_Freeze import setup, Executable
setup(
name="WebParser",
version="1.0",
description="A simple web parser program",
executables=[Executable("web_parser.py")]
)
Building the Executable
Open command prompt and navigate to the directory containing both files. Run the build command ?
python setup.py build
This creates a build directory containing your executable program.
Setup Parameters
| Parameter | Purpose | Required |
|---|---|---|
name |
Name of the program | Yes |
version |
Version number | No |
description |
Program description | No |
executables |
List of Python files to convert | Yes |
Advanced Setup Options
You can customize the build process with additional options ?
from cx_Freeze import setup, Executable
build_options = {
"packages": ["urllib", "re"],
"excludes": ["tkinter"]
}
setup(
name="WebParser",
version="1.0",
description="A simple web parser program",
options={"build_exe": build_options},
executables=[Executable("web_parser.py", base="Console")]
)
Key Benefits
− No Python Installation Required: Users don't need Python installed
− Dependency Management: All required modules are included
− Easy Distribution: Share a single executable file
− Cross-Platform: Works on Windows, macOS, and Linux
Conclusion
CX_Freeze simplifies Python program distribution by creating standalone executables. Use the setup.py file to configure build options, then run python setup.py build to generate your executable.
