How do I get into the environment VS Code is using for pylance?
Asked Answered
A

3

21

I'm using pylance to check my Python code. It tells me

Import "astor" could not be resolved

enter image description here

When I switch to the terminal within VS Code:

enter image description here

I'm pretty certain that the issue is that it uses another environment. I'm using pyenv by default and I would like if vscode would use the same environment. But at the very least I need to be able to access the environment it is using to install packages.

Interestingly, the status bar seems to show something else, because in that environment I have astor installed:

enter image description here

Accredit answered 27/9, 2020 at 12:51 Comment(1)
on macOS, I've got the same issue and resolved by typing /usr/local/Cellar/[email protected]/3.8.5/Frameworks/Python.framework/Versions/3.8/bin/python3.8 /Users/miguel-trejo/.vscode/extensions/ms-python.python-2020.9.111407/pythonFiles/pyvsc-run-isolated.py pip install -U astor --user , I've saw that pyvsc-run-isolated.py uses runpy to execute the current python script and I think that the Cellar folder is specifying the interpreter that vscode is using.Attenuation
J
29

I found this link that informs us that we should add an extra path.

These extra roots must be specified to the language server. The easiest way to do this (with the VS Code Python extension) is to create a workspace configuration which sets python.analysis.extraPaths. For example, if a project uses a sources directory, then create a file .vscode / settings.json in the workspace with the contents:

{
     "python.analysis.extraPaths": ["./sources"]
}

https://github.com/microsoft/pylance-release/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings

Jarl answered 29/10, 2020 at 12:29 Comment(1)
wouldn't this only fix the problem for a single workspace?Owensby
T
9

Another easy way to solve this on VSCODE:

  • ctrl + ','
  • type "extrapaths"
  • Down you should have something like "add element" (I have VSCode on Spanish so in my case I have "Agregar elemento")
  • type './sources/'

Also if you have problems importing local files, you can do the same thing and add your working directory path to solve the problem :) just add 'C: your working directory goes here' in addition to './sources' in the same way ;)

Tittletattle answered 25/5, 2021 at 18:16 Comment(1)
This was a much more correct answer. For a slightly more detailed version on a different question see: https://mcmap.net/q/628074/-vscode-quot-import-x-could-not-be-resolved-quot-even-though-listed-under-help-39-modules-39Owensby
B
1

I found one thing resolved my issue same as yours

  1. Go to your working env(mine is pipenv shell), then pip show 'yourmodule' to check your module is installed or not
  2. If its installed copy Location: path
  3. Go to settings ctrl +','
  4. Type extrapaths
  5. Add Item paste the path string you copied and ok.

We have added actual installed path as additional import search resolution path, so this path will be scanned for imports

Now your module should be resolved. This worked for me.

Boreas answered 7/8, 2021 at 7:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.