Automate linting & formatting in PyCharm with your favourite tools
Run black, flake8 & other tools automatically with every file change
If you’ve ever worked in a team, you know that to achieve code and style consistency, you need to agree on a formatter and linter. It will help you with onboarding new members to the codebase, create fewer merge conflicts and generally save time because developers don’t have to care about formatting and style while coding.
Before we dive into how to make your PyCharm run your favourite tools with every file change automatically, let’s cover a few basics.
Formatters
Formatters are tools that reformat some basic things in your code like quotes, commas, and line length. They make sure your codebase stays in a consistent style without any manual work on your end.
Popular Formatters: black, autopep8, yapf
Linters
These tools perform static analysis of source code, checking for semantic discrepancies. So “Linting” means running a tool against your code that identifies violations of style guides. The tool will check your code syntax and provide message that tell you what’s wrong with it.
Popular linters: flake8, pylint
In this blog article I will focus on black, isort and flake8. However, other tools such as mypy (static type checker) can be used in the exact same way. So here’s a short introduction into these tools:
Black
A popular auto formatter is black, “the uncompromising code formatter”. The great part about this tool is:
Blackened code looks the same regardless of the project you’re reading.
It is very opinionated but that’s actually good thing. Because not having many options makes you not discuss the options at all. It’s the basis of many projects (pytest, django etc.) and formats the code to look nicely without being too strict. You can compare its mindset to the formatter prettier for JavaScript.
Here’s an example:

Black is a great formatter but it does not everything what you’d expect. While black also formats your imports, it does not automate sorting them for you. Here is an example of imports that violates the style guide for Python code PEP8:

isort
Here comes isort to the rescue. It “helps to sort and format imports in your Python code.” While Black also formats imports, isort takes care of sorting them correctly, pep8-compliant:

Because there could be conflicts between these two tools, it is advised to use the black profile in isort. Just add .isort.cfg with this content in your projects root:
















