vscode-python: Applying code action 'Sort imports' hangs indefinitely

Environment data

  • VS Code version: 1.47.3
  • Extension version (available under the Extensions sidebar): v2020.7.96456
  • OS and version: Ubuntu 20.04
  • Python version: 3.8.3 (installed by pyenv)
  • Type of virtual environment used: pyenv-virtualenv
  • Relevant/affected Python packages and their versions: XXX
  • Relevant/affected Python-related VS Code extensions and their versions: XXX
  • Value of the python.languageServer setting: doesn’t seem to matter; I have tried Jedi, Microsoft, Pylance, and None
  • isort version: 4.3.21

Expected behaviour

When I hit save on a Python file, I expect it to quickly/automatically sort my imports

Actual behaviour

It pops up a box much like this one:

Saving ‘main.py’: Applying code action ‘Sort imports’.

And that box never goes away. It hangs indefinitely. Obviously it never actually sorts my imports either, nor does it proceed to linting / auto-formatting. It just gets stuck.

Steps to reproduce:

  1. pip install black pylint isort autoflake
  2. Copy my settings.json file
  3. Save a Python file

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

> ~/.pyenv/versions/myproject/bin/python ~/.vscode/extensions/ms-python.python-2020.7.96456/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.7.96456/pythonFiles/sortImports.py - --diff
cwd: ~/src/myproject/home
> ~/.pyenv/versions/myproject/bin/python ~/.vscode/extensions/ms-python.python-2020.7.96456/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.7.96456/pythonFiles/sortImports.py - --diff
cwd: ~/src/myproject/home


About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 25
  • Comments: 53 (2 by maintainers)

Most upvoted comments

I think the issue came back with version 1.56.0 of VS Code, in Windows 10. Anyone else can verified that?

I managed to workaround the problem by adding the following to settings.json (change according to your location of isort script):

“python.sortImports.path”: “${env:HOME}/.local/bin/isort”,

@soapergem, thanks for letting us know about this. It is definitely undesirable that sort-imports-on-save would prevent saving the file. We’ll work on identifying the problem and fixing it.

I am also facing the same issue. I am using vscode 1.47.3. OS Ubuntu 20.04.

Awesome🎉 The fix should be officially out in the next stable release which is scheduled in a week.

(FYI we think https://github.com/microsoft/vscode-python/pull/13717 fixed the issue, we weren’t handling errors thrown by isort correctly, we also upgraded to isort 5.5.2 which might have helped)

To add another data point, I’m experiencing this problem as well, with VSCode version on Mac:

Version: 1.47.3
Commit: 91899dcef7b8110878ea59626991a18c8a6a1b3e
Date: 2020-07-23T13:08:29.692Z (2 wks ago)
Electron: 7.3.2
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Darwin x64 18.5.0

I find it is still possible to format and save the file without sorting the imports by running these two commands from the command palette, which don’t seem to invoke import sorting:

  • Format Document
  • File: Save without Formatting

which tides me over for now. Though it would be cool to know if there’s a potential fix to the hanging “Sort imports”, as it’s awesome to have the editor manage them automatically 😃

yeah I’m getting the sorting of imports hanging on version 1.56.2, I’m using pipenv for my environment

edit: I got it to work by specifying the direct path to isort in my virtual env. For some reason if I only have ‘isort’ as the path and the interpreter pointed to the virtual env I’m working out of, isort does not run even when I manually invoke it.

Had a similar issue which was resolved by setting "python.pythonPath": "/usr/local/bin/python"

Folks would you mind trying out our Insiders build to see if you still have this issue? (View > Command Palette… and run Python: Switch to Insiders Weekly Channel)

Unfortunately I’m unable to reproduce this in 1.56.1 version of VSCode. Do you mind creating a new issue filling the issue template?

@ivasic when it started working, my invocation of isort also hangs when run directly from the terminal (I assume that’s to do with reading from stdin).

One difference is that I installed my own isort into a venv in the workspace directory, so at least on Linux it looks much more like a standard execution of isort (I realize this goes against the advice of using the insiders build of the python extension with its own isort). Assuming you have a virtual environment installed, what happens if you pip3 install isort and then explicitly set "python.sortImports.path": "${workspaceFolder}/venv/bin/isort" in your settings.json?

It only happens when I use anaconda’s python as my interpreter. I was able to fix it by installing miniconda.

Just to note, I’ve only ever used miniconda and I’m seeing this isort issue

This issue got fixed by installing isort in my virtual environment

No it didn’t. That does not fix the issue. In fact, installing isort in your virtual environment is the first step that I posted in the “Steps to Reproduce” above.

This issue got fixed by installing isort in my virtual environment pip install isort Usually vscode asks if it can install black or flake8 or any linter which is part of your settings but in case of isort it just hangs without showing any such message.