Vscode-python: Wrong order in import when using sort imports in vscode

Created on 5 Oct 2020  ยท  9Comments  ยท  Source: microsoft/vscode-python

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

area-formatting investigating type-bug

Most helpful comment

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)

All 9 comments

@lfrodrigues, thanks for letting us know about this. We'll look into this as soon as we can. In the meantime, please provide the content of your settings.json file.

I believe the cwd is wrong when isort is executed by VSCode, whereas it's using the correct one when executed in the console. This affects the implied PYTHONPATH, causing isort to be unable to find the local top-level modules.

The following example uses no workspace settings. My user settings that apply to Python follow:

{
  "python.formatting.blackArgs": ["-l 90"],
  "python.formatting.blackPath": "/home/flyte/.local/bin/black",
  "python.formatting.provider": "black",
  "python.linting.pylintArgs": ["--max-line-length=100"],
  "python.linting.flake8Args": ["--max-line-length=100"],
  "python.sortImports.args": [
    "--line-length=90",
    "--use-parentheses",
    "--trailing-comma",
    "--multi-line=3"
  ],
  "python.pythonPath": "/usr/bin/python3",
  "python.dataScience.askForKernelRestart": false
}

Directory Structure:

.
โ”œโ”€โ”€ bar
โ”‚   โ””โ”€โ”€ __init__.py
โ””โ”€โ”€ foo
    โ””โ”€โ”€ __init__.py

bar/__init__.py:

import os
import requests
import foo

pass

Sorting imports from VSCode in bar/__init__.py causes isort to have a cwd of ./bar:

> ~/dev/isort-vscode-issue-14254/.ve/bin/python3.6 ~/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/sortImports.py - --diff --line-length=90 --use-parentheses --trailing-comma --multi-line=3
cwd: ~/dev/isort-vscode-issue-14254/bar

and subsequently sorts the imports as follows:

import os

import foo
import requests

pass

If we execute isort in the console, from the top-level of the package:

(.ve) flyte@demosthenes:~/dev/isort-vscode-issue-14254
$ ~/dev/isort-vscode-issue-14254/.ve/bin/python3.6 ~/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/sortImports.py bar/__init__.py --line-length=90 --use-parentheses --trailing-comma --multi-line=3
Fixing /home/flyte/dev/isort-vscode-issue-14254/bar/__init__.py

then the imports are sorted correctly:

import os

import requests

import foo

pass

If we cd to the bar directory first, then run the same command:

(.ve) flyte@demosthenes:~/dev/isort-vscode-issue-14254
$ cd bar/
(.ve) flyte@demosthenes:~/dev/isort-vscode-issue-14254/bar
$ ~/dev/isort-vscode-issue-14254/.ve/bin/python3.6 ~/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/sortImports.py __init__.py --line-length=90 --use-parentheses --trailing-comma --multi-line=3
Fixing /home/flyte/dev/isort-vscode-issue-14254/bar/__init__.py

then the imports are sorted wrong again:

import os

import foo
import requests

pass

Here's my settings.json

{
  "files.exclude": {
    "**/.git": true,
    "**/.svn": true,
    "**/.hg": true,
    "**/CVS": true,
    "**/.DS_Store": true,
    "**/__pycache__": true
  },
  "editor.rulers": [120],
  "extensions.ignoreRecommendations": false,
  "window.zoomLevel": 0,
  "python.formatting.provider": "black",
  "python.formatting.blackArgs": [
    "--line-length",
    "120",
    "--skip-string-normalization"
  ],
  "editor.formatOnType": true,
  "editor.formatOnPaste": false,
  "editor.formatOnSave": true,
  "breadcrumbs.enabled": true,
  "python.linting.prospectorEnabled": true,
  "python.linting.enabled": true,
  "python.linting.pylintEnabled": false,
  "python.testing.unittestEnabled": false,
  "python.testing.pytestEnabled": false,
  "python.testing.nosetestsEnabled": false,
  "editor.suggestSelection": "first",
  "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
  "vsintellicode.python.completionsEnabled": true,
  "[json]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[html]": {
    "editor.defaultFormatter": "vscode.html-language-features"
  },
  "workbench.iconTheme": "vscode-icons",
  "vsicons.dontShowNewVersionMessage": true,
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },

  "search.searchOnType": false,
  "python.languageServer": "Pylance",
  "python.analysis.extraPaths": [
    "a_path_here"
  ]
}

I encountered same problem. I found that the isort package itself behaves differently when I was using isort==4.3.8 vs sort==5.5.3. If I call isort==4.3.8 in command line, everything is as expected. I think VSCode bumped the isort version it internally uses, so you see this incorrect order of imports.

In your settings.json, try to add this and see if it works for you:

"python.sortImports.args": [
        "-p",
        "accounting",
        "-p",
        "transaction"
    ],

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)

@mariadb-JeffBachtel It worked for me. however this setting is no where documented and is a necessary for using isort in vscode(at first the behavior is different and you don't know why or In workspaces having code in src like my case setting to that folder worked). maybe documenting this for troubleshooting also helps(and less risky than the patch).

Yeah, I'm curious what @ericsnowcurrently thinks is the best way forward, but I'm glad it worked for you as a workaround

For me, the solution by @mariadb-JeffBachtel partially solves my problem, but not completely. I wondering if the root of the problem is that I installed my modules with pip install -e .. Here's what I'm seeing. If I am inside a file of my_module and have no arguments in python.sortImports.args and try to sort my imports, I get:

import argparse

import numpy
from my_module.utils import func
from my_other_module import func2

from ..helpers import func3

If I add

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

to my settings (per @mariadb-JeffBachtel 's suggestion ) and then try to sort imports, I get:

import argparse

import numpy
from my_other_module import func2

from my_module.utils import func

from ..helpers import func3

So now it has designed the module I'm in (my_module) from the third-party libraries, but not my_other_module. I still don't know how to get it to do that.

As @juliussimonelli suggests:

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

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

Was this page helpful?
4 / 5 - 1 ratings