python-minifier
Transform Python source code into it's most compact representation
What it is and what it does
python-minifier rewrites Python source code into a functionally identical but much smaller form by collapsing whitespace, shortening variable names, and simplifying syntax. It was originally built to embed AWS Lambda functions in CloudFormation templates, which have a 4 KiB size limit for inline code.
The tool works as both a command-line utility (pyminify) and a Python API. It supports Python 2.7 and Python 3.3 through 3.14, and the minified output is compatible with the same Python version that ran the minifier. There are no runtime dependencies, making installation straightforward.
Use it for:
- Embed Python Lambda functions in AWS CloudFormation templates under the 4 KiB inline code limit
- Reduce deployment package size for serverless functions or edge computing environments
- Obfuscate source code for distribution when combined with other techniques
- Minimize Python code for resource-constrained environments or IoT devices
- Compress utility scripts or one-off tools before transmission or storage
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Transforms Python source code into compact, functionally equivalent minified code by removing whitespace, renaming variables, and optimizing syntax.
Yes, if you need to shrink Python source code for deployment or embedding. The package is stable (Production/Stable status), has no dependencies, and carries no security vulnerabilities. The main caveat is that minified code runs only on the Python version that performed the minification, so test carefully if you minify on a different version than your target runtime.
Install
python-minifier on PyPI
pip
pip install python-minifieruv
uv add python-minifierpoetry
poetry add python-minifierInstalling python-minifier
Before you install
Low friction: pure Python wheels for both Python 2 and 3, no runtime dependencies. Repository is active with recent commits and 718 stars; last release 226 days ago.
License in practice
MIT license is permissive; you can use, modify, and distribute this package freely in commercial and private projects with minimal restrictions.
Quickstart
pip install python-minifier
import python_minifier
with open('hello.py') as f:
minified = python_minifier.minify(f.read())
print(minified)
The minifier's output is compatible only with the Python version running the minifier itself; minifying Python 3.11 code with a Python 3.12 interpreter produces Python 3.12-only output.
Verify before relying
- Whether minified code preserves all runtime semantics (e.g., introspection, dynamic imports, metaclass behavior)
- Performance overhead of the minification process itself on large codebases
- Compatibility guarantees when minifying code written for one Python version using an interpreter running a different version
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (!=3.0.*,!=3.1.*,!=3.2.*,<3.15,>=2.7) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 226 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 271,756/month — #8,219 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: python_minifier-3.2.0-py2-none-any.whl; python_minifier-3.2.0-py3-none-any.whl
Keywords: minify, minifier
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
minify-htmlMinifies HTML, JavaScript, and CSS in a single…
permissive · top 5,000 on PyPI
jsminMinifies JavaScript code by removing…
permissive · top 5,000 on PyPI
JSON_minifyMinifies JSON by removing whitespace and…
permissive · top 15,000 on PyPI
htmlmin2Minifies HTML by removing unnecessary…
permissive · top 5,000 on PyPI
mkdocs-minify-pluginMinifies HTML, JavaScript, and CSS files in…
permissive · top 5,000 on PyPI
rjsminrjsmin minifies JavaScript code by removing…
permissive · top 5,000 on PyPI
Flask-MinifyFlask extension that automatically minifies…
permissive · top 15,000 on PyPI
django-htmlminMinifies HTML by removing whitespace and…
permissive · top 5,000 on PyPI
htmlminMinifies HTML by removing unnecessary…
permissive · top 5,000 on PyPI
csscompressorCompresses CSS by removing unnecessary…
permissive · top 5,000 on PyPI