16

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

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • on macOS, I've got the same issue and resolved by typing `/usr/local/Cellar/python@3.8/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. – Miguel Trejo Sep 27 '20 at 14:28

3 Answers3

19

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

7

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 ;)

  • 2
    This was a much more correct answer. For a slightly more detailed version on a different question see: https://stackoverflow.com/a/72322430/4808079 – Seph Reed May 20 '22 at 17:02
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.

Priya
  • 143
  • 4
  • 17