Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format my Python code with autopep8 using VS Code's format on save feature?

I am trying to format my Python code automatically using the format on save feature in vs Code. I have installed the autopep8 package and added the following configuration to my pyproject.toml file:

[tool.autopep8]
in-place = true
aggressive = 2

when I try like autopep8 test.py it is working. However, when I try to format my code using the format on save feature, I get the following error message:

--in-place cannot be used with standard input

How can I resolve this error and format my code using autopep8 with desired options? and my vs code settings looks like:

  // Python
  "[python]": {
    "editor.defaultFormatter": "ms-python.autopep8",
    
  },
 
"python.formatting.provider": "none",

like image 906
Davood Avatar asked Feb 12 '26 17:02

Davood


1 Answers

Davood, Go ahead and create a .vscode folder in the root of your project. write a settings.json file in there and add the following configuration:

{
  "python.formatting.provider": "autopep8",
  "python.formatting.autopep8Args": [
    "--max-line-length",
    "120"
  ],
  "editor.formatOnSave": true,
  "[python]": {
    "editor.tabSize": 4,
    "editor.insertSpaces": true,
    "editor.rulers": [
      79,
      120
    ],
    "editor.defaultFormatter": "ms-python.autopep8"
  }
}

Hope this helps pal, happy coding!!

like image 199
Alvison Hunter Avatar answered Feb 17 '26 20:02

Alvison Hunter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!