19

I noticed one strange thing that autopep8 autoformatting in VSCode doesn't work when we set

    "python.formatting.autopep8Args": [
        "--line-length 119"
    ],

But if this setting is in a default mode that is line length 79 then it works well. Is there some issue with autopep8 to work only with line length 79 not more than that, or I am making any mistake in VSCode. Major feature that I need is when my python program line goes too long it should be able to break it in multiple lines. I don't want to go ahead with 79 characters' approach. My preferred approach is 119 characters. Currently, I have to indent big lines manually. Is there any other format apart from pep8 which supports 119 characters and indent lines with characters more than 119 characters automatically.

I am attaching my settings.json file data

{
    "window.zoomLevel": 1,
    "python.dataScience.sendSelectionToInteractiveWindow": true,
    "diffEditor.ignoreTrimWhitespace": false,
    "editor.fontSize": 16,
    "python.formatting.provider": "autopep8",
    "editor.formatOnPaste": true,
    "editor.formatOnSave": true,
    "editor.formatOnType": true,
    "python.autoComplete.addBrackets": true,
    "python.formatting.autopep8Args": [
        "--line-length 119"
    ],
    // "python.linting.flake8Args": [
    //     "--max-line-length=120"
    // ],
    "files.autoSaveDelay": 10000,
    "editor.defaultFormatter": "ms-python.python",
    "files.autoSave": "afterDelay",
    "files.trimTrailingWhitespace": true,
    "files.trimFinalNewlines": true,
    "editor.quickSuggestions": true,
    "editor.codeActionsOnSave": null,
}
Mohit Kumar
  • 533
  • 1
  • 4
  • 11
  • 1
    is it that it's supposed to be `--max-line-length=119`? – anthony sottile Aug 08 '20 at 17:53
  • Pep8 says --max-line-length=79 but Django says --max-line-length=119. Both are associated with Python. Even in GitHub code review it is 119. I just want something that supports python with 119 characters and formats my document automatically rather than me doing it, as it formats with autopep8 with max line length = 79 – Mohit Kumar Aug 09 '20 at 02:42

3 Answers3

54

experimental worked for me

"python.formatting.autopep8Args": ["--max-line-length", "120", "--experimental"]

check out this link for proper format specifier settings

senti143
  • 726
  • 6
  • 5
  • 7
    `--experimental` was not necessary for me. – Dimitris Baltas Nov 02 '20 at 04:16
  • 2
    With autopep8 1.5.7 on Mac `--experimental` option was crucial to format according the max length, otherwise lines were kept above the provided max length. – k_rus Sep 02 '21 at 12:18
  • @k_rus: do you know in what version was this corrected for Mac ?(i.e. not requiring "experimental") – MrT77 Feb 14 '22 at 17:54
  • @MrT77 Sorry, I don't know. It didn't work with version 1.5.7. I can see now that 1.6.0 is available through homebrew, but I haven't tested. – k_rus Feb 23 '22 at 08:50
  • I'm using a setup.cfg in MacOs, so I define there experimental=True and max-line-length=120 but I still get warnings for line too long when it's greater than 100...Any idea on this ? (I'm using autopep8 1.6.0) – MrT77 Mar 02 '22 at 16:49
16

This should work -

"python.formatting.provider": "autopep8",
"python.formatting.autopep8Args": [
    "--max-line-length",
    "120",
    "--experimental"
]
PD Wanjari
  • 161
  • 3
7

As mentioned in the answer by @senti143,

On Windows, to set the python word wrap at 120 characters

Go to settings and search for autopep8

Find Python > Formatting: Autopep8 Args

Click on Add Item button and

add the value

--max-line-length

Click on Add Item button again and add the value

120

Click on Add Item button again and add the value

--experimental

As shown in the screenshot below

enter image description here

Now go back to the code and select the necessary code to format the line wrap at 120 characters

Kishan K
  • 671
  • 7
  • 18