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:

Answer from neves on Stack Overflow
🌐
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:
🌐
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.
🌐
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 - Open your VSCode settings, by going 'Code -> Preferences -> Settings'. Search for "python formatting provider" and select "black" from the dropdown menu:
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.

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.

🌐
Orchestra
getorchestra.io › guides › how-to-install-black-formatter-on-vs-code
How to Install Black Formatter on VS Code | Orchestra
1 month ago - Learn how to install and configure Black Formatter on VS Code to streamline Python code formatting and ensure consistent code style.
🌐
GitHub
github.com › microsoft › vscode-black-formatter
GitHub - microsoft/vscode-black-formatter: Formatting support for Python using the Black formatter · GitHub
A Visual Studio Code extension with support for the Black formatter. The extension ships with black=26.1.0. Note: The minimum version of Black this extension supports is 26.1.0. This extension includes support for all actively supported versions of the Python language.
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
https://marketplace.visualstud... "python.formatting.provider": "none", Now you can use the command palette to run "Format Document" to run Black on the current file....
Find elsewhere
🌐
DEV Community
dev.to › facepalm › how-to-set-formatting-and-linting-on-vscode-for-python-using-black-formatter-and-flake8-extensions-322o
How to Set Formatting and Linting on VSCode for Python using Black Formatter and Flake8 Extensions - DEV Community
October 12, 2023 - If you want VSCode to format on save, you need to write this: { "editor.defaultFormatter": "ms-python.black-formatter", "editor.formatOnSave": true, // new }
🌐
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”.
🌐
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).
🌐
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.
🌐
GitHub
github.com › microsoft › vscode-black-formatter › issues › 82
Clarify relationship with Python extension · Issue #82 · microsoft/vscode-black-formatter
February 21, 2022 - "[python]": { "editor.defaultFormatter": "ms-python.black-formatter" } https://github.com/microsoft/vscode-black-formatter#usage
Author   afeld
🌐
Orchestra
getorchestra.io › guides › black-formatter-not-working-vscode-fixes
Black Formatter Not Working: VSCode Fixes | Orchestra
December 10, 2024 - You can set the interpreter in VSCode by pressing Ctrl + Shift + P, typing Python: Select Interpreter, and choosing the correct environment. ... Ensure that your VSCode settings are configured to use Black as the default formatter.
🌐
Stack Overflow
stackoverflow.com › questions › 77236631 › vscode-python-formatting
visual studio code - VSCode Python Formatting - Stack Overflow
{ "editor.formatOnSave": false, "[python]": { "editor.defaultFormatter": "ms-python.black-formatter" } }
🌐
Orchestra
getorchestra.io › guides › how-to-enable-linting-in-vscode-with-black-formatter-and-ruff
How to Enable Linting in VScode with Black Formatter and Ruff | Orchestra
January 23, 2025 - Here’s how to install and configure it in VScode: Install Black: Open your terminal and run the following command: ... Open VScode and navigate to File > Preferences > Settings.
🌐
GitHub
gist.github.com › 1eedaegon › 06c975f59a7ebd999814210c33be8f13
Set up black python formatter on vscode · GitHub
"python.formatting.provider": "black", "python.formatting.blackArgs": ["--line-length", "150"], "python.analysis.typeCheckingMode": "basic", "[python]": { "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": true } },
🌐
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 28, 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