I always prefer to keep the code following the standards and style guides defined by the language or the community that has become popular. Doing so will improve readability, and consistency when coding collaboratively.
Ultimately we can focus on actual code and not spend time and worry about styling.
For Python, my favorite one is Black which is an opinionated PEP8-compliant formatter. In addition, I use isort, which sorts our imports, again no time is wasted there.
In this article, I’ll show how to install and configure it in your VSCode.
Install Black and isort
In VSCode Extensions, install and enable below:
- Black Formatter (Microsoft)
- isort (Microsoft)
Configure Auto-Format On Save
This will automatically format your code when you save your code. Ctrl+S
.
Make sure you have auto-format on save enabled.
Hit Ctrl + Shift + P
to open the command palette. And type Open User Setting (JSON)
.
Update Python section and isort configuration as below.
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnType": true,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
},
"isort.args": [
"--profile",
"black"
]
Leave a Reply