What is Nuitka?
Nuitka is more than just a tool for converting Python scripts into executables; it’s a powerful Python-to-C++ compiler that aims to offer an easy means to distribute Python applications. By translating Python code into optimized C/C++ code, it attempts to improve execution speed and reduce runtime dependencies. Nuitka is compatible with a wide range of Python versions, including Python 2.6, 2.7, and 3.3 to 3.10, offering developers flexibility across different projects.
Key features:
- Performance improvement: Nuitka can significantly enhance the speed of Python programs by converting them into optimized C/C++ code, which is then compiled into binary format.
- Compatibility: It maintains high compatibility with existing Python code, supporting various modules and libraries without the need for modification.
- Cross-platform: Nuitka-generated executables are cross-platform, capable of running on Windows, macOS, Linux, and more, without needing a Python interpreter installed.
- Standalone executables: One of the standout features is its ability to create standalone executables. This means all necessary files are packaged into a single executable file, simplifying distribution and deployment.
Installation
Installing Nuitka is straightforward with pip, Python’s package installer. Ensure you have a working Python environment and pip installed.
Open the terminal or command prompt and run the following command:
pip install -U nuitka
This will download and install the latest version of Nuitka, along with any required dependencies.
Usage
Nuitka provides a versatile command line interface (CLI) with numerous options to tailor the compilation process to your specific needs.
To compile a Python script (your_script.py
) into a single executable file:
This may prompt you to install a caching tool and a Ming64 compiler, you should choose yes when given these options.
nuitka --onefile your_script.py
This will create an executable that is suitable for your system. As you can see, it’s very simple for simple scripts.
Creating standalone executables
If you wish to distribute to other systems, you will need to make use of standalone mode.
In this mode, all imports are included by default. You can choose to exclude specific modules by using --nofollow-import-to
, but this will result in an ImportError
if the excluded module is attempted to be imported during program runtime. This may lead to altered behavior, but it can also improve compile time if used thoughtfully.
To include data files, use the option --include-data-files=<source>=<target>
. The source should be a file system path, while the target must be specified relative to it. In standalone mode, manual copying of data files is possible, but it may involve additional checks. In onefile mode, manual copying is not an option.
For copying files within a directory, use the option --include-data-files=/etc/*.txt=etc/
. This allows you to specify shell patterns for the files to copy and the subdirectory where they should be placed, denoted by the trailing slash.
nuitka --standalone --include-data-files=/config/app_config.yaml=/app_data/config/app_config.yaml your_script.py
Other useful arguments and flags
-follow-imports
: Tells Nuitka to include imported Python modules and packages in the compilation.-onefile
: Creates a single executable file for easier distribution (similar to PyInstaller’s onefile mode).-python-flag=no_site
: Disables thesite
module, which can reduce startup time and file size.-enable-plugin
: Enables plugins that can extend Nuitka’s functionality, such as including data files or optimizing specific libraries.
Performance tuning
While Nuitka does its best to optimize the generated C/C++ code, you can further fine-tune performance through:
- Profile-guided optimizations: Compiling your program with profiling enabled (
-enable-plugin=pylint-warnings
), running it to collect data, and then recompiling with this data can lead to more optimized binaries. - Explicit type hints: Using Python’s type hints can help Nuitka make more informed decisions during the compilation, potentially improving performance.