Minifying Python removes everything the interpreter doesn't need — comments, docstrings, and blank lines — and can optionally rename variables to single letters, shrinking the file without changing what it does. In a real test, stripping comments and whitespace cut a script 36%, and full minification (with renaming) cut it 42% — with identical output. Do it in your browser with the free Python Minifier, or the python-minifier library. One caveat: minifying makes code *smaller*, not *faster*, and it is not obfuscation — your strings and logic are still recoverable.
Minifying Python means shrinking the source to the fewest bytes that still run: strip the comments, docstrings, and blank lines, collapse indentation, and — if you push it — rename every variable to a single letter. The program behaves exactly the same; it's just smaller and unreadable.
It's useful when file size actually matters: embedding a script inside another file, squeezing into a size-limited field, or shipping fewer bytes. But it's widely misunderstood — people expect it to speed code up or protect it, and it does neither. Here's exactly what it does, tested on a real script, and where the free tools fit.
What minifying actually removes
Python ignores comments, docstrings, and blank lines at runtime, and it doesn't care whether your variables are named total_area or A. Minifiers exploit exactly that. Here's a small, readable script we'll shrink:
import mathdef circle_area(radius):"""Return the area of a circle with the given radius."""# multiply pi by r squaredresult = math.pi * radius ** 2return resultdata = [("small", 1.0), ("medium", 2.5), ("large", 4.0)]for name, r in data:print(f"{name}: {circle_area(r):.2f}")
Method 1 — the online minifier (no install)
The quickest path: paste your script into the free Python Minifier. It runs in your browser (nothing uploaded), strips the non-essentials, and hands back a compact, still-runnable script you can copy. Good when you just need a smaller file once and don't want to install anything.
Method 2 — the python-minifier library (tested)
For scripting or a build step, the python-minifier package does the same job with fine-grained control:
import python_minifiersrc = open('circles.py').read()# Safe: strip comments, docstrings, blank lines — keep namessmall = python_minifier.minify(src, rename_locals=False, rename_globals=False)# Max: also rename identifiers to single letterstiny = python_minifier.minify(src, rename_locals=True, rename_globals=True)open('circles.min.py', 'w').write(tiny)
Here are the actual sizes I measured on the script above:
| Version | Size | Reduction |
|---|---|---|
| Original | 470 bytes | — |
| Comments + whitespace stripped | 301 bytes | −36% |
| Full minify (rename identifiers) | 273 bytes | −42% |
And the fully-minified output — same program, renamed and crushed onto few lines:
import mathdef A(radius):'Return the area of a circle with the given radius.';return math.pi*radius**2for(B,C)in[("small",1.),("medium",2.5),("large",4.)]:print(f"{B}: {A(C):.2f}")
I verified behavior is preserved by running the original and the fully-minified version and comparing their output — identical. That's the guarantee a good minifier gives you: fewer bytes, same results. Always run your tests against the minified file before shipping it, though.
Minifying is not obfuscation (and not encryption)
This is the trap. Minified code *looks* scrambled, so people assume it's protected. It isn't — every string literal is still sitting there in plain text, and a formatter re-expands the structure in one click. Watch a "secret" survive full minification:
python_minifier.minify('API_KEY = "sk-live-9f83"\nprint(API_KEY)', rename_globals=True)# -> A='sk-live-9f83'\nprint(A)# 'sk-live-9f83' still present: True
Minification renames the *variable* but leaves the *value* — sk-live-9f83 is right there. If your goal is protecting logic or hiding secrets, you need real obfuscation (which encrypts string literals), not minification. See minification vs obfuscation for the full breakdown.
When to minify — and the myth to drop
The honest use cases and the honest limits:
- Do minify to reduce file size — embedding a script in another file, fitting a size-limited field, or shipping fewer bytes over the wire.
- Don't expect a speed-up. Python compiles to bytecode before running, and whitespace and variable names don't survive into that bytecode — so a minified script runs at the *same* speed. Minification saves bytes on disk, not CPU cycles.
- Don't treat it as protection — it's fully reversible and leaks every string.
- Keep your real source in version control and minify as a build step; never hand-edit minified code.
If it's raw size you care about and the file is only *transported*, gzipping it usually beats minifying (and stacks with it). Minify when the Python itself must stay small in its final form.
Minify your Python — free
Paste a script and get a smaller, still-runnable version in your browser. Nothing uploaded, no signup.
Open the Python MinifierFree tools mentioned here
Related guides
Frequently asked questions
How do I minify Python code?
Strip the parts Python ignores at runtime — comments, docstrings, and blank lines — and optionally rename identifiers to single letters. Use an online Python minifier (paste and copy), or the python-minifier library: python_minifier.minify(source). In a real test, this cut a script by 36–42% with identical output.
Does minifying Python make it run faster?
No. Python compiles source to bytecode before executing, and comments, whitespace, and variable names don't carry into that bytecode — so a minified script runs at the same speed as the original. Minification reduces file size on disk, not execution time.
Does minifying Python break my code?
A good minifier preserves behavior — it only removes things the interpreter ignores and renames variables consistently. In testing, the original and fully-minified versions produced identical output. Still, always run your test suite against the minified file before shipping, especially if you use dynamic features like eval or introspection on names.
Is minifying the same as obfuscating Python?
No. Minifying shrinks the file and makes it hard to read, but it's fully reversible (a formatter re-expands it) and leaves all string literals in plain text — including API keys. Obfuscation additionally renames for confusion and encrypts strings so secrets and logic aren't recoverable. Minify for size; obfuscate for protection.
What is the best Python minifier?
The python-minifier library is the most capable for scripting and build steps, with options to strip docstrings and rename locals/globals. For a one-off with no install, an online Python minifier that runs in your browser is fastest. Both preserve behavior; choose based on whether you need automation or a quick paste-and-copy.