Python AutoPep8 formatting not working with max line length parameter
Asked Answered
O

5

26

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,
}
Ossetia answered 8/8, 2020 at 10:41 Comment(2)
is it that it's supposed to be --max-line-length=119?Niehaus
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 = 79Ossetia
I
60

experimental worked for me

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

check out this link for proper format specifier settings

Ics answered 15/8, 2020 at 6:28 Comment(5)
--experimental was not necessary for me.Diazo
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.Vinavinaceous
@k_rus: do you know in what version was this corrected for Mac ?(i.e. not requiring "experimental")Rutty
@Rutty 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.Vinavinaceous
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)Rutty
P
21

This should work -

"python.formatting.provider": "autopep8",
"python.formatting.autopep8Args": [
    "--max-line-length",
    "120",
    "--experimental"
]
Pilferage answered 18/8, 2020 at 7:25 Comment(0)
E
14

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

Expectorant answered 9/11, 2022 at 5:11 Comment(2)
On VS Code 1.87 --experimental parameter is not required. "autopep8.args": ["--max-line-length=120"] is enoughEtoile
Oddly enough, on VS Code 1.89, the --experimental parameter is required again.Ethiopic
B
3

To modify the settings.json file for max line:

  1. Install the autopep8 vscode extension

  2. Open the vscode settings.json file (for macs, us CMD, P, then type and select settings.json)

  3. Edit the [python] section of the file

The new format goes as this.

"[python]": {
    "editor.defaultFormatter": "ms-python.autopep8",
},
"autopep8.args": [
    "--max-line-length",
    "120"
]
Bursitis answered 20/10, 2023 at 0:41 Comment(1)
This is correct but the "autopep8.args" part needs to be outside the "[python]" object (should be on the same level of it)Pyrethrum
M
3

In the root level of VSCode settings JSON.

{
  .
  .
  "autopep8.args": ["--max-line-length", "180"]
  .
  .
}

But you should know that: these settings change from time to time. So see the autopep8 latest docs.

Monovalent answered 11/11, 2023 at 1:16 Comment(2)
Thank you for your interest in contributing to the Stack Overflow community. This question already has a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Contention
This new solution worked for me using recent VS code studio 1.84.2 (Oct 2023). It seems VS code studio deprecated setting "python.formatting.autopep8Args" and recently migrated linting tools, see github.com/microsoft/vscode-python/wiki/…Dysgenics

© 2022 - 2024 — McMap. All rights reserved.