vscode-python: Python refactor: Sort imports does not work with setuptools 49.2

Environment data

  • VS Code version: 1.47.0
  • Extension version (available under the Extensions sidebar): 2020.6.91350
  • OS and version: linux Ubuntu 20.04
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.2
  • Type of virtual environment used (N/A | venv | virtualenv | conda | …): venv
  • Relevant/affected Python packages and their versions: isort
  • Value of the python.languageServer setting: Jedi

Expected behaviour

Calling “Python refactor: Sort imports” from command palette sorts import in active document

Actual behaviour

Nothing happens

Steps to reproduce:

  • install setuptools >= 49.2 in workspace venv
  • launch “Python refactor: Sort imports” from command palette

Logs

Devtools console:

Error: /home/me/work/proj/venv/lib/python3.8/site-packages/setuptools/distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.
  warnings.warn(

	at i (/home/me/.vscode/extensions/ms-python.python-2020.6.91350/out/client/extension.js:1)
	at Socket.<anonymous> (/home/me/.vscode/extensions/ms-python.python-2020.6.91350/out/client/extension.js:1)
	at Socket.emit (events.js:203)
	at addChunk (_stream_readable.js:295)
	at readableAddChunk (_stream_readable.js:276)
	at Socket.Readable.push (_stream_readable.js:210)
	at Pipe.onStreamRead (internal/stream_base_commons.js:166)
$onExtensionRuntimeError @ workbench.desktop.main.js:sourcemap:4312

Output pane:

> ~/work/proj/venv/bin/python ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/sortImports.py - --diff
cwd: ~/work/proj

This warning comes from isort that is installed in venv, both versions 4.3.21 and 5.0.9 (latest) produce this warning. Downgrading setuptools to <49.2 makes “Sort imports” command to work ok again - and this warning disappears. I believe this should not behave this way since even with the warning isort exit code is 0.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 14
  • Comments: 20

Most upvoted comments

I ran into this problem.

I was able to “fix” it by editing pythonFiles/sortImports.py

sys.stdin = io.BytesIO(stdin.read())
# End workaround

import setuptools # <— insert this line
import isort.main

isort.main.main()

Removing python.sortImports.path from settings.json fixed the issue for me

We’re going to upgrade isort and make sure it doesn’t break pylint.

Pylint is installed within the user environment itself, which will have it’s own copy of isort. My guess is we pick that isort instead of the one shipped with the extension when using the pylint command, in my case:

~\AppData\Local\Programs\Python\Python37\python.exe c:\Users\karraj\.vscode-insiders\extensions\ms-python.python-2020.6.91350\pythonFiles\pyvsc-run-isolated.py pylint --msg-template='{line},{column},{category},{symbol}:{msg}' --reports=n --output-format=text c:\Users\karraj\Desktop\imports\a.py

but this is something to double check as well.

Downloading isort-5.3.2-py3-none-any.whl (93 kB) |████████████████████████████████| 93 kB 166 kB/s ERROR: pylint 2.5.3 has requirement isort<5,>=4.2.5, but you’ll have isort 5.3.2 which is incompatible.

Fix is already out on insiders. Try the Switch to insiders weekly channel command, which installs the insiders version of the extension.

Upgraded isort. Should be fixed in the next release

Extension version v2020.7.94776

Vendored isort still throws an exception and does not work if setuptools 49.2 is installed in PYTHONPATH.

Custom provided isort 5.1.4 works fine.

can confirm. downgraded setuptools to 49.1 and it works again

@zgoda

The issue with isort has been fixed in 5.1.0 but I believe the extension ships with some older version.

True, isort >5.1.0 fix it. The issue is if someone use Pylint. Pylint is not yet compatible wiht isort 5 and thus lists in its dependencies isort <5. That’s why we are stuck at isort 4.3.21 . https://github.com/PyCQA/pylint/issues/3722

@OutOfFocus4 I first tried your solution, to edit pythonFiles/sortImports.py but sadly it did not work out for me… Then I check and the reason why is becuse I am using my env’s isort "python.sortImports.path": "C:\\Users\\PC-Gil\\Miniconda3\\envs\\forge\\Scripts\\isort.exe" . So I am not reaching the sortImports.py script. I prefer to use my env’s isort so I can control the version I use.

@zgoda @jahan01 I downgraded setuptools to 49.1.3, which remove the error from isort 4.3.21.

Now, I am having a weird behavior: When "python.sortImports.args" is set (I tried a combination of one or more arguments from this list : [ “–multi-line=3”, “–trailing-comma”, “–force-grid-wrap=0”, “–use-parentheses”, “–line-length=119”, ] ), then the sort import is broken (doesn’t work, and if set in “editor.codeActionsOnSave”, prevents saving with infinite message Saving 'filename.py': Applying code action 'Sort imports'.). And it is broken with both isort, from vscode and from my env.

Here the Output pane when I sort with one args set for isort: -With vscode’s isort

> ~\Miniconda3\envs\forge\python.exe c:\Users\PC-Gil\.vscode\extensions\ms-python.python-2020.7.94776\pythonFiles\pyvsc-run-isolated.py c:\Users\PC-Gil\.vscode\extensions\ms-python.python-2020.7.94776\pythonFiles\sortImports.py - --diff --line-length=119
cwd: c:\Users\PC-Gil\Documents\test

-With my env’s isort

> ~\Miniconda3\envs\forge\Scripts\isort.exe - --diff --line-length=119
cwd: c:\Users\PC-Gil\Documents\test

If "python.sortImports.args" is not set, then it work perfectly, (with vscode’s isort as well as with my env’s isort) but with the default isort’s settings…

I read in another issue that we can set isort args in a config file (https://github.com/microsoft/vscode-python/issues/5840#issuecomment-497321419). I tried "python.sortImports.args": ["--settings-path=${workspaceFolder}\\setup.cfg"] and for now it works for me.