DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Eric Egli
Eric Egli

Posted on

Quick guide to Python formatting in VS Code

You've followed Microsoft's tutorial on setting up Python in VS Code but formatting is just not working? It might be that other extensions are interfering with your Python formatter.

If you use VS Code mostly for web development, you most likely also use Prettier for formatting. I'm talking about Prettier as an extension, not as a package. Check if you have the following configuration in your settings:

(Open VS Code, hit Ctrl + Shift + P on Windows / Cmd + Shift + P on MacOS to open the Command Palette and search for "Settings", check both "Workspace" and "User" settings).

Prettier does not work with Python

"editor.defaultFormatter": "esbenp.prettier-vscode"
Enter fullscreen mode Exit fullscreen mode

If you find this config in your settings, you've found the reason why autopep8, black or yapf are not working - The above configuration will choose Prettier to format Python, which is not supported. In your settings, make sure you override the default formatter for python like so:

  "[python]": {
    "editor.defaultFormatter": null
  }
Enter fullscreen mode Exit fullscreen mode

And don't forget to install and select the actual formatter that you want (just like in the official docs).

  "python.formatting.provider": "black",
  "python.formatting.blackArgs": ["--line-length", "120"]
Enter fullscreen mode Exit fullscreen mode

Override default formatter

You can either overwrite editor.defaultFormatter just for your workspace or globally for your user. Also, it doesn't matter whether you're using a global Python installation or a virtual environment.

I prefer applying this configuration to my user settings - this way, I can easily start a Python project and will get formatting out of the box based on my user settings. No need to create dedicated workspace settings for every single project and override the formatter over and over. Here's the relevant config from my settings:

{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "[python]": {
    "editor.defaultFormatter": null
  },
  "python.formatting.blackArgs": ["--line-length", "120"],
  "python.formatting.provider": "black",
}
Enter fullscreen mode Exit fullscreen mode

Originally posted on my personal website and blog, eric.film.

Preview picture by @fidpad on unsplash.

Top comments (1)

Collapse
 
venubanaras profile image
Mayank Singh

Thanks for this article. My problem was solved because if this.
Actually I started web development and ihave completed it. But since my main aim was learning computer vision, I wanted to do python programming in VS Code only. I wanted to enable prettier for web development and black for python.
Through your article I put my editor's default editor as prettier and for python I disable it and use black as the editor. I did all this in the JSON based settings .
Great article. Thank you

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.