Skip to main content
Reverse Engineering

Does PyInstaller Protect Python Source Code? The Truth About Python EXE Files

By Mithun··9 min read
Quick Answer

No. PyInstaller bundles your compiled bytecode plus the Python interpreter into one executable so it *runs* without Python installed, but it doesn't obfuscate or encrypt anything (PyInstaller 6 even removed its old --key option). I retested on 24 October 2026 with PyInstaller 6.22.3 on Python 3.14: PyInstaller's own archive reader pulled my script out of the exe, and our decompiler rebuilt the exact original source, API key included, in 2 seconds. To protect source, obfuscate or compile it *before* packaging — and if you obfuscate, add PyInstaller's --hidden-import flags or the exe will crash.

This is one of the most common — and most costly — misunderstandings in Python distribution. People ship a PyInstaller .exe believing the source is now safe inside an opaque binary. It isn't, and I want to show exactly why with a real build, not assertions.

Here's what an .exe actually contains, and how quickly your code comes back out. Everything below is from a rerun on 24 October 2026 with PyInstaller 6.22.3 and Python 3.14.2 on Windows.

What PyInstaller actually does

PyInstaller is a packager. It answers one question — "how do I run this on a machine without Python installed?" — and it answers it well. The pipeline:

your_script.py
| (Python compiles it)
v
.pyc bytecode
| (PyInstaller bundles)
v
EXE = [ bootloader + python3xx.dll + your .pyc files + libraries ]
what happens to your code

Notice what's *not* in that pipeline: no obfuscation, no encryption. Your .pyc files are placed inside the executable as-is. The exe is bigger and self-contained — my 10-line test app came to 9.2 MB as a --onefile exe and ran standalone — but the bytecode inside is untouched. It's zlib-compressed, so searching the .exe for your API key finds nothing, which is exactly why it *feels* safe. It isn't.

Proof: extracting the code back out

My test app had a hard-coded API key (sk-live-4f9a2c7e1b) and a license code. After building it, I didn't need any hacking tool: PyInstaller ships its own archive viewer, and it lists your script right inside the exe:

Contents of 'myapp.exe' (PKG/CArchive):
position, length, uncompressed_length, is_compressed, typecode, name
0, 233, 291, 1, 'm', 'struct'
233, 2897, 5038, 1, 'm', 'pyimod01_archive'
...
23409, 395, 472, 1, 's', 'app'
23804, 59417, 120400, 1, 'b', 'VCRUNTIME140.dll'
...
pyi-archive_viewer --list myapp.exe — real output (excerpt)

The entry marked 's' named app is my script — 395 compressed bytes. PyInstaller's own Python API reads it back out, and the code object inside lists every string:

import sys, marshal, importlib.util
from PyInstaller.archive.readers import CArchiveReader
exe, name, out = sys.argv[1], sys.argv[2], sys.argv[3]
arch = CArchiveReader(exe)
data = arch.extract(name) # PyInstaller's own reader
code = marshal.loads(data) # the entry script's code object
def strings(c):
for k in c.co_consts:
if isinstance(k, str):
yield k
elif hasattr(k, "co_consts"):
yield from strings(k)
print("entry script:", code.co_filename)
print("strings:", list(strings(code)))
# Re-wrap as a normal .pyc (16-byte header) so any decompiler can read it.
with open(out, "wb") as f:
f.write(importlib.util.MAGIC_NUMBER + b"\0" * 12 + data)
print("wrote", out)
extract_app.py — uses only PyInstaller's own reader
> python extract_app.py myapp.exe app app.pyc
entry script: app.py
strings: ['sk-live-4f9a2c7e1b', 'PRO-2026-XYZ', 'licensed', 'trial mode', '__main__', 'status:', '| key ends']
wrote app.pyc
Real output

Then I dropped that app.pyc into our free .pyc Decompiler. It came back in 2 seconds, every function verified, identical to what I wrote:

# Decompiled with https://pyobfuscate.dev
API_KEY = 'sk-live-4f9a2c7e1b'
LICENSE = 'PRO-2026-XYZ'
def check(code):
if code == LICENSE:
return 'licensed'
return 'trial mode'
if __name__ == '__main__':
print('status:', check('PRO-2026-XYZ'), '| key ends', API_KEY[-4:])
Decompiled from the exe — real output

The popular third-party tool pyinstxtractor does the same job in one command and is what most people reach for; I used PyInstaller's own reader here to show that you don't even need it. --onedir builds are no different: the _internal/ folder holds python314.dll and the standard library (base_library.zip), and your own script sits compressed inside the .exe, where the same reader gets it.

PyInstaller won't encrypt it for you anymore. Older versions had a --key option that encrypted bundled bytecode (and stored the key in the exe). PyInstaller 6 removed it — pyinstaller --help on 6.22.3 has no --key at all.

The markers are unmistakable too: strings like _MEIPASS and a bundled python3xx.dll instantly identify any file as a PyInstaller-packed Python app — telling an analyst exactly which tool to reach for.

