Python is a high-level programming language. Therefore, packaging Python code into an executable file (.exe) is a very effective solution that allows users to run programs directly without installing a Python environment, thereby improving the user experience.
1、pyinstaller
The process of packaging Python code using PyInstaller is relatively simple, just enter the following command on the command line:
pyinstaller --onefile file name.py
2、cx_Freeze
cx_Freeze is also a widely used packaging tool, used in a similar way to PyInstaller.
cxfreeze file name.py --target-dir dist
3、Nuitka
Nuitka can not only package Python code into executable files, but also improve execution efficiency through compilation.
nuitka --standalone file name.py
4、PyOxidizer
With the increasing demand for performance and single file distribution, PyOxidizer is beginning to attract attention. It can package the entire Python application and its dependencies into a separate executable file, suitable for application development under modern microservice architectures. Although it is slightly more difficult to get started with other tools, it has obvious advantages for large-scale projects.
Install PyOxidizer:
1. First, you need to make sure you have installed the Rust toolchain because PyOxidizer is written in Rust. Rust can be installed via the following command:
curl --proto '=https' --tlsv1.2 -sSf | sh
2. After installing Rust, install PyOxidizer:
cargo install pyoxidizer
Create a PyOxidizer configuration file:
def make_python_exe(): return python_executable( name = "my_app", source = "File name.py", # Replace with your Python script ) # Call the function here to generate the executable filemake_python_exe()
5、Docker
Although .exe files cannot be generated directly, using Docker to package Python applications has become a common practice in many cloud or containerized environments. Developers can encapsulate applications and their dependencies in Docker containers for rapid deployment and environmental isolation.
Create a file named Dockerfile in the project root directory, with the following content:
# Use the official Python imageFROM python:3.9-slim # Set up the working directoryWORKDIR /app # Copy the current directory contents to the /app directory in the containerCOPY . . # Dependencies required for installationRUN pip install -r # Run Python programsCMD ["python", "File name.py"] # Replace with your Python script
Run the following command in the terminal to build a Docker image
docker build -t my_python_app .
After the build is completed, the container can be run:
docker run my_python_app
This is the end of this article about how to package Python code into exe executable files. For more related Python code into exe content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!