Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 686
Martin Thoma Avatar asked Sep 27 '20 12:09

Martin Thoma


People also ask

How do I activate the environment in VS Code?

Select and activate an environment# To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P). Note: If the Python extension doesn't find an interpreter, it issues a warning.

Is not accessed Pylance VS Code?

Pylance is an extension for Visual Studio Code (VSCode) to help the editor understand Python code. It simply means that Pylance is not using the same Python binary as your project. Open the command palette and type "Python: select interpreter", then find your Python interpreter that has pyodbc installed.


2 Answers

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

like image 81
Taciano Morais Silva Avatar answered Oct 05 '22 05:10

Taciano Morais Silva


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

like image 22
MARTIN CADAGAN GUZMAN Avatar answered Oct 05 '22 05:10

MARTIN CADAGAN GUZMAN