← back to blogposts

5 Favorite Python Code Formatters


Python, unlike some other languages (I'm looking at you, JavaScript!), has an official style-guide for writing code. If you want to prevent other programmers, who read your code, from thinking all kinds of things, you should stick to that. But don't worry, there are several tools that aim to help you achieve that.

Note: These are all great, but everything here applies to either new projects or projects that already have their code formatted in PEP8. Do not mix code styles in running projects, even if you wanted to a lot.


I've heard a lot of things about Black, but one of them more often: "Black is a must!". And I agree. There is nothing easier than letting someone do the hard work, right? No, really, Black has grown a lot since its birth and a huge portion of Python projects leverages it.

Black makes code review faster by producing the smallest diffs possible. Blackened code looks the same regardless of the project you’re reading. Formatting becomes transparent after a while and you can focus on the content instead.


iSort is focused on sorting and grouping your import statements alphabetically, and automatically separates them into sections and by type. It provides a command line utility, Python library and plugins for various editors to quickly sort all your imports. It requires Python 3.6+ to run but supports formatting Python 2 code too. Black + iSort is a great and commonly used combo, even though Black has its own import formatting algorithm.


autopep8 is another unofficial tool that automatically formates Python code to conform to PEP 8. It uses pycodestyle, Python’s official PEP 8 violation checker tool. 


YAPF is literally Yet Another Python Formatter, but it works in kind of a differrent way. Most of the formatters do not change anything in case the code already conforms PEP8. YAPF says it does not mean the code looks good, though. That said, it will format your code in such way it will still conform to the official code-style, but it will look nicer, at least according to YAPF.


Pylint, besides enforcing a coding standard, focuses on checking for errors in Python code and looks for code smells. It can also look for certain type errors, recommend suggestions about how particular blocks can be refactored and can offer you details about the code's complexity.

Pylint will warn you about missing docstrings, unused arguments, some logical errors, redundancies, and much more. A big plus is that you can create plugins for Pylint. What's even better, a lot of them already exist, e.g. pylint-django, which aims to give you more accurate result for Django code.


Now it's up to you! Choose what suits you the most, nevertheless you should have a slight awareness of each of the most popular formatters, as you could encounter them at any time. One of the most popular combos is Black for code formatting + iSort for import formatting + Pylint for finding errors.

Add a comment
You can include your email if you want a response from the author.