Auto-Formatting Python Code with Black and Isort

Maulik Parmar
3 min readJan 7, 2021

Writing code in good format is an important skill to have. Especially, when we are writing long codes, proper formatting helps in debugging and understanding code. If you are writing a small code, you can get away without formatting the code. But as code length increases, without formatting, it becomes difficult to understand the code.

Pros of formatting python code:

(1) Better readability.
(2) It makes it easy to debug code.
(3) Very helpful for team projects as everyone is using the same style so it’s easy to work together.

Black:

As mentioned in the project readme, “ Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters. You will save time and mental energy for more important matters.

Black can reformat your entire file in place according to the “Black” code style.

Installing Black:

Black can be installed by running pip install black. It requires Python 3.6.0+ to run but you can reformat Python 2 code with it, too.

Running Black:

Here is an example raw code “test.py” we want to format in Black style format.

#!/usr/bin/env python
# coding: utf-8
import numpy as npimport sys
from numpy import arange,argmax
def addition(a , b):
ans=a+ b

return ans
def subtraction(a , b):
ans=a - b
return ans
a = arange(5)print(a)print(argmax(a))

To auto-format our code in black format with default options, you can run:

$ black test.py

The output of this will be in place and the reformatted code looks like below:

#!/usr/bin/env python
# coding: utf-8
import numpy as npimport sys
from numpy import arange, argmax
def addition(a, b):
ans = a + b
return ansdef subtraction(a, b):
ans = a - b
return ans
a = arange(5)print(a)print(argmax(a))

The Python code is now reformatted and it’s more readable.

Isort:

As mentioned in the package description, “ isort is a Python utility / library to sort imports alphabetically, and automatically separated 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 doesn’t provide auto-formatting of ‘import’ statements. So, we use “isort” on top of the “Black” auto-formator to format ‘import’ statements.

To auto-format the code using “isort” with default options, you can run:

$ isort test.py

The output of this will be in place and the reformatted code looks like below:

#!/usr/bin/env python
# coding: utf-8
import sysimport numpy as np
from numpy import arange, argmax
def addition(a, b):
ans = a + b
return ansdef subtraction(a, b):
ans = a - b
return ans
a = arange(5)print(a)print(argmax(a))

Editor Integration:

You can integrate Black with your favorite editors. Currently Black supports PyCharm/IntelliJ IDEA, Wing IDE, Vim, Visual Studio Code, Sublime Text 3, and Atom. Follow the instruction here to integrate Black with your favorite editor.

If you enjoyed the article please share the story or comment if you have any doubts.

--

--