简体   繁体   English

如何进入 VS Code 用于 pylance 的环境?

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

I'm using pylance to check my Python code.我正在使用 pylance 检查我的 Python 代码。 It tells me它告诉我

Import "astor" could not be resolved导入“astor”无法解析

在此处输入图片说明

When I switch to the terminal within VS Code:当我在 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.我默认使用 pyenv,我希望 vscode 使用相同的环境。 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:有趣的是,状态栏似乎显示了其他内容,因为在该环境中我安装了 astor:

在此处输入图片说明

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.最简单的方法(使用 VS Code Python 扩展)是创建一个设置 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:例如,如果项目使用源目录,则在工作区中创建一个文件 .vscode / settings.json ,内容如下:

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

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

Another easy way to solve this on VSCODE:在 VSCODE 上解决此问题的另一种简单方法:

  • ctrl + ',' ctrl + ','
  • type "extrapaths"输入“extrapaths”
  • Down you should have something like "add element" (I have VSCode on Spanish so in my case I have "Agregar elemento")向下你应该有类似“添加元素”的东西(我有西班牙语的VSCode,所以在我的情况下我有“Agregar elemento”)
  • type './sources/'输入'./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 ;)此外,如果您在导入本地文件时遇到问题,您可以做同样的事情并添加您的工作目录路径来解决问题:) 只需以相同的方式添加“C:您的工作目录到这里”和“./sources” ;)

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转到您的工作环境(我的是 pipenv shell),然后 pip show 'yourmodule' 以检查您的模块是否已安装
  2. If its installed copy Location: path如果它安装了副本Location:路径
  3. Go to settings ctrl +','转到设置ctrl +','
  4. Type extrapaths输入额外路径
  5. Add Item paste the path string you copied and ok. Add Item粘贴您复制的路径字符串并确定。

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.这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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