They solve different problems. Minification removes whitespace, comments, and docstrings (and can shorten variable names) to make a file smaller and faster to load — the code stays fully readable and your string literals stay in plaintext, so it is not protection. Obfuscation rewrites the code — renaming identifiers and encrypting strings — to make it hard to reverse; it protects your logic and secrets, but makes the file *bigger*, not smaller. Use a minifier to ship less code; use an obfuscator to hide it.
"Minify" and "obfuscate" get used interchangeably, and they shouldn't be — they pull in opposite directions. One makes your code smaller and leaves it perfectly readable; the other makes it unreadable and, as a side effect, larger. Mixing them up leads people to minify a script thinking they've protected it (they haven't) or to obfuscate an asset they only needed to shrink.
So I ran the same file through both and measured what actually changed: file size, whether a hard-coded secret survives, whether it's still readable, and whether it still runs. All tests on Python 3.14 with python-minifier 3.2.0 and our own obfuscator.
The test file
A small licensing helper — it has comments and a docstring for the minifier to strip, and a hard-coded secret to see whether either transform hides it:
"""Licensing helper for the demo app."""import hashlib# The product's license secret — must never be exposed to users.LICENSE_SALT = "salt-9f83-demo"def make_key(user_id: int, plan: str) -> str:"""Return a deterministic license key for a user and plan."""raw = f"{user_id}:{plan}:{LICENSE_SALT}"return hashlib.sha256(raw.encode()).hexdigest()[:16]def verify(user_id, plan, key):# Recompute and compare.return make_key(user_id, plan) == key
Minification: smaller, but just as readable
Minification strips everything the interpreter doesn't need — comments, docstrings, blank lines, and extra whitespace — and collapses statements onto shared lines. Here's the file minified (no renaming):
import hashlibLICENSE_SALT='salt-9f83-demo'def make_key(user_id,plan):raw=f"{user_id}:{plan}:{LICENSE_SALT}";return hashlib.sha256(raw.encode()).hexdigest()[:16]def verify(user_id,plan,key):return make_key(user_id,plan)==key
It dropped from 584 to 316 bytes — 46% smaller — and still runs identically. But read it: the license salt 'salt-9f83-demo' is right there, the function names are intact, and the logic is trivially followable. Minification made the file *denser*, not *hidden*.
That's the whole point of minification: it's a size and performance optimisation. Smaller files download faster and parse a hair quicker. It was never meant to conceal anything.
"But it can rename variables — isn't that obfuscation?"
This is where the confusion starts. Minifiers *can* also shorten identifiers, which looks like obfuscation. I re-ran it with global and local renaming on:
C='pro'import hashlib as DE='salt-9f83-demo'def A(user_id,plan):A=f"{user_id}:{plan}:{E}";return D.sha256(A.encode()).hexdigest()[:16]def F(user_id,plan,key):return A(user_id,plan)==key
Smaller still (258 bytes, 56% off), and now make_key is A, LICENSE_SALT is E. But look at what renaming can't touch: E='salt-9f83-demo' — the secret is still sitting there in plaintext. Renaming hides the *names* you chose, which were never the sensitive part. Your string literals — keys, tokens, URLs — pass straight through.
Renaming-minification is the trap people fall into: it *looks* obfuscated, so they ship a secret thinking it's hidden. A plain text search of the output still finds every literal.
Obfuscation: unreadable, and larger
Obfuscation is aimed at the opposite goal — making the code hard to understand — so it does the things minification won't: it encrypts the string literals and restructures the program, on top of renaming. I ran the same file through our Python Obfuscator:
$ obfuscate app.py --pro# 584 bytes -> 33,259 bytes (+5,595%)# secret 'salt-9f83-demo' : NOT found in output# still runs: valid: True
Note the direction of the size change: the file got ~57× bigger, because encryption and the decode machinery add real bytes. The trade is the whole point — a text search for the salt now finds nothing, and the output is unreadable, but it still runs and produces the same key. Obfuscation buys secrecy at the cost of size; minification buys size at the cost of nothing (because it protects nothing).
Side by side
| Original | Minified | Minified + renamed | Obfuscated | |
|---|---|---|---|---|
| Size | 584 B | 316 B (−46%) | 258 B (−56%) | 33,259 B (+5,595%) |
| Secret in plaintext? | Yes | Yes | Yes | No |
| Human-readable? | Yes | Yes | Names gone, logic clear | No |
| Still runs? | Yes | Yes | Yes | Yes |
| Purpose | — | Size / speed | Size / speed | Protection |
Which one do you actually want?
Pick by goal, not by which word you heard first:
- You want a smaller, faster-loading file (shipping code to a browser, trimming a package, a size-limited environment) → minify. Try our free Python Minifier. Keep the readable source in version control; ship the minified copy.
- You want to hide logic or make reverse-engineering costly → obfuscate. Use the Python Obfuscator, which renames at the AST level *and* encrypts strings — the part minification skips.
- You want to hide a real secret (an API key, a license authority) → do neither in isolation. As the tests show, a hard-coded secret survives minification and is only *scrambled* by obfuscation (a running program still reconstructs it). Keep it off the client entirely — see how to protect API keys in Python.
And if you're weighing obfuscation seriously, read is Python obfuscation secure? and how to protect Python source code first — obfuscation raises the cost of reversing your code, it doesn't make it impossible.
Shrink your Python — free
Strip comments, docstrings and whitespace (and optionally rename) to ship a smaller file. In your browser, no signup.
Open the Python MinifierFree tools mentioned here
Frequently asked questions
What is the difference between minifying and obfuscating Python?
Minifying removes whitespace, comments, and docstrings (and can shorten variable names) to make the file smaller and faster to load — the code stays readable and string literals stay in plaintext. Obfuscating rewrites the code (renaming identifiers and encrypting strings) to make it hard to reverse-engineer, which protects logic and secrets but makes the file larger. Minification is for size; obfuscation is for protection.
Does minifying Python code protect it?
No. In my test, minification shrank the file by 46% but the license secret, the function logic, and the control flow were all still plainly readable — just more compact. Even minifying with variable renaming (56% smaller) left the string literal 'salt-9f83-demo' in plaintext. Minification is a size optimisation, not protection.
Does minifying make Python run faster?
Barely, if at all, at runtime — Python compiles to the same bytecode either way. The real win is a smaller file that downloads and parses slightly faster, which matters when you're shipping code over a network or into a size-constrained environment. For CPU speed, minification is the wrong tool.
Is obfuscated code bigger or smaller than the original?
Bigger — often dramatically. In my test the obfuscated output was ~57× larger than the source (584 bytes to 33,259) because string encryption and the runtime decode machinery add real bytes. That's the opposite of minification, which shrinks the file. If a tool claims to both obfuscate and shrink your code, it's mostly just minifying.
Should I minify or obfuscate my Python code?
Minify if your goal is a smaller, faster-loading file and you don't care who reads it. Obfuscate if your goal is to make the logic hard to reverse-engineer. If you're trying to hide an actual secret like an API key, do neither in isolation — keep the secret off the client (environment variable, backend, or secrets manager), because a running program reconstructs it regardless.