Skip to main content
Guides

Convert Python to EXE: A Real PyInstaller Walk-Through

By Mithun··10 min read
Quick Answer

To convert a Python script to a .exe, install PyInstaller and run pyinstaller --onefile your_script.py — the standalone executable lands in dist/. It bundles the Python interpreter plus your code, so it runs on machines with no Python installed. Two catches most guides skip: PyInstaller can't cross-compile, so a Windows .exe has to be built by a Windows Python (on Windows, or under Wine on Linux, which is how our online builder does it), and the .exe is not source protection — it unpacks back to bytecode and decompiles, so obfuscate first if the logic is valuable.

"How do I turn my Python script into an exe?" — I get why people ask. You wrote something useful, and you want to hand a client or a friend a single file they can double-click, without walking them through installing Python. That part genuinely works well.

But most "py to exe" tutorials stop at the happy path and skip the things that actually bite you. So I built a real .exe on this Windows machine with PyInstaller 6.22.3 (Python 3.14.2) — retested on 24 October 2026 — and wrote down exactly what happened: the command, the file sizes, measured startup times, how online builders get around the cross-compile limit, and the security reality.

The tool: PyInstaller

There are a few packagers (Nuitka, cx_Freeze, py2exe), but PyInstaller is the one most people mean by "py to exe" — though it's worth knowing how it stacks up against the alternatives (PyInstaller vs Nuitka). It works by bundling the CPython interpreter, your script's compiled bytecode, and every library it imports into one distributable. Install it with pip:

pip install pyinstaller
install

Here's the tiny script I packaged — a stand-in for a real CLI tool, with an import so the bundle has something to pull in:

import hashlib
import sys
def fingerprint(text):
return hashlib.sha256(text.encode()).hexdigest()[:16]
def main():
name = sys.argv[1] if len(sys.argv) > 1 else "world"
print(f"Hello, {name}!")
print("fingerprint:", fingerprint(name))
if __name__ == "__main__":
main()
greeter.py

Building the .exe (the real run)

One command does it. --onefile collapses everything into a single executable:

$ pyinstaller --onefile greeter.py
INFO: PyInstaller: 6.22.3, contrib hooks: 2026.9
INFO: Python: 3.14.2
INFO: Building PYZ (ZlibArchive) ... completed successfully.
INFO: Building PKG (CArchive) greeter.pkg completed successfully.
INFO: Building EXE from EXE-00.toc completed successfully.
$ dist\greeter.exe Mithun
Hello, Mithun!
fingerprint: 7f6c499c46b87f83
build + run (real output, trimmed)

That's the whole thing: a dist/greeter.exe that runs on a Windows box with no Python installed, producing the exact same output as the script. For a lot of use cases — a utility for a non-technical client, an internal tool, a game script — that's all you need.

--onefile vs --onedir (measure before you pick)

PyInstaller has two output modes, and the difference is real. I built the same script both ways and measured size and startup time (median of 7 runs; the plain python greeter.py took 58 ms):

ModeOutputSizeStartup (measured)
--onefileOne greeter.exe9.2 MB619 ms — self-extracts to a temp dir on every launch
--onedirgreeter_dir.exe + _internal/ folder20.4 MB total (2.1 MB launcher)93 ms — nothing to extract

--onefile is tidier to hand over (literally one file), but it unpacks itself into a temporary folder each time it runs — in my test it started about 6.6× slower than onedir — and that self-extraction can trip antivirus. --onedir is bulkier and "messier" (a folder of files) but launches faster and is easier to debug. For a double-click-and-share tool I use onefile; for something launched often, onedir.

Neither mode makes the app smaller than Python itself — you're shipping the whole interpreter. A 9.2 MB "Hello world" is normal, not a mistake.

The catch: no cross-compiling (and how online builders get around it)

PyInstaller cannot cross-compile. It packages the Python that runs it, so a Windows .exe must come from a *Windows* Python, a macOS app from macOS, a Linux binary from Linux. There's no flag that makes a Linux PyInstaller emit a Windows .exe.

There is one well-known way around it for Windows: run a real Windows Python under Wine on Linux, and let *that* PyInstaller do the build. That's how our Python to EXE tool produces real Windows executables online. I sent the same greeter.py through it on 24 October 2026:

TargetBuild timeSizeBuilt with
Windows .exe (onefile)127 s7.3 MBPython 3.12.7 + PyInstaller 6.22.2, under Wine
Linux binary (onefile)21 s7.1 MBPython 3.11.2 + PyInstaller 6.22.2

The downloaded greeter.exe ran on this Windows 11 machine and printed the same fingerprint: 7f6c499c46b87f83 as my local build. It's smaller than my local 9.2 MB build mostly because it bundles Python 3.12 rather than 3.14. macOS is the one target that still needs a real Mac, so for that the tool generates a GitHub Actions workflow that builds on GitHub's macOS runners.

