{"id":8555,"library":"python-minifier","title":"Python Minifier","description":"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.","status":"active","version":"3.2.0","language":"en","source_language":"en","source_url":"https://github.com/dflook/python-minifier","tags":["minification","code optimization","obfuscation","code compression"],"install":[{"cmd":"pip install python-minifier","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The PyPI package is 'python-minifier' but the primary module is 'python_minifier'.","wrong":"import pyminifier; pyminifier.minify(...)","symbol":"minify","correct":"from python_minifier import minify"}],"quickstart":{"code":"from python_minifier import minify\n\nsource_code = \"\"\"\ndef my_function(a, b):\n    \"\"\"This is a docstring for a function.\"\"\"\n    # This is a comment that will be removed\n    result = a + b\n    if result > 10:\n        return result\n    return None\n\"\"\"\n\nminified_code = minify(source_code)\nprint(minified_code)\n# Example output (may vary slightly depending on version and options):\n# def my_function(a,b):result=a+b;if result>10:return result\n","lang":"python","description":"Minify a string of Python source code using the `minify` function."},"warnings":[{"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`.","message":"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.","severity":"breaking","affected_versions":"All versions"},{"fix":"Thoroughly test minified code, especially conditional logic, when using `--remove-debug`. Consider using `--no-remove-debug` if unexpected behavior occurs.","message":"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.","severity":"gotcha","affected_versions":"<=3.1.0"},{"fix":"Use obfuscation features with extreme caution. Always retain a clear, un-obfuscated version of your source code for maintenance and debugging.","message":"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.","severity":"gotcha","affected_versions":"All versions with obfuscation options"},{"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.","message":"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.","severity":"gotcha","affected_versions":"All versions, particularly on Windows"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"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:`).","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).","error":"UnicodeEncodeError: 'charmap' codec can't encode characters in position X-Y: character maps to <undefined>"},{"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`.","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.","error":"SyntaxError: invalid syntax (or other runtime errors) in minified code"},{"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)`.","cause":"The default minification options might not be aggressive enough, or specific transforms that remove docstrings or other elements were not explicitly enabled.","error":"Minified code is not significantly smaller or retains docstrings/comments"}]}