Skip to content
Compile Python into standalone binaries

Compile Python into standalone binaries

Introducton

The best utility to compile Python 3 scripts or apps into standalone binaries or executables depends entirely on whether you want a simple packager or a true optimizing compiler.Because Python is fundamentally an interpreted language, utilities generally approach this problem in one of two distinct ways:

  • freezing (bundling the Python runtime and your script into a single compressed package)
  • or compiling (transpiling Python source code into machine code via C/C++).

Here is a breakdown of the top utilities available, organized by how they handle your code.

  1. The Industry Standard “Freezers” (Most Reliable & Popular)These utilities do not actually turn your Python code into native binary machine code. Instead, they package your script, its dependencies, and a portable Python interpreter into a single, self-extracting executable archive.PyInstaller: This is the most widely adopted and heavily supported utility across the Python ecosystem. It scans your script, maps out all dependencies (including complex libraries like NumPy or PyQt), and bundles them cleanly.Pros: Incredibly straightforward; usually works out of the box with a single command.Cons: Yields relatively large file sizes because it must include the entire Python environment. It is also vulnerable to reverse-engineering (decompilation).Quick Command: pyinstaller –onefile your_script.pycx_Freeze: A robust, cross-platform alternative that works similarly to PyInstaller.Pros: Highly customizable through standard setup.py scripts; can sometimes bundle trickier environment extensions better than its competitors.Cons: Marginally more configuration overhead required compared to PyInstaller.

  2. True Optimizing Compilers (Best for Performance & Obfuscation)If you need absolute code protection, intellectual property security, or speed boosts, you need a utility that translates your code into a lower-level language before building the binary.Nuitka: This is a true optimizing Python compiler. It takes your Python code and translates it entirely into highly efficient C source code, then compiles it into a native machine binary using a system compiler like GCC, Clang, or MSVC.Pros: Enhances application execution speed; completely obfuscates your source code to prevent reverse engineering; eliminates easy decompilation.Cons: The initial compilation takes significantly longer. Large, dynamic external libraries can occasionally throw compilation blockers.Quick Command: python3 -m nuitka –onefile your_script.py3. Emerging Next-Generation PackagersPyApp: A newer project gaining major traction that approaches packaging through the Rust ecosystem. It builds a highly efficient Rust-based wrapper around your Python project.Pros: Extreme execution speeds, cleaner cross-platform deployment, and a footprint that often bypasses traditional freezing errors.Cons: Requires a baseline understanding of how to configure Rust compilation tools locally.Directly Comparing the Top OptionsFeaturePyInstallerNuitkaPyAppHow it WorksBundles source code + zip runtimeTranspiles source to native CBuilds a custom Rust installer/wrapperCode SecurityLow (Easily decompiled back to code)High (Compiled to machine binaries)Medium (Hidden via localized unpacking)Build SpeedFast (seconds to minutes)Slow (Heavy compilation time)ModerateApp ExecutionNormalOften Faster (optimized C layer)FastBest Used ForQuick deployments, internal IT toolsCommercial distribution, IP protectionModern, containerized desktop deploymentsImportant Rule for All UtilitiesRegardless of the utility you choose, cross-compilation is not natively supported across different operating systems. If you want a Windows .exe binary, you must run the build command on a Windows environment. If you want a macOS executable, you must run the build command on macOS.If you would like to test your builds across various target platforms from a single host development machine, you will need to utilize tools like virtual machines or Docker containers.To help me narrow down the absolute best choice for your project, what operating system(s) are you targeting, and are you using any complex libraries like PyQt, pandas, or TensorFlow?

For a Python app using Slint targeting Linux and macOS first, PyInstaller is the overall best utility to start with, but Nuitka is the best long-term option if you want a lighter, native feel.Because Slint compiles its UI down to native machine code via its Rust-based engine, it relies on a shared binary backend wrapper (_slint.so or _slint.dylib) installed inside your Python environment. Your compiler utility must correctly find and embed this binary extension along with your .slint markup files.Option 1: PyInstaller (Best for Rapid Prototyping)PyInstaller is the fastest way to get your Slint app packaged into a single file. Because it copies your environment as-is, it handles the underlying Slint binary bindings smoothly without throwing compiler errors.Handling your .slint files: You must explicitly tell PyInstaller to include your UI file(s) in the bundle using the –add-data flag.The Linux/macOS Command:bashpyinstaller –onefile –windowed –add-data “ui/app-window.slint:ui” main.py Vær forsigtig med at bruge denne kode.(Note: On Linux, use a colon : to separate the source and destination paths. On Windows later, you will change that colon to a semicolon ;).The Catch: On macOS, a single-file binary (–onefile) has to unpack itself into a temporary directory every time it boots. This introduces a noticeable startup delay which clashes with Slint’s goal of being a lightweight, lightning-fast UI.Option 2: Nuitka (Best for Production & Speed)If you want your app to feel truly native, use Nuitka. It translates your Python script into C and builds a highly optimized binary. It matches Slint’s high-performance design philosophy perfectly.Why it’s better for Slint: Because Nuitka compiles the code rather than zipping it, your app will launch instantly on Linux and macOS without any unpacked runtime lag.The Linux/macOS Command:bashpython3 -m nuitka –onefile –plugin-enable=data-files –include-data-files=ui/app-window.slint=ui/ main.py Vær forsigtig med at bruge denne kode.The Catch: Compilation takes significantly longer, and you must have system build tools (like gcc on Linux or Xcode tools on macOS) installed on your development machine.Cross-Platform Architecture StrategyTo make sure your Slint + Python configuration scales gracefully from Linux/macOS to Windows later, structure your project with these three constraints in mind:1. Compile on the Target OSYou cannot cross-compile a single Python binary. You must run your compilation command on an actual Linux machine for Linux, a Mac for macOS, and a Windows machine for Windows. Set up GitHub Actions or a CI/CD pipeline early to automate building on all three environments.2. Manage App Windowing FlagsOn Linux/Windows, utilities use a background terminal unless you hide it.On macOS, your app must be bundled as an .app folder structure rather than a raw command-line executable so users can click it in the Applications folder. PyInstaller handles this natively via the –windowed flag, creating a proper macOS app bundle.3. Use Dynamic Pathing for .slint FilesWhen bundled into a single file binary, your app runs from a temporary directory (sys._MEIPASS in PyInstaller). Hardcoded paths like “ui/app-window.slint” will break. Always resolve your UI file paths dynamically inside your main.py:python

import os
import sys
import slint

# Dynamically locate the UI folder regardless of freezer/compiler used
if hasattr(sys, '_MEIPASS'):
    # PyInstaller path
    base_path = sys._MEIPASS 
elif '__compiled__' in globals():
    # Nuitka path
    base_path = os.path.dirname(__file__) 
else:
    # Standard development path
    base_path = os.path.dirname(os.path.abspath(__file__))

slint_file_path = os.path.join(base_path, "ui", "app-window.slint")
ui = slint.load_file(slint_file_path)

Recommendation

Start with PyInstaller to quickly test your Slint layout and ensure the Python business logic hooks work as expected. Once you are ready to optimize application startup performance for your Linux and macOS users, switch your deployment pipeline over to Nuitka.To help tailor the build script, are you planning to use GitHub Actions for automation, or do you have standard Linux/macOS hardware ready for testing?