← All posts

Python code formatters

Let these tools automate style formatting for your Python code.

Header image
By Sanket on 
Share on Twitter Share on LinkedIn Share on Facebook

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:

  • Black
  • isort
  • autopep8
  • YAPF

Why use code formatters for Python?

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

Inspired from tools from other ecosystems like gofmt for Go and Prettier for JavaScript, Black has gradually become the de-facto code formatter for Python projects. It touts itself as uncompromising, opinionated, fast, and deterministic — which has propelled its usage amongst developers who don’t want to think about style, yet want to follow a consistent style guide.

Black is used by some very popular open-source projects, such as pytest, tox, Pyramid, Django Channels, Poetry, and so on.

How to use Black?

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.

black {source_file_or_directory}

isort

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:

    • Standard library imports.
    • Related third party imports.
    • Local application/library specific imports.
  • 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.

How to use isort?

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

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.

How to use autopep8?

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

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.

How to use YAPF?

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.

yapf <filename>

How to use code formatters with DeepSource?

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.

About DeepSource
DeepSource helps you automatically find and fix issues in your code during code reviews, such as bug risks, anti-patterns, performance issues, and security flaws. It takes less than 5 minutes to set up with your Bitbucket, GitHub, or GitLab account. It works for Python, Go, Ruby, and JavaScript.
Get started for free

Keep reading...