Python: Auto Format Code
the 2 best python auto formatters are yapf
and black
yapf
is written in python, algorithm based on clang-format. code owned by google and written by Bill Wendling (aka gwelymernans, morbo@google.com) [https://github.com/gwelymernans].black
is written in python by Łukasz Langa [https://lukasz.langa.pl/], a Python Core Dev.
yapf
support various styles. pep8, google, facebook, with many tweakings.black
is just one style.
yapf
is 522kblack
is 716k
yapf
repo is 937kblack
repo is 5,431k
black
does not support Python 2 since version 22.0 (released 2022 Jan).
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