Python: Auto Format Code

By Xah Lee. Date: . Last updated: .

the 2 best python auto formatters are yapf and black

install:

pip install yapf
pip install black

yapf usage:

# print help
yapf -h

# print diff
yapf -d filename.py

# change file in-place
yapf -i filename.py

# pep8 style
yapf -i filename.py

# google style
yapf -i --style google filename.py

# print style help
yapf --style-help

black usage:

# print help
black -h

# print diff
black -diff filename.py

# change file in-place
black filename.py

# take input from stdin
black - -q

Format python code in emacs (emacs integration)