autopep8

2.3.2 · active · verified Thu Apr 09

autopep8 is a tool that automatically formats Python code to conform to the PEP 8 style guide. It fixes most PEP 8 issues that can be automatically fixed. Currently at version 2.3.2, it maintains an active release schedule with several updates per year addressing bug fixes, new Python version support, and rule enhancements.

Warnings

Install

Imports

Quickstart

Demonstrates programmatic use of `autopep8.fix_code` with default and custom options. It also mentions the common command-line interface usage.

import autopep8

# Sample code with PEP8 violations
source_code = """
def   my_func ( arg1 ,  arg2 ):
    if (True): # E712 comparison to True
        return arg1+arg2
"""

print("Original code:")
print(source_code)

# Fix the code with default options
# An empty dictionary for options applies default autopep8 behavior.
fixed_code = autopep8.fix_code(source_code, options={})
print("\nFixed code (default options):")
print(fixed_code)

# Fix with specific options, e.g., higher aggressiveness
# Note: Certain fixes (like E712) may require higher aggressive levels.
options_aggressive = {'aggressive': 1}
fixed_code_aggressive = autopep8.fix_code(source_code, options=options_aggressive)
print("\nFixed code (aggressive=1):")
print(fixed_code_aggressive)

# Command line usage example (run in your shell):
# echo "def   my_func ( arg1 ,  arg2 ):\n    return arg1+arg2" > example.py
# autopep8 --in-place example.py
# cat example.py # Will show the formatted code

view raw JSON →