I am facing a formatting issue. The default behavior of python.sortImports
is to organize stdlib, 3rd party, and user modules.
However, it moves my user modules above the 3rd part modules in alphabetical order which is not according to PEP8. And, it follows PEP8 when I use relative imports which is making things more difficult.
My VS Code settings are mentioned below.
"editor.defaultFormatter": "ms-python.python",
"files.autoSave": "onWindowChange",
"python.linting.flake8Enabled": true,
"python.analysis.inlayHints.functionReturnTypes": true,
"python.analysis.inlayHints.variableTypes": true,
"[python]": {
"editor.defaultFormatter": "ms-python.python",
"editor.codeActionsOnSave": {
"source.organizeImports": true,
"python.sortImports": true
}
},
"python.globalModuleInstallation": true,
"python.autoComplete.extraPaths": "/usr/local/lib/python3.10/site-packages/",
"python.analysis.extraPaths": "/usr/local/lib/python3.10/site-packages/",
"isort.check": true,
"isort.args": ["--profile", "django"],
"python.analysis.autoImportCompletions": true,
"python.analysis.packageIndexDepths":
{
"name": "sklearn",
"depth": 2
},
{
"name": "matplotlib",
"depth": 2
},
{
"name": "scipy",
"depth": 2
},
{
"name": "django",
"depth": 3
}
,
"python.analysis.indexing": true,
"python.analysis.completeFunctionParens": true
Am I doing something wrong? Or missing something that is obvious because I have been trying to solve it for the past two days?
Here, is the desired output.
from django.urls import path
from rest_framework.authtoken import views
from rest_framework.urlpatterns import format_suffix_patterns
from customauth.views import ListUsers, RegisterUser
customauth
is a user defined Django app, rest of them are 3rd part as you can see.
Actual output of python.sortImports
.
from customauth.views import ListUsers, RegisterUser
from django.urls import path
from rest_framework.authtoken import views
from rest_framework.urlpatterns import format_suffix_patterns
Note: I have figured out that this is an isort
issue. But do not know why this is happening
CodePudding user response:
And I fixed it myself. For future readers. If a local folder is showing up as a 3rd party library then use the following args in your settings.json file.
{
"isort.args": [
"--profile",
"django",
"--known-local-folder",
"my_folder1",
"--known-local-folder",
"my_folder2",
"--known-local-folder",
"my_folder3"
],
}