Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thernstig opened this issue Aug 21, 2019 · 50 comments
Assignees
Labels
area-intellisense LSP-related functionality: auto-complete, docstrings, navigation, refactoring, etc. bug Issue identified by VS Code Team member as probable bug

Comments

@thernstig
Copy link

thernstig commented Aug 21, 2019

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 #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.

@thernstig thernstig added the triage-needed Needs assignment to the proper sub-team label Aug 21, 2019
@karrtikr karrtikr self-assigned this Aug 21, 2019
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Aug 21, 2019
@karrtikr
Copy link

karrtikr commented Aug 21, 2019

Please make sure you've selected the python interpreter (guessing pipenv one in your case) in vscode, and isort is installed in that environment selected.

Once a interpreter is selected, setting python.pythonPath should show up in settings.json.

@karrtikr karrtikr added the info-needed Issue requires more information from poster label Aug 21, 2019
@thernstig
Copy link
Author

thernstig commented Aug 23, 2019

@karrtikr As you can see from my which isort I have done this, as it points to .venv in my repo. I've already set "python.pythonPath": "${workspaceFolder}/.venv",. So all that has been done.

@karrtikr
Copy link

karrtikr commented Aug 24, 2019

I see. As a workaround try using the full path instead.
"python.sortImports.path": "/home/username/project/.venv/bin/isort",

Let me know if that works. Also can you please paste settings.json.

@thernstig
Copy link
Author

Using full path works! This works as well "python.sortImports.path": "${workspaceFolder}/.venv/bin/isort"

Question is then, is it a bug that just "python.sortImports.path": "isort" does not work?

@karrtikr
Copy link

If you paste your settings and logs as mentioned in the issue template, i will be able to investigate more.

@thernstig
Copy link
Author

thernstig commented Aug 28, 2019

