Python Minifier
Python-minifier transforms Python source code into its most compact representation, primarily by removing docstrings, comments, blank lines, and minimizing indentation. It is often used for optimizing code for deployment in size-constrained environments, such as AWS Lambda functions. The library is actively maintained, with frequent updates to support new Python versions, currently up to 3.14.
Common errors
-
UnicodeEncodeError: 'charmap' codec can't encode characters in position X-Y: character maps to <undefined>
cause The default system encoding (e.g., CP1252 on Windows) is being used to write output, but the minified code contains characters not representable in that encoding (e.g., non-ASCII characters, specific Python syntax characters).fixWhen using the `pyminify` command-line tool, ensure your terminal is configured for UTF-8 or redirect output to a file with explicit UTF-8 encoding. When using the `minify` function programmatically and writing to a file, open the file with `encoding='utf-8'` (e.g., `with open('output.py', 'w', encoding='utf-8') as f:`). -
SyntaxError: invalid syntax (or other runtime errors) in minified code
cause The Python interpreter used to run `python-minifier` is a different version (often newer) than the Python interpreter intended to run the minified code. `python-minifier` produces code compatible with its *own* runtime interpreter version.fixRun `python-minifier` (or the `pyminify` command) with the same Python interpreter version that will execute the minified code. For example, if your target environment is Python 3.8, activate a Python 3.8 virtual environment before running `pyminify`. -
Minified code is not significantly smaller or retains docstrings/comments
cause The default minification options might not be aggressive enough, or specific transforms that remove docstrings or other elements were not explicitly enabled.fixReview the available options for `pyminify --help` or the `minify` function arguments. For example, to ensure docstrings are removed, use `minify(source_code, remove_literal_statements=True)`.
Warnings
- breaking Python-minifier uses the currently running Python interpreter for parsing source code. The output will be compatible with the interpreter it was run with, which may differ from the source code's original target version. Minifying code written for Python 3.11 with `python-minifier` running on Python 3.12 may result in code only runnable on Python 3.12.
- gotcha The `--remove-debug` transform can sometimes incorrectly remove `if` branches that do not specifically test `__debug__`. While a fix was implemented in 3.1.1, aggressive debug removal should be tested carefully.
- gotcha Obfuscation features (e.g., `--obfuscate`, `--obfuscate-variables`) can make the resulting code 'SERIOUSLY hard-to-read', hindering debugging or future modifications. Using `--nonlatin` for obfuscation can exacerbate this.
- gotcha When minifying code containing non-ASCII characters or if your system's default encoding is not UTF-8 (common on Windows), you might encounter `UnicodeEncodeError` when writing the output.
Install
-
pip install python-minifier
Imports
- minify
import pyminifier; pyminifier.minify(...)
from python_minifier import minify
Quickstart
from python_minifier import minify
source_code = """
def my_function(a, b):
"""This is a docstring for a function."""
# This is a comment that will be removed
result = a + b
if result > 10:
return result
return None
"""
minified_code = minify(source_code)
print(minified_code)
# Example output (may vary slightly depending on version and options):
# def my_function(a,b):result=a+b;if result>10:return result