Python Script Minifier
Minify Python with python-minifier: strip comments, rename variables, and optionally remove docstrings, annotations and asserts, hoist literals and combine imports — with per-option toggles and a live size readout.
Why use this tool?
Use the Minifier to shrink scripts for size-constrained targets — MicroPython on an ESP32 or Raspberry Pi Pico, AWS Lambda and other serverless bundles where cold-start size matters, or anywhere you ship less code over the wire. By default it strips comments, renames local variables and removes type annotations; docstring removal, global renaming and assert removal are opt-in toggles, so you control how aggressive it gets.
It's also a light first line of defense: renaming and removing docstrings makes a script noticeably harder for a casual reader to follow. For real intellectual-property protection, though, run it through the AST obfuscator instead — minification is about size, not secrecy.
Selling your Python App?
Don't let your software be pirated. With Licers.com, you can easily issue license keys, bind them to devices (HWID), and instantly validate licenses natively in Python.
- ✓ Free forever plan available
- ✓ Simple Python SDK integration
- ✓ Stop piracy instantly
Tool facts
- Supported Python
- Python 3.x
- Input limit
- Limited only by your device
- Last reviewed
- 2026-08-24
What it can't do
- •Protect your source — it shrinks and renames, but the logic, control flow and string literals are all still there (use the AST obfuscator for real protection).
- •Guarantee identical behavior with every option — renaming globals can break other modules that import those names, and removing annotations or asserts can change code that relies on them. Review aggressive settings before shipping.
About Python Script Minifier
The Python Minifier (powered by python-minifier) reduces a script's on-disk size by removing what the interpreter doesn't need and shortening names. By default it strips comments, renames local variables to short names, and removes type annotations. You can additionally remove docstrings, remove asserts, rename global names, hoist repeated literals, and combine imports — each is a toggle with a live byte-count and percentage-saved readout. In constrained targets like MicroPython firmware or serverless bundles, every kilobyte counts.
Most of these transforms preserve behavior, but not all are risk-free, which is why the tool exposes them separately rather than promising a single "safe" mode. Renaming local variables and stripping comments are safe. Renaming global names is aggressive — if another module imports one of those names, the rename breaks it (that option is off by default and labeled accordingly). Removing type annotations changes code that reads __annotations__ at runtime (dataclasses, pydantic, typing-based dispatch). Removing asserts drops assertion checks entirely. Review the output — especially with the aggressive options enabled — before you ship it.
Minifying Python is different from minifying JavaScript. JavaScript minifiers rename everything and collapse the file onto one line; Python can't collapse lines because indentation is syntax — the structure of your loops and functions lives in the leading whitespace — so the tool preserves that structure exactly. It still renames identifiers and removes docstrings and annotations, which is where most of the savings come from, but you won't reach JavaScript's ratios because the structural whitespace has to stay.
Minification isn't protection. Renaming makes a script harder for a casual reader, but the logic, control flow and string literals are all still present, and short names can be re-expanded by re-formatting. If your goal is to make the logic genuinely hard to reverse, that's the job of the AST obfuscator — identifier renaming plus string encryption, import hiding and control-flow flattening — which you can layer on top of minification. Our minification vs obfuscation guide explains the difference, and how to obfuscate Python code covers real source protection.
A practical workflow is to keep your fully commented source in version control as the thing you actually maintain, and treat the minified file as a build artifact you regenerate on each release rather than edit by hand. If you're deploying to a device or function where size is the binding constraint, minify last — after formatting and testing — so the code you validated is the code you ship.
Frequently Asked Questions
Explore Other Tools
ASCII Art Generator
Convert basic text into stylish ASCII art blocks for terminal headers.
Jupyter Notebook Converter
Convert Jupyter (.ipynb) notebooks into standard (.py) scripts.
Jupyter Notebook Viewer
Open and view Jupyter .ipynb notebooks in your browser — markdown, code, and outputs — with no Python or Jupyter install.
Jupyter Notebook Output Cleaner
Strip saved outputs and execution counts from a Jupyter .ipynb — clean notebooks for git and smaller files, in your browser.