Backend Development 6 min read

Using PyInstaller and Nuitka to Package Python Projects into Executables

This article compares PyInstaller and Nuitka for converting Python scripts into standalone Windows executables, discusses their advantages and drawbacks, provides step‑by‑step installation of Nuitka, explains key command‑line options, and demonstrates a practical example that yields a tiny, fast‑building executable versus a large, slow‑building PyInstaller output.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Using PyInstaller and Nuitka to Package Python Projects into Executables

The author needed to turn a Python project into a Windows .exe and evaluated two packaging tools: PyInstaller and Nuitka.

1. PyInstaller and Nuitka Usage Experience

1.1 Requirements

The project required source code protection and easy deployment without a Python runtime; both tools satisfy these needs.

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 : The generated executable runs without installing Python or third‑party packages.

1.2 Experience

PyInstaller: very poor experience – the resulting exe for a deep‑learning project was nearly 3 GB because it bundles the entire runtime; packaging and startup were extremely slow.

Nuitka: excellent – the same project produced a 7 MB exe, packaging completed in under a minute, and startup was fast.

2. Nuitka Installation and Usage

2.1 Installation

Install via pip: pip install Nuitka

Install a C++ compiler such as Visual Studio 2019 (MSVC) or MinGW64.

2.2 Usage Process

For projects with many heavy third‑party dependencies (e.g., torch, tensorflow, cv2, numpy, pandas, geopy), 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):

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

Compile with the following command (debug mode):

<code>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</code>

Explanation of the options:

--standalone : creates a portable package that does not require a separate Python installation.

--show-memory --show-progress : displays compilation progress and memory usage.

--nofollow-imports : skips compiling imported modules such as keras, numpy, etc.

--plugin-enable=qt-plugins : enables the Qt plugin needed for PyQt5.

--follow-import-to=utils,src : specifies the folders whose code should be compiled to C++ (comma‑separated).

--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 looks like:

<code>├─utils//source folder1
├─src//source folder2
├─out//generated exe folder
    ├─demo.build
    └─demo.dist
        ├─demo.exe // executable
        ├─logo.ico // icon
        └─demo.py // main file</code>

If the executable fails with errors such as no module named torch,cv2,tensorflow , copy the missing third‑party packages (e.g., the numpy and cv2 folders) from the Python site‑packages directory into demo.dist . After copying the required libraries, the exe runs correctly.

CLIPythonpackagingExecutablepyinstallernuitka
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login 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.