How to ignore formatting imports with vscode and black by shortcut?

79 Views Asked by At

Can Black be configured to ignore imports?

In this question, they added

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

in setting.json to control whether using black to format imports or not. But I like to use shortcut in vscode (opt+shift+f) to format my code, because I need to select if implementing black in my code and I must need to save files at the same time. It seems this json doesn't work for shortcuts in vscode.

I added the

"[python]": {
        "editor.codeActionsOnSave": {
            "source.organizeImports": false

in my setting.json. But when I use shortcut to format imports, it still doesn't work.

I want to use black for code and isort for imports with shortcuts to choose if implementing them.

1

There are 1 best solutions below

0
MingJie-MSFT On

You could try to set isort as your preferred import sorter by add the following codes to your settings.json:

    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.codeActionsOnSave": {
            "source.organizeImports.isort": "explicit"
        }
    },