Making isort compatible with black#
Both isort and black are a must have in my python life, but with their default settings, I will get different imports formats.
Note
Update 2020-12-06, thanks to Christian Jauvin's comment, since isort v5, it has introduced --profile=black
option, so the life is much easier now.
multi_line_output, include_trailing_comma and line_length#
The main difference between isort and black are on there points:
- the multi line mode
- the trailing comma of the last import
- the max line length
Personally, I prefer making isort compatible with black, so the settings to be used with isort is: isort -m 3 -tc
As per isort settings wiki:
-m 3
standards for multi line mode 3, which isVertical Hanging Indent
-tc
standards for adding trailing comma for each import including the last one
There's also a param -w 88
to set the max line length to 88, but with multi line mode 3, we rarely need it.
There's also a param -rc
to recursively sort on all files in the project.
We can also use isort custom profile to overwrite the default settings as shown here. And to use the custom profile in VSCode:
# https://github.com/microsoft/vscode/issues/83586#issuecomment-557334564
"python.sortImports.args": [
"--settings-path=${workspaceFolder}/setup.cfg"
]
!!! note
```
## isort with VSCode
isort v5-:
[https://pycqa.github.io/isort/docs/configuration/profiles/](https://pycqa.github.io/isort/docs/configuration/profiles/)
```json
{
"editor.formatOnSave":true,
"python.sortImports.path": "isort",
"python.sortImports.args":[
"-m 3",
"-tc",
],
"[python]":{
"editor.codeActionsOnSave":{
# it was `"source.organizeImports": true` in my first version of this post,
# see below comment for explanation.
"source.organizeImports.python": true
}
}
}
isort v5+:
{
"editor.formatOnSave":true,
"python.sortImports.path": "isort",
"python.sortImports.args":[
"--profile=black",
],
"[python]":{
"editor.codeActionsOnSave":{
# it was `"source.organizeImports": true` in my first version of this post,
# see below comment for explanation.
"source.organizeImports.python": true
}
}
}
After some days of using above settings, I found a very frustrating behavior that when I pressed Ctrl+S multiple times to save manually a python file, the imports part changed upon each save, and sometimes it even deleted some imports... Digged in github, people have already reported the issue. See issues/83586, and issues/9889
Warning
The solution (workaround) is here. Replace "source.organizeImports":true
by source.organizeImports.python
to allow codeActionsOnSave to specify which extension to use for a given on save action, the way editor.defaultFormatter
or python.formatting.provider
work.
isort with git hook#
Just in case you're interested in git hook, the settings is here.
Update 2021-03-28: using git pre-commit.