15

In vscode I want to use Prettier as my default formatter, but not for Python, where I will just use autopep8. I have the following settings now:

{
  "workbench.iconTheme": "vscode-icons",
  "workbench.editorAssociations": [
    {
      "viewType": "jupyter.notebook.ipynb",
      "filenamePattern": "*.ipynb"
    }
  ],
  "git.confirmSync": false,
  "editor.formatOnPaste": true,
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "python.formatting.provider": "autopep8",
  "explorer.confirmDelete": false,
  "python.showStartPage": false,
  "explorer.confirmDragAndDrop": false
}

When I save a python file, it gives me the message: "Extension 'Pretier - code formatter cannot format etc...'. So, apparently it still uses the wrong formatter for python files. How do I change this?!

4 Answers 4

35

If I disabled Prettier as the default formatter, it would not format on save anymore, but my Python would be formatted by autopep8 on save. With this in mind, the following solution worked for me to have both Prettier working for other languages and autopep8 for Python:

{
  "workbench.iconTheme": "vscode-icons",
  "workbench.editorAssociations": [
    {
      "viewType": "jupyter.notebook.ipynb",
      "filenamePattern": "*.ipynb"
    }
  ],
  "git.confirmSync": false,
  "editor.formatOnPaste": true,
  "editor.formatOnSave": true,
  "python.formatting.provider": "autopep8",
  "explorer.confirmDelete": false,
  "python.showStartPage": false,
  "explorer.confirmDragAndDrop": false,
  "python.linting.pylintArgs": ["--load-plugins=pylint_django"],
  "javascript.updateImportsOnFileMove.enabled": "always",
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "[python]": {
    "editor.defaultFormatter": "ms-python.python"
  }
}

Let me know if somebody finds a better solution!

4
  • this is perfect! thanks a lot Sep 20, 2021 at 5:21
  • thx for the tips, i was banging my head off
    – jmnguye
    Dec 21, 2021 at 9:11
  • thanks a lot! i looked a solution for this problem from long time. :) Jan 29, 2022 at 16:43
  • thanks a lot. I am looking for this as well. can you tell me where should i add this config in? I am a newbie to VSCode. Thanks.
    – lcb
    Feb 20, 2022 at 15:50
9

Meaningful config snippet from @round_circle's answer:

"[python]": {
    "editor.defaultFormatter": "ms-python.python"
  }

After adding it, autopep8 worked for python files.

2
  • 1
    This was it -- I needed to edit settings.json in my .vscode folder, for those looking for where to edit this.
    – Aerodyno
    Jan 27, 2022 at 17:37
  • Using this in v1.83.1 causes VSCode to show a notification stating: "You have deprecated linting or formatting settings for Python. Please see the logs for more details." Which in the logs state: "Formatting features have been moved to separate formatter extensions. See here for more information: code.visualstudio.com/docs/python/formatting" which indicates that you must install an extension to enable formatting. The previous default of autopep8 would then need to be installed and the line would be updated to "editor.defaultFormatter": "ms-python.autopep8",
    – Swirle13
    Nov 13 at 23:01
5

You can do what @round_circle did as that should work. If you don't want to set an initial provider e.g you might want to use Black instead, you can also add in to your settings.json:

"[python]": {
    "editor.defaultFormatter": null
  },

I happen to be using the Python extension from Microsoft and defaults to autoPep8 and this will prompt you to set up the formatter you want to use. photo of the prompt to add formatter You can then add your provider in the settings.json by adding:

"python.formatting.provider": <your_formatter>

This can be referenced here as well: VS Code Formatting

0

In VSCode, Python's "Formatting" is provided by the Python extension. Therefore, for Python, please use the formatting from the Python extension (for example: autopepe8) to avoid using repeated formatting settings so that they do not work.

enter image description here

Reference: Formatting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.