Packaging ≠ obfuscation

This is the mental model to keep: packaging and protection are different problems.

Packaging (PyInstaller)Protection (obfuscate / compile)
GoalRun without Python installedMake source hard to recover
Your bytecodeBundled as-isRewritten or removed
Strings/secretsIn plaintextEncrypted (obfuscation)
Reversible?Yes — pyinstxtractorMuch harder

PyInstaller is excellent at its job — it's just not the job you thought. (For how it compares to a true compiler, see PyInstaller vs Nuitka.)

How to actually protect a Python exe

The fix is order of operations: protect the source first, then package it. Now even after someone unpacks the exe and decompiles the bytecode, what they recover is already obfuscated.

  1. Obfuscate the source with our Python Obfuscator — rename identifiers and encrypt string literals so the extracted .pyc gives up nothing readable.
  2. Or compile the sensitive modules to a native .pyd/.so with Cython — then there's no bytecode in those modules to decompile at all.
  3. Then run PyInstaller on the protected code to get your distributable exe.
  4. And keep real secrets off the client — load API keys from the environment or a server, never hard-code them. See protecting API keys in Python.

I did exactly that with the same test app: obfuscated it with our engine, then packaged it. PyInstaller's reader still extracted the script, but the API key and license code were gone from its strings — only meaningless names were left.

The gotcha: obfuscated code + PyInstaller can crash with `No module named 'hmac'`. Our obfuscator hides imports, so some modules are only loaded at runtime, where PyInstaller's import scanner can't see them. My first protected build ran fine with python app.py but the exe failed with ModuleNotFoundError: No module named 'hmac'.

The fix is to name those modules explicitly. I obfuscated the same app with 20 different seeds and logged what each one loads at runtime: hmac, hashlib, base64, binascii and struct every time, plus one compression codec that varies by build (bz2 in 10 builds, lzma in 5, zlib in 5). This command covers all of them; both a bz2 and an lzma build ran correctly with it:

pyinstaller --onefile --hidden-import hmac --hidden-import bz2 --hidden-import lzma --hidden-import zlib app.py
Packaging obfuscated code with PyInstaller — tested

See the full walk-through in convert Python to EXE and protect it. PyInstaller stays in your toolchain — it just goes *last*, after the protection.

Protect your code before you package it

A PyInstaller exe unpacks straight back to your bytecode. Obfuscate your source first — rename, encrypt strings, flatten flow — so the extracted .pyc gives up nothing. Free, in your browser.

Open the Python Obfuscator

Free tools mentioned here

Related guides

Frequently asked questions

Does PyInstaller protect or hide my source code?

No. PyInstaller bundles your compiled .pyc bytecode and the Python interpreter into one executable so it runs without Python installed — but it does not obfuscate or encrypt anything. Tools like pyinstxtractor unpack the exe back into .pyc files, which decompile to near-original source and leak string literals.

Can a PyInstaller exe be decompiled?

Yes. In my October 2026 test (PyInstaller 6.22.3, Python 3.14), PyInstaller's own archive reader extracted the script from the exe and a .pyc decompiler rebuilt the exact original source, API key included, in 2 seconds. The third-party pyinstxtractor does the same extraction in one command. The exe is packaging, not protection.

What is pyinstxtractor?

pyinstxtractor is a widely-available tool that reads a PyInstaller executable and writes out every bundled .pyc file and resource. It's the standard way people recover code from a PyInstaller exe, which is why packaging alone provides no source protection.

How do I make a PyInstaller exe that actually protects my code?

Protect the source before packaging: obfuscate it (rename identifiers, encrypt strings) or compile the sensitive modules to a native .pyd/.so with Cython, then run PyInstaller on the protected code. That way, even after someone extracts and decompiles the bundled bytecode, they only recover the obfuscated version. Also keep real secrets off the client.

Is --onefile more secure than --onedir?

No, both are equally recoverable. --onefile unpacks itself to a temp folder (_MEIPASS) at runtime; --onedir keeps python314.dll and the standard library in _internal/ and your own script compressed inside the .exe. Either way PyInstaller's own reader (or pyinstxtractor) recovers your bytecode. The packaging mode doesn't affect protection.

Why does my obfuscated PyInstaller exe say 'No module named hmac'?

Obfuscation hides imports, so modules loaded at runtime are invisible to PyInstaller's import scanner and get left out of the exe. Add them explicitly: pyinstaller --onefile --hidden-import hmac --hidden-import bz2 --hidden-import lzma --hidden-import zlib app.py. In my test across 20 obfuscated builds that covered every module the code loads at runtime.

Does PyInstaller still have the --key encryption option?

No. PyInstaller 6 removed the --key bytecode-encryption option (it was weak anyway: the key had to be stored inside the exe). PyInstaller 6.22.3 has no --key flag. Protect the source before packaging instead.

Keep reading