reorder-python-imports

VSCode extension to sort and refactor python imports using reorder-python-imports.

Unlike other import organizers, reorder-python-imports focuses on reducing the frequency of merge conflicts, using static analysis to avoid the need for an active venv, and providing better better compatibility with pre-commit and black.

To learn more, visit the original tool's homepage.

Features

Refactoring the imports is provided as a code action, which can be invoked by right clicking on the code or the lightbulb icon and selecting Reorder Imports, or selecting Python Refactor: Reorder Imports from the command palette.

context-menu-example

Settings

Below is an example of a settings.json file with settings relevant to vscode-reorder-python-imports.

{
    "reorder-python-imports.args": [
        "--application-directories=.:src",
        "--add-import 'from __future__ import absolute_import'",
        "--add-import 'from __future__ import division'",
        "--add-import 'from __future__ import print_function'"
  ]
}

Reordering on Save

Reordering imports on save is also supported, but requires you to set the following in your settings to prevent Microsoft's python extension from running isort:

"[python]": {
    "editor.codeActionsOnSave": {
        "source.organizeImports": false,
        "source.organizeImports.reorder-python-imports": true
    },
}

Requirements

reorder-python-imports must be installed in the venv used by visual studio.

GitHub

https://github.com/TheButlah/vscode-reorder-python-imports