44

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

EDIT: 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?

Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
Rafael Zayas
  • 2,061
  • 1
  • 18
  • 20
  • you need `"--line-length=80"` instead of `"--line-length 80"` note the equals. – Mark Jul 06 '21 at 14:57
  • 1
    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. – Rafael Zayas Jul 09 '21 at 15:13

4 Answers4

77

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

He3lixxx
  • 3,263
  • 1
  • 12
  • 31
Pavel Hanpari
  • 4,029
  • 2
  • 20
  • 23
  • 1
    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. – Rafael Zayas Sep 23 '19 at 14:54
  • @RafaelZayas [citation needed] on vs code settings having changed. This answer is equivalent, it uses the gui vs editing settings.json directly. – Keith Devens Oct 20 '19 at 16:02
  • 1
    @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. – Rafael Zayas Oct 21 '19 at 14:48
  • 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. – Pavel Hanpari Oct 24 '19 at 09:43
  • 1
    Works on "VS Code 1.49.2" and "black-20.8b1" on 2020-09-28. Thanks a lot! – Vega Sep 28 '20 at 08:54
  • For reference: in `settings.json` this is now `"black-formatter.args": ["--line-length=80"]` (as of 2023) – F.D.Castel Aug 31 '23 at 17:13
19

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"]
Keith Devens
  • 2,832
  • 2
  • 15
  • 7
  • Are you sure this is the correct format for Black? It doesn't seem to work for me. – Wessi Jun 07 '18 at 09:48
  • 3
    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? – Keith Devens Jun 07 '18 at 15:41
  • This one worked for me: "python.formatting.blackArgs": ["--line-length=119"] ... none of other options did. – Lovato Apr 08 '19 at 13:21
  • 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. – Rafael Zayas Sep 23 '19 at 14:53
  • @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. – Keith Devens Oct 20 '19 at 16:00
  • According to the [documentation](https://code.visualstudio.com/docs/python/editing#_formatterspecific-settings) and [one of the maintainers](https://github.com/microsoft/vscode-python/issues/19413#issuecomment-1176503490) this is the correct answer. – Andreas Haferburg Jul 07 '22 at 11:12
9

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"]
felipeportella
  • 121
  • 1
  • 5
5

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

DarkJediNinja
  • 495
  • 6
  • 10
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/23704377) – Mihai Chelaru Aug 04 '19 at 00:05
  • Thanks for your guidance, @MihaiChelaru. I edited the answer to go directly to the point. – DarkJediNinja Aug 04 '19 at 01:06
  • 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. – Rafael Zayas Aug 06 '19 at 00:02