2023-06-10

Black Formatter in VSCode

Black Code Formatter in VSCode

The Black Formatter, version 23.3.0, integrated within VSCode extension. The Formatter extension with Black supports all actively supported versions of the Python language, starting from Python 3.7. It is important to note that the bundled version of Black (23.3.0) is only utilized if there isn't an installed version of Black in the selected Python environment. Additionally, the minimum supported version of Black by this extension is 22.3.0.

Note on Experimental Status

The Formatter extension is currently (June 10, 2023) in its experimental phase. Plans are in place for this extension to eventually replace the Black formatting functionality integrated within the Python extension for VSCode.

Installation

To get started, install the Black Formatter extension from the VSCode marketplace. Once installed, it becomes available as a formatter for Python files.

Usage

Setting Black Formatter as Default

After installation, it's necessary to set "Black Formatter" as the default formatter for Python files. This can be done in two ways:

  • Use the context menu (right-click on an open Python file in the editor) and select "Format Document With..."
  • Add the following snippet to your settings.json file.
settings.json
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}

In case you have another formatter set, ensure you override it by adding:

settings.json
"python.formatting.provider": "none"

Enabling Auto-Formatting

For a more streamlined workflow, you can enable the formatter to automatically format your code whenever you save a file. To enable this, include the following in your settings.json file:

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

References

https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!