Why Nuitka Beats PyInstaller: Faster, Smaller Python Executables
This article compares PyInstaller and Nuitka for turning Python projects into standalone executables, highlighting Nuitka's dramatically smaller file size, faster build times, and better performance while explaining installation steps, command‑line options, and how to handle third‑party dependencies.
1. Experience with PyInstaller and Nuitka
1.1 Requirements
For a recent project I needed to convert Python code into an .exe file, and I discovered two tools that can package Python projects: PyInstaller and Nuitka.
Both tools satisfy the following 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 . Users can run the program without installing Python or any third‑party packages.
1.2 Impressions
The biggest takeaways after using the two tools are:
PyInstaller experience is poor:
A deep‑learning project packaged with PyInstaller resulted in an exe of nearly 3 GB because it bundles the entire runtime environment.
Packaging is extremely slow, and the resulting executable starts very slowly.
Nuitka is excellent:
The same project produces an exe of only about 7 MB.
Packaging completes in under a minute and the executable starts instantly.
2. Installing and Using Nuitka
2.1 Installation
Install directly via pip: pip install Nuitka Download Visual Studio 2019 (MSVC) or MinGW64 – any C++ compiler will work.
2.2 Usage Process
For projects with many third‑party dependencies (e.g., torch, tensorflow, cv2, numpy, pandas, geopy), it is best to compile only your own code to C++ and leave the large external packages untouched.
Example directory structure of my demo (using PyQt5 for the UI):
├─utils//source folder1
├─src//source folder2
├─logo.ico//demo icon
└─demo.py//main fileGenerate an executable with the following command (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.pyExplanation of the most important flags: --standalone: creates a portable package that runs on other machines without requiring Python. --show-memory --show-progress: displays memory usage and progress during compilation. --nofollow-imports: skips compiling imported modules such as keras, numpy, etc. --plugin-enable=qt-plugins: enables the Qt plugin needed for PyQt5 applications. --follow-import-to=utils,src: specifies the two folders containing the code to be compiled to C++. --output-dir=out: sets the output directory. --windows-icon-from-ico=./logo.ico: sets the generated exe's icon. --windows-disable-console: (optional) hides the console window when the exe runs.
After about one minute of compilation you will see a directory structure like:
├─utils//source folder1
├─src//source folder2
├─out//generated exe folder
├─demo.build
└─demo.dist
└─demo.exe // final executable
├─logo.ico // icon
└─demo.py // main fileRunning the exe may initially raise errors such as no module named torch,cv2,tensorflow because those third‑party packages were not compiled. Copy the required package folders (e.g., numpy, cv2) from your Python site‑packages directory into demo.dist.
Once the missing modules are added, the exe runs perfectly.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
