question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong order in import when using sort imports in vscode

See original GitHub issue

Sorting 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:closed
  • Created 3 years ago
  • Reactions:28
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

67reactions
mariadb-JeffBachtelcommented, Oct 14, 2020

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 image

However a patch to importSortProvider.ts to default cwd to workspaceFolder might work better (but I don’t know other gotchas)

18reactions
alexanderdevmcommented, Nov 12, 2020

As @juliussimonelli suggests:

"python.sortImports.args": [
        "--src=${workspaceFolder}"
    ]

by adding the following to vscode setting.json resolved the issue, at least for me.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found