Nuitka Advanced Tour

Dear Python expert,

you know Python well, and you want to know what Nuitka is really doing, and how to get the most out of it. This tour gives you the short version, with pointers to the deep documentation.

How Nuitka works

Nuitka is a source to source compiler. It translates your Python program into optimized C code, which is then compiled by your system’s C compiler and linked against libpython, so the resulting binary behaves exactly like CPython.

On top of the translation, Nuitka applies a growing set of optimizations, including:

  • Constant folding and propagation.

  • Control flow optimizations.

  • Type inference that replaces Python objects with native C types where it is provably safe. This is work in progress, with more coming in future releases.

  • anti-bloat work, which replaces slow or unnecessary modules, e.g. mock or test helpers, with fast dummy implementations. This is on by default.

The result is usually a noticeable speedup over CPython, without changing the semantics of your program.

Controlling the Build

You can compile whole programs rather than single files:

python -m nuitka --follow-imports program.py

For non-trivial projects, put the build configuration in nuitka-project: comment directives at the top of your main program, so that options, data files, and plugin settings are versioned with the source. See Nuitka User Manual for the syntax.

Nuitka has a plugin system to support packages that need special handling, e.g. GUI toolkits. Plugins are automatically activated when Nuitka detects that their target package is used.

Performance Tuning

  • Use --lto=yes for link time optimizations.

  • Compile the whole program with --follow-imports.

  • Read Performance for what to expect and what to measure.

Extending Nuitka