How can I set max line length in vscode for python?

Question:

For JavaScript formatter works fine but not for Python. I have installed autopep8 but it seems that I can’t set max line length. I tried this:

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

and my settings.json looks like this:

{
  "workbench.colorTheme": "One Dark Pro",
  "git.autofetch": true,
  "workbench.iconTheme": "material-icon-theme",
  "git.enableSmartCommit": true,
  "terminal.integrated.shell.windows": "C:\WINDOWS\System32\cmd.exe",
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[html]": {
    "editor.defaultFormatter": "vscode.html-language-features"
  },
  "javascript.updateImportsOnFileMove.enabled": "always",
  "[javascriptreact]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "liveServer.settings.donotShowInfoMsg": true,
  "editor.formatOnSave": true,
  "window.zoomLevel": 1,
  "vscode-w3cvalidation.validator-token": "Fri, 07 Aug 2020 07:35:05 GMT",
  "python.formatting.provider": "autopep8",
  "python.formatting.autopep8Args": [
    "--max-line-length",
    "79",
    "--experimental"
  ],
  "python.autoComplete.addBrackets": true,
  "python.autoComplete.extraPaths": []
}

Any ideas how to fix that?

Asked By: Jan Ponichter

||

Answers:

From autopep8-usage, the default value of max-line-length is 79, so you can change to other value and have a try.

About the effect of autopep8 in vscode, I made a test with the same settings as yours, like the following screenshot shows:

enter image description here

every print sentence line-length is over 79, the first and the second print() parameters are expressions, and the setting works for the first one, not for the second. This is because setting applicable rules are provided by python extension and it has own caculation mechanism.

When it comes to print strings, the setting doesn’t work, so if you mean this in your question, you can add the following code in user settings.json.

"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn": 79
Answered By: Molly Wang-MSFT
"python.formatting.autopep8Args": [
    "--line-length",
    "119"
]

works for me

Answered By: Phuhui

Specifically for autopep8:

    "autopep8.args": [
        "--max-line-length", "119"
    ],

This is according to https://github.com/microsoft/vscode-autopep8

Answered By: kolis