A real Windows build under Wine took about two minutes in my test. If a website hands you a Windows .exe *instantly*, ask what it actually built.

Problems you will actually hit

  • Antivirus false positives. A self-extracting --onefile exe from an unknown publisher looks, to a heuristic scanner, a lot like malware. Unsigned exes get flagged. Code-signing or shipping --onedir reduces this.
  • Missing modules at runtime. PyInstaller finds imports by static analysis, so anything imported dynamically (importlib, plugins, some libraries) may be left out. The fix is --hidden-import modulename or a hook. Obfuscated code is a common case: in my test an obfuscated script crashed as an exe with No module named 'hmac' until I added --hidden-import hmac --hidden-import bz2 --hidden-import lzma --hidden-import zlib (our online builder adds these automatically for code from our obfuscator).
  • Data files not found. Bundled images/configs need --add-data, and your code must locate them via the sys._MEIPASS temp path at runtime, not a relative path.
  • Size. Every heavy dependency (pandas, numpy, PyQt) balloons the exe. --exclude-module and a clean virtualenv keep it down.

Reality check: an .exe is not source protection

This is the part that matters if your script has valuable logic or secrets. Packaging to .exe feels like it hides your code. It doesn't. I searched the real onefile exe I'd just built for tell-tale strings:

greeter -> FOUND
PyInstaller -> FOUND
_MEIPASS -> FOUND
python314 -> FOUND
grepping the .exe (real)

Those markers (_MEIPASS is PyInstaller's runtime-extraction path) instantly identify it as a PyInstaller-packed Python app. And in --onedir, the _internal/ folder literally contains python314.dll and base_library.zip sitting in the open. Your own script is compressed inside the .exe, but that doesn't help: in a separate test, PyInstaller's own archive reader pulled a script out of an exe and our decompiler rebuilt the exact source, API key included, in 2 seconds (full test). The third-party pyinstxtractor does the same extraction in one command.

Order of operations matters: an exe is a *packager*, not a protector. If the logic is worth protecting, obfuscate the source first, then package it. Doing it the other way round protects nothing.

That's the workflow we'd recommend: run your script through the Python Obfuscator (AST renaming, string encryption, and up to a fully encrypted wrapper), *then* build the .exe. Now even if someone unpacks the exe and decompiles the bytecode, what they get back is the obfuscated version — not your real code. (Remember the --hidden-import flags above when you package obfuscated code yourself.)

Build a real .exe from your Python — free

Paste your script and download a real Windows .exe or Linux binary built with PyInstaller — plus a GitHub Actions generator for macOS.

Open the Python to EXE tool

Free tools mentioned here

Related guides

Frequently asked questions

How do I convert a Python script to an .exe?

Install PyInstaller (pip install pyinstaller) and run 'pyinstaller --onefile your_script.py'. The standalone .exe appears in the dist/ folder and runs on Windows machines without Python installed. On this machine (PyInstaller 6.22.3, Python 3.14), that produced a working 9.2 MB executable from a small script.

Can I build a Windows .exe on Mac or Linux?

Not with a normal PyInstaller install — it can't cross-compile. A Windows .exe must be built by a Windows Python: on Windows, or under Wine on Linux (that's how our online builder makes real Windows exes; in my test it built one in about two minutes and it ran on Windows 11). A macOS app still needs a Mac, for example a GitHub Actions macOS runner.

Why is my Python .exe so large?

Because it bundles the entire Python interpreter and every imported library. Even a tiny script produces a 7-9 MB onefile exe (9.2 MB with Python 3.14 in my test); heavy dependencies like numpy or PyQt push it much higher. Use a clean virtualenv and --exclude-module to trim it.

Does turning Python into an .exe protect my source code?

No. A PyInstaller exe can be unpacked (e.g. with pyinstxtractor) back into .pyc bytecode, which decompiles to near-original source and leaks string literals. Packaging is distribution, not protection. Obfuscate the source first, then build the exe.

onefile or onedir — which should I use?

Use --onefile for a single shareable file (slower startup, more antivirus friction). Use --onedir for faster startup and easier debugging, at the cost of shipping a folder. In my test onefile was 9.2 MB and started in 619 ms; onedir was 20.4 MB total (2.1 MB launcher) and started in 93 ms.

Can an online tool build a real Windows .exe?

Yes, if it runs a real Windows Python — on a Windows machine or under Wine. Our Python to EXE tool builds Windows .exe files under Wine and Linux binaries natively: in my October 2026 test a small script built in 127 seconds for Windows and 21 seconds for Linux, and the .exe ran correctly on Windows 11.

Keep reading