A versatile command-line tool for stripping, cleaning, and minifying Python code. Useful for sending lengthy code to an LLM that has token limits.
- Remove docstrings, comments, and empty lines
- Minify whitespace and compress code
- Strip or preserve essential imports
- Remove type hints, error handling, assertions, decorators
- Replace function and class bodies with pass
- Shorten variable names
- Convert simple functions to one-liners
- Compress multiline strings into a single line
--extreme– Apply all stripping options--moderate– Clean while preserving readability--structure-only– Keep only function/class signatures--safe- Minimal cleanup for LLM-friendly code
You can either paste code in directly or provide an input file:
python python-code-minifier.py --strip-comments --minify-code
Just paste your code into the terminal and press Ctrl+D (or Ctrl+Z on Windows) to run.
python python-code-minifier.py --file input.py --output clean.py --extreme
--count– Show character count before and after--output– Save to a file instead of printing to stdout
Original:
def greet(name: str) -> None:
"""Say hello"""
print(f"Hello, {name}") # Greet user
With --extreme:
def greet(name):print(f"Hello,{name}")