How to turn a Python script into an exe
PyInstaller and Nuitka both turn a .py file into a program people can run without installing Python. Here's how to use each, which one to pick, the problems you'll hit along the way, and what's left to do if you're shipping the result to customers.
To turn a Python script into an .exe, most people need two commands:
pip install -U pyinstaller
pyinstaller --onefile yourscript.py
The result is dist\yourscript.exe, a single file that runs on a Windows computer with no Python installed. Leave out --onefile and you get a folder with the executable and its files in it, which starts faster and is easier to debug.
That's PyInstaller, the tool most people start with. The main alternative is Nuitka, which compiles your code to C. Everything below was checked against each project's own documentation, repository and PyPI page on November 22, 2026.
The tools
| What it does | Latest release | Status | |
|---|---|---|---|
| PyInstaller | Bundles your script, its dependencies and a Python interpreter | 6.22.3, September 2026 | Active |
| Nuitka | Compiles Python to C, then to a native executable | 4.3.5, November 2026 | Active |
| auto-py-to-exe | A graphical window on top of PyInstaller | 2.50.1, April 2026 | Active |
| cx_Freeze | Bundles a script into a folder, or an MSI installer | 8.7.4, October 2026 | Active |
| Briefcase | Packages a project as a native app with an installer | 0.4.7, October 2026 | Active, beta |
| py2exe | Windows-only bundler | 0.14.2.0, June 2026 | Active, no single-file builds on Python 3.12 or later |
| py2app | macOS-only, builds .app bundles |
0.28.10, February 2026 | Active |
| PyOxidizer | Embeds Python in a single Rust-built binary | 0.24.0, December 2022 | No release since 2022, supports Python 3.8 to 3.10 only |
The short version:
- You want an exe today: PyInstaller
- You'd rather click than type: auto-py-to-exe, which describes itself as a ".py to .exe converter using a simple graphical interface and PyInstaller"
- You want compiled code that's harder to read back, and don't mind longer builds: Nuitka
- You're starting a new GUI app and want an installer at the end: Briefcase
- Don't start on PyOxidizer. Its author wrote in 2024 that the project is "effectively in a zombie state"
Python's own documentation doesn't pick one. Its FAQ lists Nuitka, PyInstaller, PyOxidizer, cx_Freeze, py2app and py2exe side by side.
PyInstaller, step by step
PyInstaller describes what it does this way: it "bundles a Python application and all its dependencies into a single package", and the user "can run the packaged app without installing a Python interpreter". Your code isn't compiled. A copy of the interpreter travels with it.
Install and build
pip install -U pyinstaller
pyinstaller yourscript.py
PyInstaller supports Python 3.8 and newer. Your app ends up in the dist folder. If the pyinstaller command isn't found, python -m PyInstaller yourscript.py does the same thing.
One folder or one file
| One folder (default) | One file (--onefile) |
|
|---|---|---|
| What you get | A folder with the executable and an _internal folder next to it |
A single executable |
| Startup | Faster | Slower: it unpacks itself first |
| To share it | Zip the folder, or wrap it in an installer | Send the file |
| Debugging | Easier | Harder |
A one-file executable isn't magic. Each time it starts, it unpacks everything into a temporary folder named _MEIxxxxxx, runs from there, and deletes the folder when it exits. PyInstaller's documentation says that's why a one-file app "is a little slower to start", and that the folder "is not removed if the program crashes or is killed".
Its advice is to get the one-folder build working first: "make sure your app works correctly when bundled to one folder."
Hide the console window
A GUI app shouldn't open a black console window behind it. Add --windowed:
pyinstaller --onefile --windowed yourscript.py
--noconsole is the same option under another name. On macOS it also makes PyInstaller build an .app bundle.
Add an icon
pyinstaller --onefile --windowed --icon=app.ico yourscript.py
Windows wants an .ico file and macOS wants an .icns. If you give PyInstaller another image format, it tries to convert it, but only if Pillow is installed. It's simpler to hand it the right file: the ICO converter makes one from a PNG or SVG, and the PNG to ICNS converter makes the Mac one.
Include data files
PyInstaller follows your import statements. It doesn't know about the images, templates or config files your code opens at runtime, so you list those yourself:
pyinstaller --add-data "src/README.txt:." yourscript.py
The format is source:destination, where . means the top level of the bundle. The colon works on every platform, Windows included, since PyInstaller 6.0. Older tutorials use a semicolon on Windows. That still works, and the changelog calls it discouraged.
Then find the file relative to your script, not relative to wherever the user launched the program from:
from pathlib import Path
readme = Path(__file__).parent / "README.txt"
PyInstaller's documentation calls __file__ "the preferred way" to locate bundled files. If you need to know whether you're running bundled at all, it sets two attributes you can check:
import sys
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
print('running in a PyInstaller bundle')
sys._MEIPASS is the bundle folder: the _internal folder in a one-folder build, or the temporary folder in a one-file build. Anything a one-file app writes there is gone when it exits, so save user data somewhere else.
Nuitka, step by step
Nuitka takes a different route. In its own words it "translates the Python modules into a C level program", which a C compiler then turns into an executable. Your Python code ends up as machine code, not as bytecode files next to an interpreter.
What you need
A C compiler. Nuitka's manual asks for one "with support for C11":
- Windows: Visual Studio 2022 or higher. If you don't have it, Nuitka offers to download a MinGW64 compiler on the first build. The manual notes that MinGW64 "does not work with Python 3.13 or higher" without an experimental option
- macOS: the system Clang compiler, which comes with Xcode
- Linux: the system GCC, or Clang
The manual also warns against the Microsoft Store version of Python: "it doesn't work properly". Use the installer from python.org.
Install and build
python -m pip install Nuitka
python -m nuitka --mode=standalone yourscript.py
That produces a yourscript.dist folder you can copy to another machine. For a single file:
python -m nuitka --mode=onefile yourscript.py
The --mode option matters. Without it, Nuitka builds in "accelerated" mode, and that result still depends on the Python installed on your machine, so it won't run anywhere else. Older guides show --standalone and --onefile as separate flags. The current tutorial uses --mode=standalone and --mode=onefile.
Like PyInstaller, Nuitka recommends getting the folder build working before the single file, because problems such as missing data files "are easier to diagnose and fix in standalone mode". A onefile build is "a single binary, that extracts itself on the target, before running the program".
The options you'll want
python -m nuitka --mode=onefile ^
--windows-console-mode=disable ^
--windows-icon-from-ico=app.ico ^
--include-data-files=src/README.txt=README.txt ^
yourscript.py
--windows-console-mode=disableis Nuitka's version of--windowed--windows-icon-from-icosets the icon. On macOS it's--macos-app-icon, and--mode=appbuilds an.appbundle--include-data-filesand--include-data-diradd files, written assource=destination. For data that belongs to an installed package, the documentation prefers--include-package-data
Nuitka doesn't set sys.frozen. To detect a compiled build, check for the __compiled__ attribute.
The license, and Nuitka Commercial
Nuitka is licensed under the AGPL version 3, with a runtime exception that exists so you can compile proprietary programs without the result falling under the AGPL. In practice: you can sell what you build with it.
There's also a paid edition. Nuitka Commercial is €250 a year, and a "Full Package" with better support is €400 a year. It adds protection for the constants and data files inside your program, encrypted tracebacks, support for deploying to older versions of Windows, and automatic updates, which its page says are currently for onefile mode. If you cancel, you keep using the version you had.
PyInstaller vs Nuitka
| PyInstaller | Nuitka | |
|---|---|---|
| How it works | Bundles your bytecode with an interpreter | Compiles your code to C, then to machine code |
| Needs a C compiler | No | Yes |
| Build time | Short | Longer, since it's compiling C |
| Single-file output | --onefile |
--mode=onefile |
| Your source code | Shipped as .pyc bytecode, which can be decompiled |
Compiled. Strings and constants are still readable |
| Detect at runtime | sys.frozen and sys._MEIPASS |
__compiled__ |
| Cross-compiling | No | No |
| License | GPL 2.0 with an exception that lets you ship under any license | AGPL 3 with a runtime exception for compiled programs |
| Paid edition | None | Nuitka Commercial, from €250 a year |
Is Nuitka faster at runtime? Its homepage says it can "boost your program runtime and launch performance". The benchmark table on its performance page is for Python 2.7 only, so there's no official figure for current Python to quote. Measure your own program before you choose it for speed.
A reasonable path: start with PyInstaller because it's quicker to get working. Move to Nuitka if you need the code to be harder to read back, or if your own measurements show it's worth the longer builds.
Can I build a Windows exe on a Mac or on Linux?
No. Neither tool cross-compiles.
PyInstaller's documentation is direct: "it is not a cross-compiler; to make a Windows app you run PyInstaller on Windows". Nuitka's says its architecture option "is not for cross compilation, which requires a Python of the target architecture anyway". cx_Freeze says the same about itself: "it only makes an executable that runs on that platform".
So a Windows exe is built on Windows, a Mac app on a Mac, and a Linux binary on Linux. If you don't own all three, a virtual machine or a hosted build service with one runner per operating system does the job. Nuitka's manual points to its own GitHub Action for that.
Two related limits: a PyInstaller build made on a current Linux distribution "may fail to execute" on an older one, and a Mac build works on the macOS release you built on and later ones. Build on the oldest system you plan to support.
Why antivirus flags your exe
This is the problem that surprises people most. You build a harmless script, send it to a friend, and their antivirus deletes it.
It happens because every PyInstaller executable starts with the same small launcher, and malware authors use PyInstaller too. Antivirus products learn to distrust the launcher. Your file is new, unsigned and looks like the others.
What the projects say:
- PyInstaller won't take bug reports about it. Its issue template reads: "False positives should instead be reported to the vendors of the offending antivirus software", and adds that "there is no magic
--i-am-not-malwareoption". It notes that--windowedbuilds get flagged more often, and that this isn't a bug - Nuitka says some vendors "may flag compile binaries using Nuitka's default settings on Windows as malware". Its documented answer is to buy Nuitka Commercial and follow the instructions that come with it, adding that "there are no guarantees"
What you can do:
- Report the false positive to the antivirus vendor. PyInstaller calls this the correct way, and Microsoft has a submission page for Defender
- Sign the executable with a code signing certificate. PyInstaller's template is honest that "this is not guaranteed to fix AV false positives", but a signature at least tells Windows and your users who published the file
- Rebuild PyInstaller's bootloader yourself. The template says this "can make applications better accepted", because your launcher no longer matches the common one
Signing is also what deals with the separate "Windows protected your PC" warning from SmartScreen. Microsoft says that even a signed new file can show it, and that building a reputation "can take several weeks and hundreds of clean installs". The pages on SignTool, Azure Trusted Signing and EV code signing certificates cover how to sign and what it costs.
Making the exe smaller
A bundled Python app is big because it carries an interpreter and every library it found. The documented ways to trim it:
- Exclude what you don't use. In PyInstaller,
--exclude-module nameleaves a module out - Build from a clean environment with Nuitka. Its documentation says to use a minimal virtualenv, because it compiles whatever is installed and reachable. It also has an anti-bloat plugin with options such as
--noinclude-pytest-mode=nofollowand--noinclude-setuptools-mode=nofollow, for libraries that drag in test frameworks - UPX compression, with care. PyInstaller can compress binaries with UPX, which its documentation says is "currently used only on Windows". The same page warns that UPX "may end up corrupting a collected shared library", and that UPX-compressed files can't be code signed on macOS.
--noupxturns it off
PyInstaller also has a --strip option, which its own documentation says is "not recommended for Windows".
Common errors and their fixes
"Failed to execute script". The app crashed at startup and, with --windowed, there's no console to show why. PyInstaller's advice is to "re-bundle your application without the --windowed option" and run it from a command prompt, so you can read the real error.
ModuleNotFoundError or ImportError in the built app, but not from source. That's a hidden import: a module loaded through __import__(), importlib.import_module() or similar, which PyInstaller's analysis can't see. Its documentation warns there are "no warnings, only an ImportError at run-time". Name the module with --hidden-import modulename. In Nuitka the equivalents are --include-module and --include-package.
FileNotFoundError for a file that exists on your machine. The data file wasn't bundled, or your code looks for it relative to the working directory. Add it with --add-data in PyInstaller, or --include-data-files in Nuitka, and locate it relative to __file__.
A Nuitka app with no console shows nothing when it fails. Its documentation says to remove --windows-console-mode=disable while you debug.
The exe works on your machine and nowhere else (Nuitka). You built in the default accelerated mode. Use --mode=standalone or --mode=onefile.
Does it hide your source code?
Not with PyInstaller. Its documentation says the bundle contains compiled .pyc files, and that these "could in principle be decompiled to reveal the logic of your code". PyInstaller used to have a --key option that encrypted the bytecode. It was removed in version 6.0.0, in September 2023. The maintainers' reasoning was that it "was only ever a mild deterrent", and that it misled people into thinking a build was a safe place for things like API keys.
Nuitka does better, because there's no bytecode to extract. Its own commercial page is careful about the limit: "Compiling with Nuitka protects the source code", but constants such as strings are "plain and readable in the compiled programs". Hiding those is one of the things the paid edition adds.
Either way, treat anything inside a program you hand to someone as readable by them. Don't ship secrets such as API keys or database passwords in it. If your goal is to make sure only paying customers use the app, that's a licensing job, covered below.
Building a Mac app
On macOS, add --windowed and PyInstaller builds an .app bundle. It recommends against combining that with --onefile: a one-file executable inside an app bundle "is not recommended".
Signing isn't optional on a Mac. On Apple Silicon, code has to be signed even if only ad hoc, and PyInstaller ad-hoc signs what it collects by default. To distribute outside your own machine you sign with your Developer ID:
pyinstaller --windowed --codesign-identity "Developer ID Application: Your Name" yourscript.py
PyInstaller then turns on the hardened runtime, which needs a real Apple-issued certificate. Entitlements go in with --osx-entitlements-file. Nuitka's equivalents are --macos-sign-identity and --macos-sign-notarization, which prepares the signature for notarization.
After signing, Apple requires notarization for software distributed with a Developer ID. That's a separate upload to Apple that neither tool's documentation walks through. If customers see a message that your app "is damaged", the page on the "app is damaged" error explains what broke, and DMG vs PKG covers what to put the app in.
After the build: selling and updating your app
An exe in a dist folder is a build. A product needs a few more things.
- An installer, if you want Start menu entries and an uninstaller. Briefcase and cx_Freeze can produce an MSI, and the page on Inno Setup and NSIS covers the usual installers for a PyInstaller folder
- Updates. None of the free tools replace an installed app with a newer version. The guide to updating a Python desktop app compares the options, including Velopack
- Payments and license keys, if it's for sale. How to sell software online walks through the pieces
LicenseSeat handles the licensing and update part for apps sold from your own website. Each purchase creates a license key, your app activates it on the customer's computer and checks it at startup, and the update feed serves new versions only to customers whose license covers them.
To be straight about the fit for Python: there's no official Python SDK yet. Activation and validation are plain HTTPS calls, which requests handles in a few lines, and the licensing API guide describes them. LicenseSeat charges a flat monthly fee and takes 0% of your revenue.
Common questions about converting Python to exe
How do I convert a .py file to an .exe?
Install PyInstaller with pip install -U pyinstaller, then run pyinstaller --onefile yourscript.py on Windows. The executable appears in the dist folder and runs without Python installed.
Is PyInstaller safe?
Yes. It's an open source project that has been maintained for many years, and it only packages the code you give it. The antivirus warnings people see come from the way its executables are built, not from anything harmful in the tool.
Why does my antivirus flag my exe?
PyInstaller and Nuitka executables share startup code with every other program built by the same tool, including malware, and a new unsigned file has no reputation. Report the false positive to the antivirus vendor and sign your executable.
Should I use PyInstaller or Nuitka?
Start with PyInstaller: it needs no compiler and builds quickly. Choose Nuitka when you want your code compiled instead of shipped as bytecode, and you can accept longer builds and a C compiler on your build machine.
Can I make a Windows exe on a Mac?
No. PyInstaller, Nuitka and cx_Freeze all build only for the operating system they're running on. Use a Windows machine, a virtual machine, or a hosted build runner.
Does the person running my exe need Python installed?
No. The interpreter and your dependencies are inside the build. That's also why the file is large.
Why is my exe so big?
It contains a Python interpreter plus every library your script imports, and sometimes libraries it doesn't. Exclude unused modules, and with Nuitka build from an environment that has only what the app needs.
Can someone get my source code back from the exe?
From a PyInstaller build, largely yes: the bytecode can be extracted and decompiled. A Nuitka build is compiled, which is much harder to reverse, though strings in it are still readable.
What's the difference between auto-py-to-exe and PyInstaller?
auto-py-to-exe is a graphical front end. You pick your script and options in a window, and it runs PyInstaller for you. The output is the same.