Using PyInstaller and Nuitka to Package Python Applications: Experience and Step‑by‑Step Guide
This article compares PyInstaller and Nuitka for converting Python projects into standalone executables, shares practical experiences—including size and speed differences—and provides a detailed installation and command‑line guide for Nuitka with code examples and post‑build dependency handling.
The author needed to turn a Python project into an executable and evaluated two packaging tools, PyInstaller and Nuitka, both of which meet the basic requirements of hiding source code and enabling easy deployment.
1.1 Usage demand – Both tools can hide source code (PyInstaller encrypts with a key, Nuitka compiles Python to C++ producing binary .pyd files) and simplify distribution without requiring a Python runtime on the target machine.
1.2 Experience – PyInstaller produced a ~3 GB EXE, was slow to build and start, while Nuitka generated a ~7 MB EXE, built in under a minute and launched quickly.
2.1 Installing Nuitka – Install via pip: pip install Nuitka . Install a C++ compiler such as Visual Studio 2019 or MinGW‑64.
2.2 Build process – For projects with many third‑party dependencies (e.g., torch, tensorflow, cv2, numpy, pandas, geopy), the recommended approach is to compile only the project's own code to C++ and leave large libraries untouched.
Example project structure:
<code>├─utils// source folder 1
├─src// source folder 2
├─logo.ico// demo icon
└─demo.py// main script</code>Command to build the executable (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 key options:
--standalone: creates a self‑contained package that runs on machines without Python.
--show-memory, --show-progress: display build progress.
--nofollow-imports: skip compiling imported libraries such as keras, numpy.
--plugin-enable=qt-plugins: include Qt plugins needed for PyQt5.
--follow-import-to=utils,src: compile only the specified source folders.
--output-dir=out: place build output in the out directory.
--windows-icon-from-ico=./logo.ico: set the executable icon.
After about one minute of compilation, the output directory contains:
<code>├─utils// source folder 1
├─src// source folder 2
├─out// generated files
├─demo.build
└─demo.dist
└─demo.exe
├─logo.ico
└─demo.py</code>Running the EXE may initially raise errors for missing modules (e.g., torch, cv2, tensorflow). Copy the required third‑party package folders from the Python site‑packages directory into demo.dist , then the executable runs correctly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.