LinuxCommandLibrary

autopep8

Automatically format Python code to PEP 8 style

TLDR

Format Python file

$ autopep8 --in-place [file.py]
copy
Format with aggressive fixes
$ autopep8 --in-place --aggressive --aggressive [file.py]
copy
Show diff instead of modifying
$ autopep8 --diff [file.py]
copy
Format recursively
$ autopep8 --in-place --recursive [directory/]
copy
Select specific fixes
$ autopep8 --select=[E501,W293] --in-place [file.py]
copy

SYNOPSIS

autopep8 [--in-place] [--aggressive] [options] files

DESCRIPTION

autopep8 automatically formats Python code to conform to PEP 8 style guide. It fixes issues like whitespace, indentation, and line length while preserving code semantics.
The tool integrates with editors and CI pipelines to enforce consistent Python style.

PARAMETERS

-i, --in-place

Modify files in place
-d, --diff
Print diff instead of modifying
-r, --recursive
Process directories recursively
-a, --aggressive
More aggressive fixes (repeatable for more aggression)
--select=errors
Fix only specified errors
--ignore=errors
Ignore specified errors
--max-line-length=n
Maximum line length (default: 79)
--range start end
Only fix lines in range
--exclude patterns
Exclude file patterns

CONFIGURATION

setup.cfg

Project-level configuration under `[pycodestyle]` section.
pyproject.toml
Project-level configuration under `[tool.autopep8]` section.
~/.config/pycodestyle
User-level default configuration for pycodestyle rules.

ERROR CODES

Based on PEP 8:
- E: Errors
- W: Warnings
- Examples: E501 (line too long), W291 (trailing whitespace)

CAVEATS

May conflict with other formatters like black. Aggressive mode can change code logic in rare cases. Review changes before committing. Black is now more popular for opinionated formatting.

HISTORY

autopep8 was created by Hideo Hattori to provide automatic PEP 8 compliance, released around 2010.

SEE ALSO

black(1), isort(1), flake8(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community