Visual Studio Code with Python auto-formatting

I was writing python code and facing issues with formatting. It becomes a problem with code review and toolings to detect these issues. Life is much easier with formatter integrated with your code editor and auto-fix those issues on save. Here is how to do:

  1. First, install yapf from Google as a formatter:
pip install yapf

2. Next, in your Visual Studio Code editor, press “Command + Shift + P” for mac for “Ctrl + Shift + P” for linux, and type to search “Open settings (JSON) and add this lin

"python.formatting.provider": "yapf"

3. If you want to auto format on save instead of just tips in editor, add this setting as well:

"editor.formatOnSave": true

4. Optionally, if you want to use your project .styles.yapf instead of global styles, add this line as well:

"python.formatting.yapfArgs": [ "--style", ".style.yapf" ]

Now you can test it out, such as not having a new line at the end of the python file, then press saves, it would fix your new line issue automatically for you.

Written by Victor Leung who is a keen traveller to see every country in the world, passionate about cutting edge technologies. Get in touch


Originally published at https://victorleungtw.com.

By Victor Leung

Experience in software development, consulting services and technical product management. Understanding of business and technology with an MBA in Finance and a Master degree in Computer Science. AWS Certified Solution Architect with experience in building products from scratch and serving as a charismatic leader.

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.