VS Code Python + Black formatter arguments - python.formatting.blackArgs
Asked Answered
O

7

69

April 2024 UPDATE, Please read:

It appears the answer since late 2023 is the newly accepted answer provided by Mircea, this question is old so older, now incorrect answers, have more upvotes but please scroll down for that answer.

original question:
I'm using the May 2018 Python extension (released June 2018) for VS Code 1.23.1 on Windows, python 3.6 via Anaconda, conda installing black from conda-forge into my conda environment.

In my user settings.json I have the below:

"python.formatting.blackArgs": [
    "--line-length 80"
],

which I'd think would be the correct way to structure this to pass arguments to black in VS Code Python formatting.

However, in my python Output pane I get the below:

Formatting with black failed.
Error: Error: no such option: --line-length 80

If I edit my settings.json to be no args, such as:

"python.formatting.blackArgs": [],

black works as expected.

Does anyone know how to pass arguments correctly to the new (as of June 2018) black formatter?

Osmund answered 6/6, 2018 at 17:6 Comment(3)
you need "--line-length=80" instead of "--line-length 80" note the equals.Skuld
Thanks @Mark! For this, it looks like the accepted answer was edited to match the current way VS Code settings operate. It looks like thanks to @He3lixxx for helping edit and keep the accepted answer relevant. Older info along these lines is also in the comments to the accepted answer.Osmund
NB, for the second time in 6 years I've changed the accepted answer, as the settings in VS Code for this have changed.Osmund
C
18

Hopefully the answer for a more recent VSCode and Black helps:

With VsCode Insiders (1.83.0-insider) and Black installed from extensions (v2023.4.1), installed from extensions (https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter)

I had to add -l or --line-length and 80 as separate items to File->Preferences->Settings->[type]black->Black-formatter: Args->Add item.

In user settings json (Ctrl + Shift + P --> Open User Settings) I have:

"black-formatter.args": ["--line-length", "80"]

If this doesn't work, there's useful information in the Output Window (you can select Black Formatter) to see the logs from Black.

Counterforce answered 20/9, 2023 at 9:45 Comment(2)
This is the one working as of 2024.03 that I've tried this.Ablate
i've changed my 'accepted' answer to this with the recent update to VS Code. Thanks!Osmund
S
110

The issue is that you need =80 instead of 80 after --line-length for version 1.38.1 and above:

--line-length=80  

enter image description here

Stevana answered 22/9, 2019 at 11:47 Comment(6)
Thanks Pavel - given how the VS Code settings have changed, I believe this answer is now correct, although the previously correct answer has more upvotes, as it correct for over a year.Osmund
@RafaelZayas [citation needed] on vs code settings having changed. This answer is equivalent, it uses the gui vs editing settings.json directly.Buccaneer
@KeithDevens I think the VS Code experience has changed such that the GUI is the most common way to interact with the settings. There is not even a prompt in the black formatter settings to "Edit in settings.json" as there are for some other settings.I think it is most appropriate to change the accepted answer to match the current state of how VS Code settings are edited, not the settings at the time the question was asked.Osmund
You are right. I would not post it here if I found a simple way how to edit json file in my version of VS Code. If someone knows how to edit settings in old fashion, I would be happy to add this piece of information to my answer.Stevana
Works on "VS Code 1.49.2" and "black-20.8b1" on 2020-09-28. Thanks a lot!Bethina
For reference: in settings.json this is now "black-formatter.args": ["--line-length=80"] (as of 2023)Smarm
B
22

The examples of formatter-specific settings show the following:

"python.formatting.autopep8Args": ["--max-line-length", "120", "--experimental"],
"python.formatting.yapfArgs": ["--style", "{based_on_style: chromium, indent_width: 20}"]

So try:

"python.formatting.blackArgs": ["--line-length", "80"]
Buccaneer answered 6/6, 2018 at 18:6 Comment(6)
Are you sure this is the correct format for Black? It doesn't seem to work for me.Samples
I just tested the following settings with different line lengths and it works: "python.formatting.blackArgs": ["-S", "--line-length", "80"] Are you using the newest versions of 1. VSCode, 2. the Python extension, 3. Black?Buccaneer
This one worked for me: "python.formatting.blackArgs": ["--line-length=119"] ... none of other options did.Immediately
I originally marked this answer as correct. However, as the VS Code python extension args have changed recently, I believe the correct answer is now listed below, which I have marked as the answer.Osmund
@RafaelZayas [citation needed] on vs code settings being changed. This answer still works (try it). The other answer just uses the gui to set the setting.Buccaneer
According to the documentation and one of the maintainers this is the correct answer.Rigid
C
18

Hopefully the answer for a more recent VSCode and Black helps:

With VsCode Insiders (1.83.0-insider) and Black installed from extensions (v2023.4.1), installed from extensions (https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter)

I had to add -l or --line-length and 80 as separate items to File->Preferences->Settings->[type]black->Black-formatter: Args->Add item.

In user settings json (Ctrl + Shift + P --> Open User Settings) I have:

"black-formatter.args": ["--line-length", "80"]

If this doesn't work, there's useful information in the Output Window (you can select Black Formatter) to see the logs from Black.

Counterforce answered 20/9, 2023 at 9:45 Comment(2)
This is the one working as of 2024.03 that I've tried this.Ablate
i've changed my 'accepted' answer to this with the recent update to VS Code. Thanks!Osmund
A
10

The proper way to config in the Settings GUI pane is with --line-length and the desired value as separate items:

Visual Studio Code GUI Settings for Python Formatting

This converts into the settings.json into this:

Visual Studio Code JSON Settings for Python Formatting

"python.formatting.provider": "black",
"python.formatting.blackArgs": ["--line-length", "110"]
Augustus answered 27/12, 2019 at 14:34 Comment(1)
This is exactly what I was looking for!Thievery
P
6

My setting is: "python.formatting.blackArgs": ["--line-length=110"] and it's working correctly. The equal sign was missing in your user settings.json

Purpose answered 3/8, 2019 at 22:9 Comment(3)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewHance
Thanks for your guidance, @MihaiChelaru. I edited the answer to go directly to the point.Purpose
Hi DarkJediNinja - I've been using the settings as Keith indicated, and given the VS Code settings documentation, I think that's most consistent with how it works for other formatting settings. But I'm upvoting this as well because if I'm stuck on command line kinds of settings I'll try this trick of single string with an equal sign.Osmund
B
4

As of April 2024, I don't think python.formatting is recognized by vscode.

"python.formatting.blackArgs": [],

gets dimmed by vscode and on hover shows Unknown Configuration Setting

Mircea mentioned doing something like

"black-formatter.args": ["--line-length", "80"]

and that worked for me

Bugleweed answered 3/4 at 18:27 Comment(0)
D
0

As @Landon said same on black formatter v2024.2.0 and vscode 1.89.1.

At cmd + , -> Black-formatter: Args,

I added "--line-length" and "140(which is my preference)" as separate two item like below with no quotes.

Black-formatter: Args

then it turn into the list on user setting json like below and it works.

user setting json

Dewyeyed answered 9/7 at 5:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.