在Ubuntu20.04LTS上使用Python3.x在VSCode中无法解析“导入“Path.to.own.script”Pylance(reportMissingImports)”
这是一个类似的情况我已经遇到过了几个月前使用pylint的事先pylance:
我的python 3.9x- 脚本(使用VS Codeon Ubuntu 20.04 LTS)从以下自定义“工具”导入开始:
import sys
sys.path.append(
'/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts/'
)
import General.Misc.general_tools as tools
现在,Pylance声明:
Import "General.Misc.general_tools" could not be resolvedPylance (reportMissingImports)
即使在程序执行期间模块被完美地导入,也会发生这种情况。
因此,为了确保Pylance理解这是一个现有的模块路径,除了sys.path.append(..)- 方法之外,我在settings.json- 文件中添加了以下内容:
{
...
// Possible values: "Jedi", "Pylance", "Microsoft", "None".
"python.languageServer": "Pylance",
// NOTE on changing from microsoft to pylance language server: python.autoComplete.extraPaths --> python.analysis.extraPaths
// Docs: https://github.com/microsoft/pylance-release/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings
"python.analysis.extraPaths": [
"/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts"
],
...
}
然而,reportMissingImports即使它被正确导入,我仍然收到 -消息。
我在这里找到的一种解决方法效果很好(附加# type: ignore到导入语句中):
import General.Misc.general_tools as tools # type: ignore
尽管如此,这只是一种解决方法,这就是我希望解决此问题根源的原因。从技术上讲,这与我之前使用的解决方法相同,以消除来自pylint. 可能这是VS-Code settings.json- 配置固有的东西,因为使用VS-Code是这里的常数因素。
编辑没有解决问题的其他措施:
我加了
export PYTHONPATH="$PYTHONPATH:/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts"
到我的~/.bashrc- 文件,这使我现在可以python从终端直接在-shell 中导入模块,而无需之前的sys-path 操作。然而,这仅适用于全局系统 python 环境,而不适用于任何虚拟环境。为了更改那里的 sys-path,我按照这些说明进行操作,而我的特定虚拟环境“scrapy_course”已打开,如下所示:
(scrapy_course) andylu@andylu-Lubuntu-PC:~/$ add2virtualenv /home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts
该命令适用于virtualenvwrapper,它可以巧妙地结合 pyenv 管理虚拟环境。现在,即使没有sys.path.append(...)导入模块,我也可以在当前环境中运行上述脚本,pylance但仍然无法正确识别路径并向我显示与以前相同的警告。
编辑"python.analysis.useImportHeuristic": true:
我一直在我的全局settings.json文件中激活这个选项,但我仍然没有注意到任何影响。一旦这种情况发生变化,或者最终有(不同的)解决方案出现在我面前,我会及时通知您。
编辑抑制/禁用 Pylance 'reportMissingImports' linting-消息:
我已经找到了如何完全抑制特定的 Pylance-linting-消息,如果您有兴趣作为一种解决方法。尤其是在我目前的状况,我需要利用pylint的在平行无论如何,所以我完全不依赖于Pylance的棉短绒。
回答
默认情况下,Pylance 包含工作区的根路径。如果要包含其他子目录作为导入解析路径,可以使用python.analysis.extraPaths工作区的设置添加它们。
- 在 VS Code 中按
<ctrl> + <,>打开设置。 - 输入
python.analysis.extraPaths - 选择“添加项目”
- 输入库的路径
/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts/
- I use virtualenv for my (Django) project and I had to reference the path for this to work: ~/.virtualenvs/<project directory>/Lib/site-packages/
回答
下面两种方法:
-
在 VS 代码中,您可以编辑该
setting.json文件。如果添加"python.analysis.useImportHeuristic": truelinting 错误将被删除。 -
另一种方法是
# type: ignore在导入代码的末尾添加。
这是我从以下地址获得上述解决方案的 github 链接:https : //github.com/microsoft/pylance-release/issues/68
它对我有用:python 3.9, VScode,windows10
- Just checked it on Windows 10, in my `settings.json` the line `"python.analysis.useImportHeuristic": true` is greyed out. When I hover over it, it states "Unknown Configuration Setting".
- This is expected as it is a hidden option. https://github.com/microsoft/pylance-release/issues/1167#issuecomment-822647493