Python: Format Code

By Xah Lee. Date: . Last updated: .

Black (python code formatter)

Install

pip install 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

yapf

Install

pip install 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

Format python code in emacs (emacs integration)