Python AutoPep8 formatting not working with max line length parameter
Question:
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,
}
Answers:
experimental worked for me
"python.formatting.autopep8Args": ["--max-line-length", "120", "--experimental"]
This should work –
"python.formatting.provider": "autopep8",
"python.formatting.autopep8Args": [
"--max-line-length",
"120",
"--experimental"
]
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
Now go back to the code and select the necessary code to format the line wrap at 120 characters
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,
}
experimental worked for me
"python.formatting.autopep8Args": ["--max-line-length", "120", "--experimental"]
This should work –
"python.formatting.provider": "autopep8",
"python.formatting.autopep8Args": [
"--max-line-length",
"120",
"--experimental"
]
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
Now go back to the code and select the necessary code to format the line wrap at 120 characters