December 2nd, 2019

VS Code - sort Python imports automatically

I really like when the VS Code sorts Python imports on file save, it's a perfect addition to the Black code formatter.

VS Code  - sort Python imports automatically

To enable this option, you need the isort utility.

pipenv install isort --dev

Now let's enable it. Open the settings (⇧⌘P or Ctrl+Shift+P), find Preferences: Configure Language Specific Settings... and then find the Python. It will open the settings.json file. The configuration is very simple.

"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}

That's all. Whenever you save the *.py file, the VS Code will run isort automatically to sort the imports.


VS Code documentation: https://code.visualstudio.com/docs/python/editing#_sort-imports

Do you want to support me?

© 2019 Przemysław Kołodziejczyk