Back to Posts
An artistic conceptualization of a high-tech control room showing the dynamic process of improving Python application performance with Nuitka, where symbolic Python and C++ elements intertwine in a flow of energy and data, indicative of the compilation journey from high-level Python code to optimized C++

Optimize Python with Nuitka

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.


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.


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 ( 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

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

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 the site 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.

Improve your code with my 3-part code diagnosis framework

Watch my free 30 minutes code diagnosis workshop on how to quickly detect problems in your code and review your code more effectively.

When you sign up, you'll get an email from me regularly with additional free content. You can unsubscribe at any time.

Recent posts