Converting Python Scripts to Optimized Executables with Nuitka and the UPX Plugin

Ahmet Nuri Yılmaz
4 min readApr 13, 2023

--

Nuitka is a powerful, open-source Python compiler that converts Python code into C or C++ code and compiles it into standalone executables. By optimizing the code and reducing the overhead of the Python interpreter, Nuitka aims to enhance the performance of Python programs. However, the size of Nuitka-generated executables can be relatively large due to the bundled Python interpreter and other dependencies.

To address this issue, we can use the UPX (Ultimate Packer for executables) plugin, which compresses the executables without affecting their functionality. UPX is a free, open-source executable packer that results in smaller distribution sizes and potentially faster startup times.

Photo by Alex Chumak on Unsplash

In this article, we will provide a step-by-step guide on how to convert Python scripts to optimized executables using Nuitka and UPX.

Overview of Nuitka Features

  • Performance Optimization: Nuitka compiles Python code into C or C++ and applies various optimization techniques, potentially improving the performance of Python programs.
  • Standalone Executables: Nuitka enables the creation of standalone executables that bundle the Python interpreter, allowing for the distribution of Python programs without requiring users to install Python or dependencies.
  • Source Code Protection: Compiling Python code with Nuitka obfuscates the original source code, making it more difficult to reverse-engineer or modify the program.
  • Compatibility: Nuitka supports a wide range of Python versions (from 2.6 to 3.11) and is compatible with most Python libraries and extensions.

Overview of UPX Features

  • Executable Compression: UPX compresses executables without affecting their functionality, leading to smaller distribution sizes.
  • Cross-platform Support: UPX supports a variety of executable formats and works on multiple platforms, including Windows, macOS, and Linux.
  • Faster Startup Times: Compressed executables may experience faster startup times due to reduced file sizes and faster loading.

Installing Nuitka and UPX

  • Install Nuitka using pip, the Python package installer, by executing the following command in the terminal or command prompt:
pip install Nuitka

UPX: Download the appropriate UPX binary for your platform from the official website (https://upx.github.io/) and follow the installation instructions provided.

Compiling Python Scripts with Nuitka

After installing Nuitka, compile a Python script by navigating to the directory containing the script and running the following command:

nuitka --onefile your_script.py

Replace your_script.py with the name of your Python script. The --onefile option instructs Nuitka to create a single, standalone executable file.

Compressing the Compiled Executable with UPX

After successfully compiling your script with Nuitka, compress the resulting executable using UPX. Navigate to the directory containing the compiled executable and run the following command:

upx --best your_compiled_executable

Replace your_compiled_executable with the name of the compiled executable. The --best option tells UPX to use the highest compression level.

Once the compression process is complete, run the compressed executable to execute your optimized Python program. The functionality of the program should remain unchanged, but the file size should be reduced, and you may experience faster startup times.

Use Nuitka and UPX Together

You can also use them together at the same time for obtaining an optimized executable from your Python script.

For that, execute the following command in your prompt:

 python -m nuitka --standalone --plugin-enable=upx --upx-binary="YOUR_UPX_PACKAGE_LOCATION" your_script.py

Thus, the optimized executable file will be created in the same directory as your Python script.

"YOUR_UPX_PACKAGE_LOCATION" is the directory which you extract the downloaded UPX package.

Advanced Usage and Options:

Both Nuitka and UPX offer a variety of advanced options and features for developers who wish to fine-tune the optimization and compression processes:

  1. Nuitka: Nuitka provides several advanced options, such as enabling aggressive optimization with the --lto (Link Time Optimization) option or manually specifying the location of your Python installation with the --python-version option. For a comprehensive list of available options and features, consult the Nuitka documentation (https://nuitka.net/doc/user-manual.html), (https://github.com/Nuitka/Nuitka).
  2. UPX: UPX also offers multiple options, such as adjusting the compression level with the --ultra-brute option for even smaller file sizes (at the cost of increased compression time) or decompressing a previously compressed executable with the --decompress option. To explore all available UPX options, refer to the UPX documentation (https://github.com/upx/upx).

Nuitka and UPX are powerful tools that can help Python developers optimize their code, create standalone executables, protect their source code, and reduce distribution sizes. By following the steps outlined in this blog post, you can harness the power of these tools to enhance your Python projects. While the performance gains and compression rates may vary depending on the nature of the code, the combination of Nuitka and UPX offers numerous benefits that make them indispensable additions to any Python developer’s toolkit.

--

--