If you write code that other developers review or contribute to, chances are you’re already adhering to a style guide for writing code. In this post, we look at the most popular code formatters in Python and help you take a decision on which one you should adopt.
The most popular code formatters in Python are:
Following a style guide keeps the code’s aesthetics clean and improves readability, making contributions and code reviews easier. Automated code formatters make sure your codebase stays in a consistent style without any manual work on your end. If adhering to a specific style of coding is important to you, employing an automated to do that job is the obvious thing to do. This avoids bike-shedding on nitpicks during code reviews, saving you enormous amount of time overall.
Black is used by some very popular open-source projects, such as pytest, tox, Pyramid, Django Channels, Poetry, and so on.
Install the latest version of Black from PyPI:
pip install black
And then, run black on one or many files. On doing so, Black will format the files in-place.
PEP 8, Python’s official style guide, recommends the following rules for writing import statements:
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
Imports should be grouped in the following order:
You should put a blank line between each group of imports.
isort sorts imports in your Python files automatically — alphabetically, separated into sections, and by type. It ensures that the imports in a Python file follows the recommended PEP 8 guidelines.
Install the latest version of isort from PyPI:
pip install isort
And then, run isort on one or many files. On doing so, isort will format the files in-place.
isort . # runs recursively on the current working directory
autopep8 is an unofficial, yet popular, tool that automatically formates Python code to conform to PEP 8. It uses pycodestyle, Python’s official PEP 8 violation checker tool, to determine what parts of the code needs to be formatted.
Install the latest version of autopep8 from PyPI:
pip install autopep8
And then, run autopep8 on one or many files. On doing so, autopep8 will format the files in-place.
autopep8 --in-place <filename>
YAPF, or Yet Another Python Formatter, takes a different approach in formatting code than the other tools listed here. It works on the premise that code that conforms to the PEP 8 guidelines may not be re-formatted, but it doesn’t mean that the code looks good. So it’s algorithm takes the code and reformats it to the best formatting that conforms to the style guide, even if the original code didn’t violate the style guide. This idea is similar to tools like clang-format or gofmt.
Install the latest version of YAPF from PyPI:
pip install yapf
And then, run YAPF on one or many files. On doing so, YAPF will format the files in-place.
If you use DeepSource for static analysis and automated code reviews, you can use Transformers to automate running one or more of these code formatters in your
.deepsource.toml file. For instance:
[[transformers]] name = "black" enabled = true
The full list of Transformers is available in the docs.