Skip to content

Instantly share code, notes, and snippets.

@camsvn
Last active November 9, 2023 21:29
Show Gist options
  • Save camsvn/fc48f483004c718e49d20d29b34823a2 to your computer and use it in GitHub Desktop.
Save camsvn/fc48f483004c718e49d20d29b34823a2 to your computer and use it in GitHub Desktop.
Black: The Uncompromising Code Formatter, setup on VSCode and PyCharm

Black: The Uncompromising Code Formatter, setup on VSCode and PyCharm

By using Black, you agree to cede control over minutiae of hand-formatting. In return, Black gives you the speed and freedom about your python code formatting. Black makes code review faster by producing the smallest diffs possible.

Here in this doc I'll walk you through the setup of Black in VSCode and PyCharm code editor's.

Install Black in your virtual environment:

$ pip install black

VSCode:

Install Microsoft's Black extension in VSCode:

Microsoft Marketplace

The bundled black is only used if there is no installed version of black found in the selected python environment.

Usage

You can do this either by using the context menu (right click on a open python file in the editor) and select "Format Document With...", or you can add the following to your settings:

"[python]": {
    "editor.defaultFormatter": "ms-python.black-formatter"
  }

and change the following, if set:

"python.formatting.provider": "none"

Format on save

You can enable format on save for python by having the following values in your settings:

"[python]": {
    "editor.defaultFormatter": "ms-python.black-formatter",
    "editor.formatOnSave": true
  }

PyCharm:

You need to setup External tools.

  • File > Settings > Tools > External Tools
  • Click on the “+” icon
  • Mention your desired Name in the Name input, and give it some description.
  • In tool settings, update as following

black as external tool in pycharm

  • Click on “OK”

Usage

Now, you’ve done the basic setup of Black and you can now format any file by going on Tools > External Tools ( Your Group name, mine is Linter) > Black.

format the current file

Additionally you could create a shortcut on PyCharm’s for formatting file with Black by configuring a keymap.

add keymap

With this Ctrl+Alt+Shift+L; shortcut we can now call the script to format the file.

Format on save

To setup format on save, we need to goto settings again:

  • File > Settings > Tools > File Watchers
  • Click on the “+” icon.
  • Select “custom”

Add a new custom file watcher

In the dialog box which appears,

  • Input Name of your choosing in “Name”
  • In File type select “Python”
  • In ‘tool to run on changes’ input following
Program: $PyInterpreterDirectory$/black
Arguments: $FilePath$
Output paths to refresh: $FilePath$
Working directory: $ProjectFileDir$

configure file watcher

  • Click on “OK” and you’re good to go.

Now just restart your PyCharm instance and you can witness the “Black” magic. 🥂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment