Registry / devops / python-minifier

python-minifier

JSON →
library3.2.0pypypi✓ verified 88d ago

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.

pip install python-minifier
INSTALL
IMPORT
SIG · PYTHON-MINIFIER
P
python-minifier
devopspythonv3.2.0
Install
1.6s avg
Import
22ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v3.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.025s · 18.4MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 1.6s · import 0.020s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

minify
✓ from python_minifier import minify
✗ import pyminifier; pyminifier.minify(...)
The PyPI package is 'python-minifier' but the primary module is 'python_minifier'.

Minify a string of Python source code using the `minify` function.

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
python-minifier --version
Debug
Known issues
breakingPython-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.
fix
Ensure `python-minifier` is run using the exact Python interpreter version targeted for the minified code's execution environment. For example, if deploying to a Python 3.8 environment, use a Python 3.8 interpreter to run `python-minifier`.
affects: All versions
gotchaThe `--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.
fix
Thoroughly test minified code, especially conditional logic, when using `--remove-debug`. Consider using `--no-remove-debug` if unexpected behavior occurs.
affects: <=3.1.0
gotchaObfuscation 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.
fix
Use obfuscation features with extreme caution. Always retain a clear, un-obfuscated version of your source code for maintenance and debugging.
affects: All versions with obfuscation options
gotchaWhen 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.
fix
Explicitly specify UTF-8 encoding when writing minified output to a file or set the `PYTHONIOENCODING` environment variable to `UTF-8` for the `pyminify` command line tool.
affects: All versions, particularly on Windows
Errors
Common errors & fixes
UnicodeEncodeError: 'charmap' codec can't encode characters in position X-Y: character maps to <undefined>
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).
fix
When 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
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.
fix
Run `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
The default minification options might not be aggressive enough, or specific transforms that remove docstrings or other elements were not explicitly enabled.
fix
Review 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)`.
Upgrade
Version history
3.2.0latest on PyPI · released Dec 31, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
python-minifier — pip install python-minifier · libregistry