如何将Python 2.7代码转换为Linux可执行文件(类似gcc编译方式)
gcc -o hello hello.c) Hey there! If you're looking to convert your Python 2.7 script into a standalone Linux executable—similar to how gcc compiles a C file into a runnable binary—you've got a couple of solid tools to choose from. Let's break them down step by step:
1. Using PyInstaller (Most Popular Option)
PyInstaller packages your Python script and all its dependencies into a single executable file, which works without needing Python installed on the target machine.
Steps:
- First, install a version of PyInstaller that supports Python 2.7 (newer versions dropped support for 2.7). Run:
pip install pyinstaller==3.6 - Navigate to the directory where your Python script (say,
my_script.py) is located. - Run the packaging command:
Thepyinstaller --onefile my_script.py--onefileflag tells PyInstaller to bundle everything into a single executable. Without it, you'll get a folder with multiple files instead. - Once it finishes, you'll find your executable in the
distsubdirectory. Run it like any other Linux binary:./dist/my_script
Notes:
- Make sure all third-party libraries your script uses are installed in your Python 2.7 environment—PyInstaller will bundle them into the executable.
- For best results, package the executable on the same Linux distribution/architecture you plan to run it on (this avoids compatibility issues with system libraries).
2. Using cx_Freeze
cx_Freeze is another reliable tool that freezes Python scripts into executables, and it also supports Python 2.7.
Steps:
- Install a compatible version of cx_Freeze:
pip install cx_Freeze==5.1.1 - Create a
setup.pyfile in the same directory as your script. Here's a template you can adapt:import sys from cx_Freeze import setup, Executable # Replace with your script's filename target_script = "my_script.py" # No base is needed for Linux executables base = None if sys.platform == "linux": base = None executables = [Executable(target_script, base=base)] # Configure the build setup setup( name="MyApp", version="1.0", description="My Python 2.7 Application", executables=executables ) - Run the build command:
python setup.py build - Your executable will be in a subfolder under the
builddirectory (e.g.,build/exe.linux-x86_64-2.7/my_script). Run it with:./build/exe.linux-x86_64-2.7/my_script
A Quick Note on Shebang (Not True "Compilation")
If you don't need a fully standalone executable and just want to run the script directly like a binary, you can add a shebang line at the very top of your Python script:
#!/usr/bin/env python2.7
Then make it executable with:
chmod +x my_script.py
You can run it with ./my_script.py, but this requires Python 2.7 to be installed on the target system—it's not a compiled binary like the outputs from the tools above.
Important Reminder
Python 2.7 has been end-of-life since 2020, so if possible, consider upgrading to Python 3 for better security and long-term support. But if you're stuck with 2.7 for now, the tools above should get the job done.
内容的提问来源于stack exchange,提问作者Goten Black

