Python Minifier

3.2.0 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

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

view raw JSON →