How to format python on Vscode

Endrit Qerreti

Endrit Qerreti

Formatting your code it's a must when it comes to coding, because not only it makes it look better and clean, but it also saves you a lot of time because it's easier to read it.

You may think, I can read it because I wrote it, that's true but not always, especially when working on your old projects.

In this tutorial, you will learn how to format python on Vscode.

Step 1 - Install black Formatter

black formattter is the extension that we are going to use to format the code on visual studio code.  There are multiple ways to install black formatter extension,

Install black formatter via pip

pip install black

Output

Install black formatter from vscode extensions

Alternatively, you can install black formatter from extension section on visual studio code, press CTRL + SHIFT + X to open the extension window, and search for "Black formatter" and click it

Next, Install the extension by clicking the "Install" button

or simply by using the install command

Press CTRL + P on vscode and enter the command below

ext install ms-python.black-formatter

Step 2 - Configure black formatter

Once you have installed black formatter on your computer, now you need to configure it so you can use it.

1.Launch Vscode

2. Go to Settings by pressing CTRL + comma, or by GUI: File > Preferences > Settings

3. Once you are in the settings section, click to the search box and type python.formatting.provider and the option "Python Formatting Provider" should show up as shown in the image below

Click to the drop down box, and select "black"

Now your vscode is using black formatter to format code

Step 3 - Enable format on save

If you want your code to be automatically formatted once you save your code by pressing CTRL + S, you can do so by enabling the format on save option. To enable this option, go to settings and search for "editor.formatOnSave" and check the box as shown in the image below

Step 3 - Format your code

Now all you have to do is save a python file by pressing CTRL + S and the black formatter will format your code.

For example, this is how a unformatted code looks like

After formatting with black formatter

You can also format your code by right clicking somewhere in the code and then clicking to " Format Document", this action is equivalent to CTRL + SHIFT + I. However, you don't have to press any key combination to format code with black formatter because you can use the format on save option.

Alternatively, black formatter can also format your code as you type, this option is not enabled by default, so to enable it, open settings and search "editor.formatOnType" and check the box

If for some reason these settings don't work, could be because the path of black is not configured correctly.

First you need to identify where the path bin file is located, to do this, simply run the command below

which black

and it should show you the full path of black

Now, you need to copy this path and paste to settings.json file, the settings file is located on .config/code/user/settings.json

  1. Open settings.json with vscode
  2. Add the path to settings.json
  3. Settings.json should look like this

4. Save settings, and restart vscode

💡
Note: The two options that we enabled above (Format on Save and Format on Type) by using vscode GUI can also be enabled manually by adding them on the settings.json file.

For example, to enable "Format on save" and "Format on Type" manually you need to add the config below to your settings.json file

"editor.formatOnType": true
"editor.formatOnSave": true

For example, this how the settings.json should look with the configs above enabled

Now python code will be autoformatted as you type.

Conclusion

In this tutorial, we explained step by step how to format your python code on Visual Studio code, by using the black formatter extension. We also explained, how you can enable format on type and format on save options via GUI, and manually by configuring the settings.json file.