skip to Main Content

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",

2

Answers


  1. You can change your settings like the following codes:

    "python.formatting.provider": "autopep8",
    "editor.formatOnSave": true,
    "python.formatting.autopep8Args": [
      "in-place = true",
      "aggressive = 2"
    ],
    

    You can read document for more detials.

    Login or Signup to reply.
  2. 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!!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search