Minify Python code safely

This Python minifier makes source files smaller by removing comments, blank lines, extra spaces, and optionally docstrings, and by shrinking indentation to a single space or tab per level. In Python, whitespace is syntax, so the tool reads your code the way the Python interpreter does: line by line, tracking every indentation level, bracket, and string. It changes only what Python ignores.

Everything runs in your browser, so your code is never uploaded.

How to use it

  1. Paste your code, open a .py file, or drag one onto the tool.
  2. Choose an indentation style. One space is smallest, and four spaces stays readable.
  3. Decide whether to remove docstrings.
  4. Copy the result or download it as a .py file.

What gets removed

Before:

# Calculate a discount
def price(total, rate=0.1):
    """Return the discounted total."""
    if total > 100:   # big orders
        return total * (1 - rate)
    return total

After, with docstrings removed:

def price(total,rate=0.1):
 if total>100:
  return total*(1-rate)
 return total
Removed or shortenedKept exactly
Comments and blank linesEvery name, keyword, number, string, and operator
Spaces around operators and after commasThe block structure: which lines belong to which if, for, def, or class
Indentation, shrunk to one space or tab per levelThe shebang line (#!/usr/bin/env python3) and encoding declaration
Line breaks inside brackets and after backslashesMulti-line strings, f-strings, and raw strings, character for character
Docstrings (optional)A pass is added when a docstring was a function's only statement

A space is kept wherever removing it would change the code. For example, r "x" must not become the raw string r"x", and 1 .real must not become 1.real.

Tested against Python itself

Every result is checked in your browser: the minified code is read back and compared, token by token and block by block, with the original. The green Verified badge means they match. Before release, the minifier was run on every file in Python 3.12's standard library and 5,000 files from popular packages, about 80 MB of real-world code. Python's own parser produced exactly the same syntax tree for every original and minified file, with docstrings kept and with them removed. The tests included modern syntax like match statements, the walrus operator, type parameters, and Python 3.12 f-strings that reuse quotes inside expressions.

When to minify Python

  • Embedded and microcontroller projects: MicroPython and CircuitPython boards have very little storage, and smaller source files load faster.
  • Serverless and packaged code: smaller deployment packages for AWS Lambda layers and similar platforms.
  • Pasting code into limited fields: config tools, notebooks, and forms with length limits.

For ordinary applications there's little benefit, since Python compiles source to bytecode anyway. Keep your original, readable files under version control and minify only copies.

Frequently asked questions

Will my code run the same?

Yes. Only whitespace, comments, and optionally docstrings change, and the result is verified token by token. Docstring removal can matter if your code reads __doc__, for example with argparse.ArgumentParser(description=__doc__), so it's off by default.

Is my code uploaded?

No. Parsing and minifying happen entirely in your browser.

Does it rename variables or obfuscate code?

No. Renaming can break code that uses names dynamically, through getattr, **kwargs, or imports from other files. This tool only removes what Python ignores. Minified code is still readable with a code formatter.

Which Python versions are supported?

Python 3, up to 3.12 syntax, including f-strings with nested quotes, type parameter lists, and type statements. The output runs on whatever version the original ran on.

How do I make minified code readable again?

Run it through a formatter like Black or Ruff (ruff format file.py). They restore standard spacing and indentation. Comments and docstrings that were removed can't be recovered, so keep your original.

Our Full Dev Tool Library

Flipper File