There are only a few settings you need to setup black as a formatter on VS Code. It seems you got most of it right, but I am doubtful about using relative paths for blackPath (VS Code should have shown an error if the path is indeed incorrect).

I suggest switching to absolute paths.

Here are my settings:

// User Settings

"editor.defaultFormatter": null,
"editor.formatOnSave": false,  // enable per language
"[python]": {
    "editor.formatOnSave": true
},
"python.formatting.provider": "black",
"python.formatting.blackPath": "/usr/local/bin/black"

// Workspace Settings

"python.formatting.blackPath": "/absolute/path/to/venv/with/black",
"python.formatting.blackArgs": [
    "-l 120"
],

First of all, I suggest getting rid of the editor.defaultFormatter setting (or just set it back to the default null). Instead of setting a default for everything, configure your formatter for each language and for each extension. Here, it's null and then I configure python-specific settings then I have separate ones for other languages (ex. JS and C++). You mentioned something about Prettier, and that could be interfering with VS Code using black.

Second, make sure you are modifying the correct settings. VS Code has 3 sets of settings: User, Workspace, and Folder. I normally have the formatOnSave enabled for Python on the User settings, and provider set to black (using system-wide installed black). On a specific workspace, I have a virtual environment and I override the blackPath to the black specifically installed on that virtual environment. You can also just put all the settings in the User settings or use the same system-wide-installed black. But the main point here is to use absolute paths for both (basically copying the output of which black from the console).

Note that, if you specified blackPath to point to a particular virtual environment, make sure to select that same virtual environment on your workspace.

Lastly, you can check for any issues from the Output tab > Python:

Answer from Gino Mempin on Stack Overflow
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
Black Formatter - Visual Studio Marketplace
Extension for Visual Studio Code - Formatting support for Python files using the Black formatter.
Top answer
1 of 7
18

There are only a few settings you need to setup black as a formatter on VS Code. It seems you got most of it right, but I am doubtful about using relative paths for blackPath (VS Code should have shown an error if the path is indeed incorrect).

I suggest switching to absolute paths.

Here are my settings:

// User Settings

"editor.defaultFormatter": null,
"editor.formatOnSave": false,  // enable per language
"[python]": {
    "editor.formatOnSave": true
},
"python.formatting.provider": "black",
"python.formatting.blackPath": "/usr/local/bin/black"

// Workspace Settings

"python.formatting.blackPath": "/absolute/path/to/venv/with/black",
"python.formatting.blackArgs": [
    "-l 120"
],

First of all, I suggest getting rid of the editor.defaultFormatter setting (or just set it back to the default null). Instead of setting a default for everything, configure your formatter for each language and for each extension. Here, it's null and then I configure python-specific settings then I have separate ones for other languages (ex. JS and C++). You mentioned something about Prettier, and that could be interfering with VS Code using black.

Second, make sure you are modifying the correct settings. VS Code has 3 sets of settings: User, Workspace, and Folder. I normally have the formatOnSave enabled for Python on the User settings, and provider set to black (using system-wide installed black). On a specific workspace, I have a virtual environment and I override the blackPath to the black specifically installed on that virtual environment. You can also just put all the settings in the User settings or use the same system-wide-installed black. But the main point here is to use absolute paths for both (basically copying the output of which black from the console).

Note that, if you specified blackPath to point to a particular virtual environment, make sure to select that same virtual environment on your workspace.

Lastly, you can check for any issues from the Output tab > Python:

2 of 7
2

I was also having the issue. What solved the issue for me was after downloading the Black formatter extension and once the document was open I did a right-click with the mouse and then press "format document". Choose black. I was good to go and had no more issues after that.

Discussions

