Nuitka: Converting Python Scripts to Binary on Linux
1. Install Nuitka
On Linux, ensure that Python and Nuitka are installed:
pip install nuitka
If your system lacks a C compiler (such as gcc), install it using the following command (for Ubuntu):
sudo apt update && sudo apt install -y gcc g++
2. Basic Compilation Command
Convert a Python script into an executable file using the basic command format:
nuitka --follow-imports your_script.py
Options explanation:
--follow-imports: Automatically tracks and includes all Python modules.your_script.py: Your Python script file.
3. Generate a Standalone Executable (No Python Environment Required)
To run the executable without requiring a Python interpreter on the target system, use the --standalone option:
nuitka --standalone --follow-imports your_script.py
This command generates an independent folder containing:
- The executable file
your_script.bin - Necessary Python runtime libraries and C extension files
Run the executable using:
./your_script.bin
4. Generate a Single Binary File
To package everything into a single executable file, use the --onefile option:
nuitka --standalone --onefile --follow-imports your_script.py
Note:
--onefilecompresses all dependencies into a single binary, but at runtime, it extracts them to a temporary directory, which may result in slower startup times.
5. Include Additional Data Files
If your program depends on extra files (such as config.json), use the --include-data-files option:
nuitka --standalone --include-data-files=config.json=./config.json --follow-imports your_script.py
6. Additional Optimization
6.1 Remove Debug Information to Reduce File Size
strip your_script.bin
6.2 Statically Link the Python Runtime
To statically link the Python runtime and avoid dependencies:
nuitka --standalone --static-libpython=yes --follow-imports your_script.py
7. Common Issues
Q1: Why is the --onefile output so large?
Since --onefile packages all Python runtime and dependencies, the file size is larger. To reduce size, use upx for compression:
upx --best --lzma your_script.bin
Q2: The generated file won’t run due to missing shared libraries?
Check dependencies with:
ldd your_script.bin
If there are not found dependencies, install the missing libraries.
Q3: Why is the --onefile version slow to start?
Since --onefile extracts files to /tmp, you can speed up execution by specifying a custom extraction path:
--onefile-tempdir-spec=/custom/path
8. Summary
| Requirement | Recommended Command |
|---|---|
| Compile Python script | nuitka --follow-imports your_script.py |
| Generate a standalone executable | nuitka --standalone --follow-imports your_script.py |
| Generate a single executable file | nuitka --standalone --onefile --follow-imports your_script.py |
| Include additional data files | nuitka --standalone --include-data-files=config.json=./config.json --follow-imports your_script.py |
| Statically link Python runtime | nuitka --standalone --static-libpython=yes --follow-imports your_script.py |
With this guide, you can successfully use Nuitka to convert Python scripts into binary executables on Linux! 🚀