如何进入pylance使用的环境和代码?

2024-04-30 07:11:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pylance检查我的Python代码。它告诉我

Import "astor" could not be resolved

enter image description here

当我切换到VS代码内的终端时:

enter image description here

我很确定问题在于它使用了另一个环境。默认情况下我使用的是pyenv,我想知道vscode是否会使用相同的环境。但至少我需要能够访问它用于安装软件包的环境

有趣的是,状态栏似乎显示了其他内容,因为在该环境中我安装了astor:

enter image description here


Tags: 代码importpyenv终端环境not情况be
3条回答

我发现这个链接告诉我们应该添加一个额外的路径

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

我发现有一样东西解决了我和你的问题

  1. 转到您的工作环境(我的是pipenv shell),然后pip显示“yourmodule”以检查您的模块是否已安装
  2. 如果其已安装的副本Location:路径
  3. 转到设置ctrl+','
  4. 类型外部路径
  5. Add Item粘贴复制的路径字符串并确定

我们添加了实际安装路径作为附加导入搜索解析路径,因此将扫描此路径以进行导入

现在你的模块应该被解析了。这对我有用

在VSCODE上解决此问题的另一种简单方法:

  • ctrl+','
  • 输入“外部路径”
  • 下面应该有“addelement”(我有西班牙语的VSCode,所以在我的例子中有“agregarElemento”)
  • 键入“./sources/”

此外,如果您在导入本地文件时遇到问题,您也可以执行相同的操作,并添加工作目录路径以解决问题:)只需以相同的方式在“./sources”之外添加“C:your working directory goes here”。)

相关问题 更多 >