Why Nuitka Beats PyInstaller: Faster, Smaller Python Executables

This article compares PyInstaller and Nuitka for converting Python projects into standalone executables, highlighting Nuitka's dramatically smaller file size, faster build times, and better performance while also providing step‑by‑step installation and usage instructions.

21CTO
21CTO
21CTO
Why Nuitka Beats PyInstaller: Faster, Smaller Python Executables

1. PyInstaller vs. Nuitka Experience

The project required turning Python code into an executable, leading to the discovery of two packaging tools: PyInstaller and Nuitka.

Hide source code : PyInstaller encrypts the source with a key, while Nuitka compiles Python to C++ (producing binary .pyd files) to prevent decompilation.

Easy portability : Both tools allow the resulting exe to run without installing Python or third‑party packages.

1.2 Personal impressions

PyInstaller experience:

The generated exe for a deep‑learning project was nearly 3 GB because PyInstaller bundles the entire runtime.

Packaging and startup were both extremely slow.

Nuitka experience:

The same project produced an exe of only about 7 MB.

Packaging completed in under a minute and the executable started instantly.

2. Installing and Using Nuitka

2.1 Installation

Install via pip: pip install Nuitka Download a C++ compiler such as Visual Studio 2019 (MSVC) or MinGW‑64.

2.2 Build command

For projects with many third‑party dependencies (e.g., torch, tensorflow, cv2, numpy, pandas), it is best to compile only the project's own code to C++ and leave the large libraries untouched.

Example directory structure (using a PyQt5 UI):

├─utils//source folder1
├─src//source folder2
├─logo.ico//demo icon
└─demo.py//main file

Run the following command to build the executable (debug mode):

nuitka \
  --standalone \
  --show-memory \
  --show-progress \
  --nofollow-imports \
  --plugin-enable=qt-plugins \
  --follow-import-to=utils,src \
  --output-dir=out \
  --windows-icon-from-ico=./logo.ico demo.py

Explanation of the most important options: --standalone: creates a portable package that runs on other machines without Python installed. --show-memory --show-progress: displays compilation progress and memory usage. --nofollow-imports: skips compiling imported libraries such as keras or numpy. --plugin-enable=qt-plugins: enables the Qt plugin needed for PyQt5. --follow-import-to=utils,src: specifies the two folders containing the project's own source code to be compiled. --output-dir=out: sets the output directory. --windows-icon-from-ico=./logo.ico: sets the executable's icon.

After about one minute of compilation, the output directory contains:

├─utils//source folder1
├─src//source folder2
├─out//generated files
│   ├─demo.build
│   └─demo.dist
│       └─demo.exe // final executable
│       ├─logo.ico // icon
│       └─demo.py // original script

If the executable reports missing modules (e.g., no module named torch, cv2, tensorflow), copy the corresponding package folders from the Python site‑packages directory into demo.dist. After that, the exe runs correctly.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceDeploymentExecutablepyinstallerpython packagingNuitka
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.