Environment data

  • VS Code version: 1.38.0-insider (user setup)
  • Extension version (available under the Extensions sidebar): 2019.8.30787
  • OS and version: Ubuntu 16.04
  • Python version (& distribution if applicable, e.g. Anaconda): 3.6.9
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): Pipenv
  • Relevant/affected Python packages and their versions: XXX
  • Jedi or Language Server? (i.e. what is "python.jediEnabled" set to; more info How to update the language server to the latest stable version #3977): language server

Actual behaviour

Workspace Settings when it fails:

{
  "editor.formatOnPaste": true,
  "editor.formatOnSave": true,
  "editor.suggest.localityBonus": true,
  "editor.codeActionsOnSave": {
    "source.organizeImports": true
  },
  "files.associations": {
    ".flake8": "ini"
  },
  "python.autoComplete.addBrackets": true,
  "python.jediEnabled": false,
  "python.linting.pylintEnabled": false,
  "python.linting.flake8Enabled": true,
  "python.pythonPath": "${workspaceFolder}/.venv",
  "python.sortImports.path": "isort"
}

Python plugin output:

User belongs to experiment group 'AlwaysDisplayTestExplorer - experiment'
User belongs to experiment group 'ShowPlayIcon - start'
User belongs to experiment group 'ShowExtensionSurveyPrompt - enabled'
Starting Microsoft Python language server.
> conda --version
> python3.7 -c "import sys;print(sys.executable)"
> python3.6 -c "import sys;print(sys.executable)"
> python3 -c "import sys;print(sys.executable)"
> python2 -c "import sys;print(sys.executable)"
> python -c "import sys;print(sys.executable)"
> ~/project/.venv/bin/python -c "import sys;print(sys.executable)"
> conda info --json
> pipenv --version
cwd: ~/project
> pipenv --venv
cwd: ~/project
> pipenv --py
cwd: ~/project
> ~/project/.venv/bin/python -m flake8 --format=%(row)d,%(col)d,%(code).1s,%(code)s:%(text)s ~/project/folder1/pythonfile1.py
cwd: ~/project
> ~/project/.venv/bin/python -m flake8 --format=%(row)d,%(col)d,%(code).1s,%(code)s:%(text)s ~/project/folder1/pythonfile1.py
cwd: ~/project
[Info  - 10:03:25 AM] Analysis cache path: /home/username/.cache/Microsoft/Python Language Server
[Info  - 10:03:25 AM] GetCurrentSearchPaths /home/username/project/.venv/bin/python 
[Info  - 10:03:25 AM] Interpreter search paths:
[Info  - 10:03:25 AM]     /home/username/project/.venv/lib/python3.6
[Info  - 10:03:25 AM]     /home/username/project/.venv/lib/python3.6/lib-dynload
[Info  - 10:03:25 AM]     /home/username/.pyenv/versions/3.6.9/lib/python3.6
[Info  - 10:03:25 AM]     /home/username/project/.venv/lib/python3.6/site-packages
[Info  - 10:03:25 AM] User search paths:
##########Linting Output - flake8##########
[Info  - 10:03:27 AM] Microsoft Python Language Server version 0.3.66.0
[Info  - 10:03:27 AM] Initializing for /home/username/project/.venv/bin/python
> conda info --json
> conda --version
> pipenv --version
cwd: ~/project
> pipenv --venv
cwd: ~/project
> pipenv --py
cwd: ~/project
> isort ~/project/folder1/pythonfile1.py --diff
Error: spawn isort ENOENT
> ~/project/.venv/bin/python -m autopep8 --diff ~/project/folder1/pythonfile1.py
cwd: ~/project
> ~/project/.venv/bin/python -m autopep8 --diff ~/project/folder1/pythonfile1.py
cwd: ~/project
> ~/project/.venv/bin/python -m flake8 --format=%(row)d,%(col)d,%(code).1s,%(code)s:%(text)s ~/project/folder1/pythonfile1.py
cwd: ~/project
> ~/project/.venv/bin/python -m flake8 --format=%(row)d,%(col)d,%(code).1s,%(code)s:%(text)s ~/project/folder1/pythonfile1.py
cwd: ~/project
##########Linting Output - flake8##########

@karrtikr
Copy link

karrtikr commented Sep 2, 2019

Thanks for providing the logs.

Try running the commands listed in log in the terminal, given your logs I expect running isort ~/project/folder1/pythonfile1.py --diff to throw an error for you.

If running it in CLI works, but the extension throws error, then it can be considered an extension issue.
But if simply running isort ~/project/folder1/pythonfile1.py --diff in terminal from within environment fails for you, the issue is not related to the extension.

Please let me know how it goes.

@tmm
Copy link

tmm commented Sep 5, 2019

@karrtikr Not to steal @thernstig's thunder, I have the same problem and ran isort from the cli — it works as expected.

The ${workspaceFolder}/.venv/bin/isort solution also worked for me too.

@thernstig
Copy link
Author

@karrtikr Running isort ~/project/folder1/pythonfile1.py --diff from the terminal does not show any error. I tried doing so with both the settings "python.sortImports.path": "isort", and "python.sortImports.path": "${workspaceFolder}/.venv/bin/isort",. Neither combination showed an error in the terminal for the above command.

@karrtikr karrtikr changed the title "python.sortImports.path": "isort" not working with Pipenv - gives "spawn isort ENOENT" Having setting "python.sortImports.path": "isort" gives "spawn isort ENOENT" from within virtual environment Sep 13, 2019
@karrtikr
Copy link

I am able to confirm this is an issue from within virtual environment.

@karrtikr karrtikr added feature-refactoring bug Issue identified by VS Code Team member as probable bug and removed info-needed Issue requires more information from poster triage labels Sep 13, 2019
@brettcannon brettcannon added area-intellisense LSP-related functionality: auto-complete, docstrings, navigation, refactoring, etc. and removed feature-refactoring labels Apr 9, 2020
@xNathan
Copy link

xNathan commented May 14, 2020

I also got "spawn isort ENOENT" when I set:

"python.sortImports.path": "~/.local/bin/isort"

but full path works for me:

"python.sortImports.path": "/Users/nathan/.local/bin/isort"

@iutlu
Copy link

iutlu commented May 14, 2020

Not sure about ~/ but #4922 seems relevant for the full path requirement.

@karrtikr
Copy link

@xNathan does using ~/ works with other settings? For eg. python.pythonPath or some other setting that carries path.

@xNathan
Copy link

xNathan commented May 18, 2020

@karrtikr ~/ works for black, flake8, mypy, etc.

@jaklan
Copy link

jaklan commented Aug 14, 2020

Very annoying issue. ${workspaceFolder}/.venv/bin/isort works, but what if you use e.g. conda or poetry without in-project venvs? I would like to keep workspace settings.json files in git to share them with dev teams, but for projects as above it's impossible to make it work for isort, because local paths differ from each other.

@thernstig
Copy link
Author

@smitthakkar1 that does not work if users have different locations for the virtual env

@BLucky-gh
Copy link

BLucky-gh commented 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.

@den-is
Copy link

den-is commented Nov 27, 2021

Wow... that looks like an ancient issue. Hope someone picks it and fixes. 😞

I'm using it in conjunction with black - and it is ugly to watch how they fight for setting style.

I'm using isort 5.10.1 installed using Pipx ob Debian 11 in a Remote Container workflow.

Below are possible settings I have in my vscode settings - both work just fine.

                 // individual isort setup using pipx
		//"python.sortImports.path": "/usr/local/py-utils/bin/isort",

                 // isort installed as a dependency during of flake8-isort. injected into flake8 pipx setup.
		"python.sortImports.path": "/usr/local/py-utils/venvs/flake8/bin/isort",

Was able to fix the setup by including .isort.cfg in the project's root.

[settings]
profile=black

Got excellent compatibility with black formatter.

@baregawi
Copy link

Does the vscode team hear about these bug reports? It's a bit odd to just let this things linger for several years. I would think Microsoft has a decent amount of engineering capacity.

@jaklan
Copy link

jaklan commented Feb 16, 2022

@baregawi well, when you look at contributors stats you will see there's only 9 people with more than 100 commits and half of them were not even active recently. We can try to mention @brettcannon or @karrtikr as it's still really annoying and prevent sharing a proper VSCode config with colleagues.

@baregawi
Copy link

Wow, I feel bad. A few warriors are maintaining vscode. I thought there would be much more resources behind it. I really like it. Is there any way I can help with some contributions?

@jaklan
Copy link

jaklan commented Feb 16, 2022

@baregawi

  1. Not VSCode, just the Python extension.
  2. Everything is in main README.md:
    https://github.com/microsoft/vscode-python#questions-issues-feature-requests-and-contributions

@brettcannon
Copy link
Member

Does the vscode team hear about these bug reports?

Yes we do. Actually the entire team sees every bug report (we triage as a team in our stand-up).

It's a bit odd to just let this things linger for several years.

Not when you have 262 bugs to deal with on top of large amounts of technical debt to clear out. And this is the 8th most requested bug to fix, so it isn't at the top of the list. So I expect we will get to it eventually, but we have other stuff to work through first.

We can try to mention @brettcannon or @karrtikr as it's still really annoying and prevent sharing a proper VSCode config with colleagues.

One thing to know is we are in the process of breaking out tool support into their own extensions, and that includes isort. We are putting the tools behind LSP for performance, but it also gives us the chance to rewrite the code from scratch. Based on how we have been structuring things, I suspect that will fix this issue implicitly.

Is there any way I can help with some contributions?

Sure! You can check out https://github.com/microsoft/vscode-python/wiki/Submitting-a-pull-request and the wiki for contributing. Otherwise you can wait for the new extension and give it a try when we announce it.

@webventurer
Copy link

I had this issue just now. What worked for me was to create a venv, install the 'isort' module and add this to settings.json:

"python.sortImports.path": "venv/bin/isort"

i.e. pointing directly to the 'isort' module inside the 'venv'.

Visual Studio Code - Insiders 1.65.0

@jaklan

This comment was marked as off-topic.

@thernstig
Copy link
Author

I had this issue just now. What worked for me was to create a venv, install the 'isort' module and add this to settings.json:

"python.sortImports.path": "venv/bin/isort"

This has been deducted working already as a workaround, see #7042 (comment)

The issue is about using the one found in the env if it is in the PATH. Having a relative path is not great in case users share a project VS Code config and users place their venv in different places.

@thernstig
Copy link
Author

I am confused about this bug in relation to https://marketplace.visualstudio.com/items?itemName=ms-python.isort

According to that extension from Microsoft, it says:

The bundled isort is only used if there is no installed version of isort found in the selected python environment.

But I have not seen that be the case for this extension where one has to set "python.sortImports.path".

  1. Why is it that this extension does not use the default isort found in the env, but instead requires "python.sortImports.path" to be set?
  2. I am not sure if you are involved in the new ms-python.isort, but why would one need that one if one already has this extension? Seems weird to focus on a separate isort-only extension imo.

@jeremyn
Copy link

jeremyn commented May 10, 2022

@thernstig

Why is it that this extension does not use the default isort found in the env, but instead requires "python.sortImports.path" to be set?

FYI, I asked a similar question just yesterday at #4922 (comment) regarding the similar Black extension.

@brettcannon
Copy link
Member

  1. Why is it that this extension does not use the default isort found in the env, but instead requires "python.sortImports.path" to be set?

If you're asking why isort is different compared to other supported tools in this extension, it's historical. This extension has shipped isort internally since before it came to Microsoft, so how it handles environment-installed isort has simply always been treated differently.

2. I am not sure if you are involved in the new ms-python.isort

It's from our team, so we are.

2. but why would one need that one if one already has this extension? Seems weird to focus on a separate isort-only extension imo.

Because we will be removing isort support from this extension once the standalone isort extension is deemed to be working well. The separate extension has much better performance thanks to being behind LSP and thus being able to keep a Python process running, we can update isort in the separate extension more easily (i.e. you can independently roll it back if you don't like an updated version compared to this extension), and we don't have to ship the same bits over and over again like we do in this extension that don't change constantly.

@thernstig
Copy link
Author

@brettcannon then I suppose it makes no sense to work on this feature here, if you intend to remove python.sortImports.path from this extension and instead rely on the extension ms-python.isort, right? If so I think you can close this issue with that reason, since it does not make sense for you to waste time on that.

@Greg7000
Copy link

Greg7000 commented Nov 23, 2022

Anyone knows how to dynamically link to isort from selected python interpreter? VsCode used to update the "python.pythonPath" variable in settings.json when changing the python interpreter and detect everything properly but does not seem to do it anymore.

For now my work around is to use system isort but I hate this solution, I would like to use the one from the selected python interpreter (wihout having to manually change settings.json manually each time).

I don't want a relative path solution, really looking for using the selected python interpreter.

@thernstig
Copy link
Author

@Greg7000 does #7042 (comment) not work anymore?

@Greg7000
Copy link

I am not in the #7042 (comment) context.

I often switch the selected python interpreter (from VsCode) menu and I can't use relative path since I don't put my virtual env at my project level (using miniconda specific env folder instead).

@Greg7000 does #7042 (comment) not work anymore?

@Greg7000
Copy link

I am not in the #7042 (comment) context.

I often switch the selected python interpreter (from VsCode) menu and I can't use relative path since I don't put my virtual env at my project level (using miniconda specific env folder instead).

@Greg7000 does #7042 (comment) not work anymore?

Finally the only way I managed to deal with this issue is to keep "python.sortImports.path": "isort" which will work no matter the selected python interpreter if you install isort in the base env (you mignt need admin privileges to do so).

@thernstig
Copy link
Author

Close this?

The new isort extension https://github.com/microsoft/vscode-isort has the setting "isort.importStrategy": "fromEnvironment" which I assume should "fix" this.

@github-actions github-actions bot removed the needs PR Ready to be worked on label Oct 25, 2023
@brettcannon
Copy link
Member

@thernstig we are actually planning to go through the repo to clean up all the linter and formatter issues once we dropped built-in support, but thanks to the nudge I've gone ahead and close this now.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-intellisense LSP-related functionality: auto-complete, docstrings, navigation, refactoring, etc. bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests