round_circle
round_circle

Reputation: 1015

Problem formatting python when using Prettier in vscode

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?!

Upvotes: 20

Views: 69691

Answers (5)

rhry
rhry

Reputation: 1

{
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "editor.formatOnSave": false,
    "prettier.printWidth": 400,   // Increase line length to 100 characters
    "prettier.tabWidth": 2,
    "prettier.useTabs": false,
    "prettier.semi": true,
    "prettier.singleQuote": true,
    "prettier.trailingComma": "es5",
    "prettier.bracketSpacing": true,
    "prettier.bracketSameLine": false,
    "prettier.arrowParens": "always",
    "prettier.proseWrap": "preserve",
    "prettier.endOfLine": "lf"
  }

Upvotes: 0

TitanRain
TitanRain

Reputation: 175

Meaningful config snippet from @round_circle's answer:

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

After adding it, autopep8 worked for python files.

Upvotes: 12

user8378204
user8378204

Reputation: 51

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

Upvotes: 5

round_circle
round_circle

Reputation: 1015

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!

Upvotes: 41

Jill Cheng
Jill Cheng

Reputation: 10354

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.

Upvotes: 0

Related Questions