13

I'm trying to use black as a formatter for Python on VS Code on Ubuntu 20.04 but it's not working on auto save.

I've selected black in Python>Formatting:Provider. I'm using prettier as my default formatter for which I added a .prettierignore, disabled, and uninstalled to make sure it wasn't interfering with black. I also added a custom path to ./local/bin/black. It works though when I run it through the terminal. How do I make it work?

{
  editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.formatOnSave": true,
  "python.formatting.provider": "black",
  "python.formatting.blackArgs": [
    "-l 120"
  ],
  "editor.formatOnType": true,
  "python.formatting.blackPath": "./local/bin/black"
}
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
VectorXY
  • 349
  • 1
  • 3
  • 12
  • Can you post what you have on your settings.json for `python.formatting.*`. Also, `./local/bin/black` could be the wrong directory (could be the directory for the current workspace). Is there really a `.` at the start? – Gino Mempin Mar 20 '21 at 01:42
  • @GinoMempin I edited the question. Before I change the path the default was `path` which is working. But this shouldn't be the problem here. `./local/bin/black` still executes black. – VectorXY Mar 20 '21 at 01:48
  • On VS Code, open the Output tab (normally on the bottom panel) and select Python from the dropdown. Do you see any messages about black? – Gino Mempin Mar 20 '21 at 01:48
  • Only outputs from pylint. – VectorXY Mar 20 '21 at 01:52

5 Answers5

15

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:

VS Code screenshot of Output

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • 1
    Thanks. `home//.local/bin/black` worked. I will take your advice about the default formater. – VectorXY Mar 20 '21 at 19:45
  • I can not get the line length to work. I put for example 500 but it still does it at the default length (80, 90 or whatever it is). In the terminal (output tab) I get ```~\Desktop\fastapi_training\myenv\Scripts\black --line-length 500 --diff --quiet ~\Desktop\fastapi_training\postgre_sql_app\crud.py cwd: ~\Desktop\fastapi_training``` – elano7 Nov 11 '21 at 18:17
  • 1
    @elano7 It might be dependent on the line being formatted, since the [black code style mentions](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#line-length) that "*Black will try to respect that. However, sometimes it won’t be able to without breaking other rules.*" I recommend to try it outside of VS Code (ex. do `black -l 500 file.py`) to see if it's a black or VS Code issue, then post a _separate_ question for that _specific_ issue. – Gino Mempin Nov 11 '21 at 23:50
  • @GinoMempin thanks, you were right. It respects my rule on method chaining or long strings, but on a tuple it has its own rules. – elano7 Nov 12 '21 at 18:24
0

For me it was because I had one invalid argument in black arguments.

ar-siddiqui
  • 161
  • 1
  • 8
0

My VSCode version is Version: 1.72.2. I installed python 3.10 using Anaconda and the black version installed by conda is 19.10b0.

I have configured "python.formatting.provider": "black" in VSCode settings.json and black doesn't format.

Once I manully update black to 22.10.0, the format works properly.

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
0

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.

Artur Dutra
  • 515
  • 3
  • 18
0

For the people that got to this point and still haven't found an answer to this and tried multiple solutions, including installing the microsoft black formatter extension:

  • disable it
  • add the following to your user settings and workspace settings:
    "python.formatting.provider": "black",
    "editor.formatOnSave": true,
    "python.linting.lintOnSave": true,
    "[python]": {
        "editor.defaultFormatter": null,
        "editor.insertSpaces": true,
        "editor.tabSize": 4,
        "editor.formatOnSave": true
    },

m0r
  • 102
  • 9