How to use black, flake8, and isort to format Python Code

10 min readAug 3, 2021

black: The Uncompromising Code Formatter

Black can format Python code from version 2.7 to 3.8 (as of version 20.8b1). It’s a versatile tool and a good alternative to YAPF, which is limited by the Python version it’s used with.

My preference is using PEP 8 as my style guide, and so, 79-characters per line of code is what I use. So it’s as simple as running the following code at the root of my project and all non-compliant files will be reformatted:

$ black --line-length 79 --target-version py27 .

Let’s explain each option.

  • -l or --line-length: How many characters per line to allow. [default: 88]
  • -t or --target-version: Python versions that should be supported by Black's output. [default: per-file auto-detection]

Fairly simple. Allow 79 characters per line, and use py27 as the targetted version.

isort: A Python library to sort imports.

And just as their slogan states: “isort your imports, so you don’t have to.”


$ isort --multi-line 3 --profile black --python-version 27 .

The options used are mainly to be compatible with black (see here):

  • --multi-line: Multiline output (0-grid, 1-vertical, 2-hanging, 3-vert-hanging, 4-vert-grid, 5-vert-grid-grouped…