VSCode: Using Black to automatically format Python

adamlombard

Adam Lombard

Posted on April 30, 2020

VSCode: Using Black to automatically format Python

Black is "the uncompromising Python code formatter." It can be configured to automatically format your code whenever you save a file in VSCode.

Install Black in your virtual environment:



$ pip install black


Enter fullscreen mode Exit fullscreen mode

Install Microsoft's Python extension in VSCode:

Microsoft's Python extension in VSCode

Open your VSCode settings, by going 'Code -> Preferences -> Settings'.

Search for "python formatting provider" and select "black" from the dropdown menu:

The VSCode settings interface for the

In the settings, search for "format on save" and enable the "Editor: Format on Save" option:
The VSCode settings interface for the

Black will now format your code whenever you save a *.py file.

Before saving:
Code before formatting

After saving:
Code after formatting


Was this helpful? Did I save you some time? For $1 you can...

🫖 Buy Me A Tea! ☕️


More: How to set Black line lengths in VSCode


Images from VSCode using Hyper Term Theme.

💖 💪 🙅 🚩
adamlombard
Adam Lombard

Posted on April 30, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related