Wrong order in import when using sort imports in vscode
See original GitHub issueSorting using the Python Refactor: Sort Imports
doesn’t generate a correct output.
When I run the same command in the console the output is correct.
Environment data
- VS Code version: 1.49.0
- Extension version (available under the Extensions sidebar): v2020.9.112786
- OS and version: Ubuntu 18.04
- Python version (& distribution if applicable, e.g. Anaconda): Python 3.6
- Type of virtual environment used (N/A | venv | virtualenv | conda | …): virtualenv
- Value of the
python.languageServer
setting: Pylance
Expected behaviour
When I sort in the console:
~/Development/virtual_env/bin/python ~/.vscode/extensions/ms-python.python-2020.9.112786/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.112786/pythonFiles/sortImports.py src/accounting/lms/utils/xero/book_gt_bank.py
I get:
from datetime import date, datetime
from typing import List
import pytz
from django.conf import settings
from django.utils import timezone
from accounting.lms.utils.xero.base import xero_execute_booking
from transaction.models import Transaction
Actual behaviour
When I sort using the vscode command Python Refactor: Sort Imports
the I get:
from datetime import date, datetime
from typing import List
import pytz
from accounting.lms.utils.xero.base import xero_execute_booking
from django.conf import settings
from django.utils import timezone
from transaction.models import Transaction
Steps to reproduce:
Just try to sort imports as I explained above and it should happen.
Logs
Here are the logs from vscode
> ~/Development/virtual_env/bin/python ~/.vscode/extensions/ms-python.python-2020.9.112786/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.112786/pythonFiles/sortImports.py - --diff
cwd: ~/Development/backend/src/accounting/lms/utils/xero
> ~/Development/virtual_env/bin/python ~/.vscode/extensions/ms-python.python-2020.9.112786/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.112786/pythonFiles/sortImports.py - --diff
cwd: ~/Development/backend/src/accounting/lms/utils/xero
Issue Analytics
- State:
- Created 3 years ago
- Reactions:28
- Comments:14 (3 by maintainers)
Top Results From Across the Web
VS Code's "Sort Imports" fails with exception - Stack Overflow
I tried switching the Language Server, but it does not help. Steps to reproduce: Open a python script with multiple import lines in...
Read more >Easily sort imports and remove unused declarations on each ...
All this time I was using VSCode formatting on save which triggers the selected default formatter (Prettier).
Read more >sort-imports - Visual Studio Marketplace
Sort ES6 imports automatically. Installation. Launch VS Code Quick Open ( Ctrl+P ), paste the following command, and press enter.
Read more >sort-imports - ESLint - Pluggable JavaScript Linter
This rule checks all import declarations and verifies that all imports are first sorted by the used member syntax and then alphabetically by...
Read more >prettier-plugin-organize-imports - npm
Make prettier organize your imports using the TypeScript language service API. ... You should have any import order rules/plugins disabled.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I ran into this and tracked it down a bit. The problem seems to be at https://github.com/microsoft/vscode-python/blob/648952899d5f067f2156e9ce8b8a003e5bc26c7e/src/client/providers/importSortProvider.ts#L177 where the
cwd
is set to the directory the Python source file lives in, vice say${workspaceFolder}
For me, a solution was setting as follows
However a patch to
importSortProvider.ts
to defaultcwd
toworkspaceFolder
might work better (but I don’t know other gotchas)As @juliussimonelli suggests:
by adding the following to vscode setting.json resolved the issue, at least for me.