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.

Setting `python.sortImports.path` to `isort` or setting it to a relative path does not work from within virtual environment

See original GitHub issue

Error: Either spawn isort ENOENT" is thrown or isort hangs indefinitely.

Workspace Settings

"python.sortImports.path": "isort",

Using Pipenv, I can do this in the VS Code terminal:

$ which isort
/home/username/project/.venv/bin/isort

If I reload the VS Code window with these settings the Python extension gives an error that says ``spawn isort ENOENT` indicating it cannot find isort.

What am I missing to use a custom isort? In https://github.com/microsoft/vscode-python/issues/6369 I understood there is an inner version, but since users of a project can use different editors it’s better to point out the specific version that we install with pipenv.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:21
  • Comments:48 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
mihai-m13commented, Sep 17, 2020

but what if you use e.g. conda or poetry without in-project venvs?

You can use the full paths in those cases

I’m facing an issue with this workaround as well. It appears that when simply using the full path to the binary, isort runs outside of the virtual env in which it was installed, and as such it cannot identify which installed modules from within the env are 3rd party and sorts them improperly. I now have pylint throwing warnings that 3d party libraries aren’t preceding local packages as they should.

When simply calling isort . from the integrated terminal with the env activated it works as it should tho.

7reactions
BLucky-ghcommented, Oct 23, 2021

I think I found out what the issue is:

The extension seems to use an old version of isort since that version still is just a .py script that is passed to the interpreter. but setting python.sortImports.path to isort loads a newer version which is called directly (isort instead of python isort.py or whatever the script’s name was).

This results in the extension passing the arguments after a single dash python isort.py - --diff, while the isort binary (let’s call it a binary from now on for simplicity) doesn’t need the single dash and gets confused by it (it just hangs indefintiely expecting a stream).

Another issue is that the allowed arguments are wildly different: I compared the help text from isort 5.9.3 and whatever version the extension has built in and the allowed argument list is different, and the arguments for the same things differ between the 2 versions. (for instance a lot of the verbose arguments (--max-line-length instead of -l) are not available in the builtin version.

For a fix I suggest either updating the version that the extension has built-in or detecting whether the path set in python.sortImports.path is a python file or a “binary” and remove the single dash accordingly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

VSCode: how to config 'Organize imports' for Python (isort)
The version of isort that is built in to the Microsoft Python plugin doesn't show as isort. Instead it is sortImports.py. It has...
Read more >
Configuration options for isort
Values can be file names, directory names or file paths. To skip all files in a nested path use --skip-glob. Type: List of...
Read more >
Settings Reference for Python - Visual Studio Code
Settings Reference for the Python extension in Visual Studio Code. ... venvFolders, [], Paths to folders where virtual environments are created.
Read more >
coc-pyright - npm
Configuration Description Default pyright.enable Enable coc‑pyright extension true python.analysis.autoSearchPaths Automatically add common search paths like 'src' true python.analysis.typeshedPaths Paths to look for typeshed modules
Read more >
isort - Pants build system
The Python import sorter tool (https://pycqa.github.io/isort/). ... a custom lockfile, set this option to a file path relative to the build root, then...
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