python - Formatter black is not working on my VSCode...but why? - Stack Overflow
I have started using Python and Django and I am very new in this field. And, this is my first time to ask a question here...I do apologise in advance if there is a known solution to this issue... W... More on stackoverflow.com
🌐 stackoverflow.com
How to auto-format your Python with Black + VSCode
Just right click and then click format document That is it will do all things automatically More on reddit.com
🌐 r/Python
20
33
June 18, 2020
VS Code Python + Black formatter arguments - python.formatting.blackArgs - Stack Overflow
As of April 2024, I don't think python.formatting is recognized by vscode. ... As @Landon said same on black formatter v2024.2.0 and vscode 1.89.1. More on stackoverflow.com
🌐 stackoverflow.com
visual studio code - Formatting python with Black in VSCode is causing arrays to expand vertically, any way to compress them? - Stack Overflow
I'm using Black to format python in VSCode, and it's making all my arrays super tall instead of wide. I've set max line length to 150 for pep8 and flake8 and black (but I'm new to Black, and not su... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Visual Studio Code
code.visualstudio.com › docs › python › formatting
Formatting Python in VS Code
November 3, 2021 - Alternatively, you can set it as the default formatter for all Python files by setting "editor.defaultFormatter" in your User settings.json file, under a [python] scope. You can open settings.json with the Preferences: Open User Settings (JSON) command. For example, to set Black Formatter as the default formatter, add the following setting to your User settings.json file:
🌐
Medium
marcobelo.medium.com › setting-up-python-black-on-visual-studio-code-5318eba4cd00
Setting up python Black on Visual Studio Code | by Marco Belo | Medium
August 25, 2020 - Setting up python Black on Visual Studio Code To setup python black as a formatter for VS-Code workspace you need to install it on your virtual env or in your local python with the command: $ pip …
🌐
DEV Community
dev.to › adamlombard › how-to-use-the-black-python-code-formatter-in-vscode-3lo0
VSCode: Using Black to automatically format Python - DEV Community
April 4, 2024 - Black is "the uncompromising Python code formatter." It can be configured to automatically format your code whenever you save a file in VSCode.
🌐
Orchestra
getorchestra.io › guides › how-to-install-black-formatter-on-vs-code
How to Install Black Formatter on VS Code | Orchestra
February 10, 2026 - Learn how to install and configure Black Formatter on VS Code to streamline Python code formatting and ensure consistent code style.
Top answer
1 of 16
128

Update 2023-09-15:

Now VSCode has a Microsoft oficial Black Formatter extension. It will probably solve your problems.

Original answer:

I use Black from inside VSCode and it rocks. It frees mental cycles that you would spend deciding how to format your code. It's best to use it from your favorite editor. Just run from the command line if you need to format a lot of files at once.

First, check if you have this in your VSCode settings.json (open it with Ctrl-P + settings):

"python.formatting.provider": "black",
"editor.formatOnSave": true,

Remember that there may be 2 setting.json files: one in your home dir, and one in your project (.vscode/settings.json). The one inside the project prevails.

That said, these kind of problems usually are about using a python interpreter where black isn't installed. I recommend the use of virtual environments, but first check your python interpreter on the status bar:

If you didn't explicitly select an interpreter, do it now clicking on the Python version in your status bar. You can also do it with Ctrl-P + "Python: Select Interpreter". The status bar should change after selecting it.

Now open a new terminal. Since you selected your interpreter, your virtual environment should be automatically activated by VSCode. Run python using your interpreter path and try to import black:

$ python
Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import black
>>> 

Failed import? Problem solved. Just install black using the interpreter from the venv: python -m pip install black. You also can install using Conda, but in my experience VSCode works better with pip.

Still not working? Click in the "OUTPUT" tab sibling of the TERMINAL and try to get more info at the "Log" output (if you use the newer Black plugun it may be called "Black Formatter"). Select it in the pull down menu:

2 of 16
68

2024/02/19 updated

The previous solution doesn't work anymore.

There're two issues:

  1. vscode has new official extension
  2. different black output from vscode extension and black in your python virtual environment

If you just want to use 'black' formatter and don't care issue #2. Just install vscode black extension

I revised my solution for issue #2
a. install black extension first
b. set up your virtual environment, install black init and set your project interpreter to it
c. go to user setting, set Black-formatter: Path to "black" in your virtual environment

"black-formatter.path": ["black"]

NOTE: Install 'black' in the python environment you need, remove global 'black'

===== Obselete solution for old vscode =====
Attach my finding for those who still can't solve the Black formatting issue in VS Code.

First, you have to install Black globally or locally (if you use virtual env like conda).

Then, make sure your VS settings as following, set python default formatter provider as 'black':

