简体   繁体   中英

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

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

Import "astor" could not be resolved

在此处输入图片说明

When I switch to the terminal within VS Code:

在此处输入图片说明

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:

在此处输入图片说明

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM