LinuxCommandLibrary

autopep8

Automatically format Python code to conform to PEP 8

TLDR

Format a file to stdout, with a custom maximum line length

$ autopep8 [path/to/file.py] --max-line-length [length]
copy

Format a file, displaying a diff of the changes
$ autopep8 --diff [path/to/file]
copy

Format a file in-place and save the changes
$ autopep8 --in-place [path/to/file.py]
copy

Recursively format all files in a directory in-place and save changes
$ autopep8 --in-place --recursive [path/to/directory]
copy

SYNOPSIS

autopep8 [options] [files | dirs]

PARAMETERS

-h, --help
    Show help message and exit.

-v, --verbose
    Print verbose progress output.

-q, --quiet
    Print less output.

-d, --diff
    Print diffs of changes instead of modifying files.

-i, --in-place
    Modify files in place.

-r, --recursive
    Recursively process directories.

-j N, --jobs=N
    Run N parallel jobs (only for stdin or multiple files).

-p N, --peppercorns=N
    Apply N fix passes (default 2).

-a, --aggressive
    Enable one aggressive fix pass.

--aggressive-level N
    Set aggressive fix passes (0-N).

--max-line-length N
    Set max line length (default 79).

--indent-size N
    Set indentation size (default 4).

--experimental
    Enable experimental fixes.

--select CODE
    Fix only specific error codes (e.g., E4,W).

--ignore CODE
    Ignore specific error codes (e.g., E501).

--match PATTERN
    Select files by regex pattern.

--exclude PATTERN
    Exclude files by regex pattern.

--list-fixes
    List all fixes available.

--global-config PATH
    Use global pycodestyle config file.

--config PATH
    Use specific config file.

--in-place-ignore CODE
    Ignore codes when in-place editing.

--diff-ignore CODE
    Ignore codes when printing diffs.

--exit-code
    Exit with code 2 if fixes made, 1 if unchanged.

--line-range START [END]
    Fix only lines in range.

-E, --error-summary
    Print error summary at end.

DESCRIPTION

autopep8 is a command-line tool that automatically formats Python code to conform to the PEP 8 style guide. It uses the pycodestyle library (formerly pep8) to detect style violations and applies fixes where possible, saving developers time on manual reformatting.

The tool intelligently wraps lines, sorts imports, removes unnecessary spaces, and fixes other common issues. It supports both single files and recursive directory processing. While not perfect—it cannot fix every PEP 8 violation—it's highly effective for most cases, promoting consistent code style in projects.

Ideal for use in CI/CD pipelines, pre-commit hooks, or interactively via editors like Vim or VS Code with plugins. It preserves code functionality and is safe for most codebases, though aggressive modes may introduce changes requiring review.

CAVEATS

May not fix all PEP 8 issues; aggressive modes can alter logic. Always review changes. Requires Python and pycodestyle installed.

INSTALLATION

pip install autopep8 or pipx install autopep8. Depends on Python 3.6+.

EXAMPLE

autopep8 --in-place --aggressive file.py
autopep8 --diff --recursive src/

EDITOR INTEGRATION

Supports Vim (ale), Emacs, VS Code plugins for on-save formatting.

HISTORY

Developed by Anthony Sottile starting 2012. Inspired by pep8 tool. Actively maintained; latest versions integrate with modern Python formatters.

SEE ALSO

pycodestyle(1), black(1), yapf(1)

Copied to clipboard