简体   繁体   English

Pyright 报告“导入无法解决”

[英]"Import could not be resolved" reported by Pyright

在此处输入图像描述

I've just started using Pyright .我刚开始使用Pyright Running it on files that run perfectly well I get plenty of errors.在运行良好的文件上运行它,我得到了很多错误。

This question is similar, but refers to one's own modules. 这个问题类似,但是指的是自己的模块。

For example Import "numpy" could not be resolved .例如Import "numpy" could not be resolved What does it mean, and how do I resolve it?这是什么意思,我该如何解决?

On my computer I have 3 Pythons, a 3.6 from Anaconda, and a 2.7 & 3.7 that are regular python.在我的电脑上,我有 3 个 Python,一个来自 Anaconda 的 3.6,以及一个 2.7 和 3.7,它们是常规的 python。 Prompted by a nudge from this GH issue , I switched from the Anaconda 3.6 to the 3.7, and back again, and the problem went away.这个 GH 问题的推动下,我从 Anaconda 3.6 切换到 3.7,然后又回来了,问题就消失了。

python 选择器

I think that this is the case because your .vscode/settings.json (the following is mine), doesn't have that last line until you change your python, at which point, that last line is put in and Pyright has something to look at.我认为是这种情况,因为您的.vscode/settings.json (以下是我的),在您更改python 之前没有最后一行,此时,最后一行被放入并且 Pyright 有一些东西看着。

{
  "python.linting.enabled": true,
  "python.formatting.provider": "black",
  "python.pythonPath": "C:\\Users\\ben\\Anaconda3\\python.exe"
}

I'm not sure if you have already fixed this, but I was having a lot of troubles with stuff I got with pip install and they were installed successfully, but when I tried to import them, they didn't import我不确定您是否已经解决了这个问题,但是我在安装 pip 时遇到了很多麻烦,并且它们已成功安装,但是当我尝试导入它们时,它们没有导入

I found out that for me, some of the installs get installed to a separate folder, and not the correct folder that they need to be in to import.我发现对我来说,一些安装被安装到一个单独的文件夹,而不是它们需要在其中导入的正确文件夹。

If you try installing it again with pip install, then it will say you already have it installed and it will give you the location where the file is, and I copied the files from there and pasted them into the correct location which is (on windows)如果您尝试使用 pip install 再次安装它,那么它会说您已经安装了它,它会给您文件所在的位置,我从那里复制文件并将它们粘贴到正确的位置,即(在 Windows )

C:\Users{Username}\AppData\Local\Programs\Python{Python Version}\Lib\site-packages C:\Users{用户名}\AppData\Local\Programs\Python{Python 版本}\Lib\site-packages

I was trying to find a place to share this and this was the closest to the issue, so I hope it helps!我试图找到一个分享这个的地方,这是最接近问题的地方,所以我希望它有所帮助!

python -m pip install -U pylint python -m pip install --upgrade pip python -m pip install -U pylint python -m pip install --upgrade pip

Open VS Code -> CTRL + SHIFT P -> Search ' select linter ' [ Python: Select Linter ] -> Hit Enter and Select Pylint Open VS Code -> CTRL + SHIFT P -> Search ' select linter ' [ Python: Select Linter ] -> Hit Enter and Select Pylint

If not solved.如果不解决。 Try deactivate your environment pip install numpy in your global environment.尝试deactivate您的环境pip install numpy

And if you are using Local Environment getting unresolved imports error then add In.vscode/settings.json "python.analysis.extraPaths": ["./path-to-your-code"], for example "python.analysis.extraPaths": [ "*.lib" ]如果您使用本地环境得到未解决的导入错误,则添加In.vscode/settings.json "python.analysis.extraPaths": ["./path-to-your-code"],例如"python.analysis.extraPaths": [ "*.lib" ]

I work in Local Environment and I can solve this problem by going to Setting.json and search python analysis but I just addressed this path C:\Users{Username}\AppData\Local\Programs\Python{Python Version}\Lib\site-packages我在本地环境中工作,我可以通过转到 Setting.json 并搜索 python 分析来解决这个问题,但我刚刚解决了这个路径 C:\Users{Local Version}\AppData\Libsite\Python{ -包

Setting the interpreter as Python with current version worked just fine for me.将当前版本的解释器设置为 Python 对我来说效果很好。 You can do it by clicking Python in lower left corner and selecting your project.您可以通过单击左下角的 Python并选择您的项目来完成此操作。

I solved mine by just adding the conda executable path.我通过添加 conda 可执行路径解决了我的问题。

Here is how I solved it on my Mac.这是我在 Mac 上解决它的方法。 In a terminal window (system's terminal not the vscode one) type:在终端 window(系统终端不是 vscode 终端)中输入:

which python哪个 python

On my setup I got: "/Volumes/Attic/miniforge3/bin/python" Then in VsCode copy that whole path to Setting > Extensions > Default Interpreter Path在我的设置中,我得到: “/Volumes/Attic/miniforge3/bin/python”然后在 VsCode 中将整个路径复制到设置 > 扩展 > 默认解释器路径

That's it.就是这样。 That instantly resolved the issue.那立即解决了这个问题。

I also did the same for Setting > Extensions > Conda Path populating that field with the path returned by which conda But that's a separate issue.我也对Setting > Extensions > Conda Path做了同样的事情,用which conda返回的路径填充该字段但这是一个单独的问题。

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

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