Finally, open settings.json of your VS Code, add the following segment for it.

"[python]": {
    "editor.defaultFormatter": null,
    "editor.insertSpaces": true,
    "editor.tabSize": 4,
    "editor.formatOnSave": true
}

The key point is:

"editor.defaultFormatter": null

If you still use "editor.defaultFormatter": "black" as many old posts suggest, the Black formatter will not work in newer VS Code.

Find elsewhere
🌐
GitHub
github.com › microsoft › vscode-black-formatter
GitHub - microsoft/vscode-black-formatter: Formatting support for Python using the Black formatter · GitHub
Integrated formatting: Once this extension is installed in VS Code, Black will be automatically available as a formatter for Python. This is because the extension ships with a Black binary.
Starred by 190 users
Forked by 45 users
Languages   Python 88.5% | TypeScript 11.1% | JavaScript 0.4%
🌐
GitHub
gist.github.com › kstrauser › c0cf3c440c3bffed60cb8e85de7f6649
Using the "black" Python formatter in VS Code · GitHub
... The joslarson.black-vscode plugin is deprecated and black is supported by the native Python package: https://code.visualstudio.com/docs/python/editing#_general-formatting-settings
🌐
Microsoft Developer Blogs
devblogs.microsoft.com › dev blogs › microsoft for python developers blog › python in visual studio code – may 2022 release
Python in Visual Studio Code – May 2022 Release - Microsoft for Python Developers Blog
May 5, 2022 - We’re excited to announce that the May 2022 release of the Python and Jupyter Extensions for Visual Studio Code are now available! With this release we’re introducing three new extensions: Black, isort, and Jupyter Powertoys. If you’re interested, you can check the full list of improvements in our changelogs for the Python, Jupyter and Pylance […]
🌐
Microsoft Developer Blogs
devblogs.microsoft.com › dev blogs › microsoft for python developers blog › python in visual studio code – may 2018 release
Python in Visual Studio Code – May 2018 Release - Microsoft for Python Developers Blog
November 19, 2019 - To enable the Black formatter, go into File > User Preferences > Settings, and put the following setting in your User Settings (for settings for all workspaces) or Workspace settings (for the current workspace/folder).
🌐
StatusNeo
statusneo.com › home › software development › introducing black: the uncompromising python code formatter
Introducing Black: The Uncompromising Python Code Formatter - StatusNeo
October 23, 2023 - Install Black using pip, as mentioned earlier. Open VSCode settings by clicking on “File” -> “Preferences” -> “Settings” · Search for “python.formatting.provider” and set the value to “black”.
🌐
GitHub
gist.github.com › zerossB › 1a2b128cd378491fa26747dd7fd70bee
How to configure black in vscode · GitHub
"editor.codeActionsOnSave": { "source.organizeImports": true }, "editor.formatOnSave": true, "editor.formatOnPaste": true, "python.formatting.provider": "black", "python.formatting.blackArgs": [ "-t", "py37" ] ... Works for me! ... My vscode doesn't sync with my ~/.config/black configuration file.
🌐
Reddit
reddit.com › r/python › how to auto-format your python with black + vscode
r/Python on Reddit: How to auto-format your Python with Black + VSCode
June 18, 2020 - --- If you have questions or are new to Python use r/LearnPython ... Sorry, something went wrong when loading this video. View in app · Archived post. New comments cannot be posted and votes cannot be cast. Share ... AFAIK you need to provide VSCode with a formatter to use, eg. autopep8 or Black
🌐
GitHub
github.com › microsoft › vscode-black-formatter › issues › 82
Clarify relationship with Python extension · Issue #82 · microsoft/vscode-black-formatter
June 14, 2022 - "[python]": { "editor.defaultFormatter": "ms-python.black-formatter" } https://github.com/microsoft/vscode-black-formatter#usage
Author   afeld
🌐
GitHub
yellowduck.be › posts › vscode-setting-line-lengths-in-the-black-python-code-formatter
🔗 VSCode: Setting line lengths in the Black Python code formatter
The docs for the Black Python code formatter say that the formatter "is not configurable". This is largely true, but if you have Black set up > to work in VSCode, you can configure the line length.