VS Code/Pylance/Pylint 无法解析导入 [英] VS Code / Pylance / Pylint Cannot resolve import

查看:70
本文介绍了VS Code/Pylance/Pylint 无法解析导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结

我有一个从 VS Code 终端运行时可以工作的 python 导入,但是 VS Code 的编辑器给出了警告.此外,转到定义"不起作用.

问题

我从图像 tensorflow/tensorflow:1.15.2-py3 创建了一个 docker 容器,然后使用 VS Code 的Remote-Containers"附加到它.延期.然后我在容器中创建了以下文件.

main.py:

将 tensorflow.compat.v1 导入为 tf打印(tf.__version__)

这在 VS Code 终端中运行良好,但编辑器和问题窗格都给我一个 unresolved import 'tensorflow.compat' 警告.还有转到定义"不适用于 tf.__version__.

我正在使用多个扩展,但我相信相关的是 Microsoft Python 扩展(安装在容器中),以及 Remote - Containers 扩展,以及现在的 Pylance 扩展(安装在容器中).

我尝试过的事情

我已经用默认的 pylint 尝试过这个,然后在安装 pylance 之后也有类似的结果.我也看过一些文档 关于类似的问题,但它们与为项目中的模块设置正确的源文件夹位置有关.相比之下,我的代码在我的项目中似乎与导入/转到定义一起工作得很好.外部库似乎不起作用.

另外,为了这个最小的例子,我以 root 身份附加到容器,所以我猜这不是权限提升的问题.

我也尝试禁用除以下扩展之外的所有扩展,但得到了相同的结果:

  • 远程 - 容器(本地)
  • 远程 - WSL(本地)
  • Python(在容器上)
  • Jupyter(在容器上,Python 出于某种原因需要)

以上所有扩展都是最新版本.

我也摆弄过设置 python.autocomplete.extraPaths,但我不确定正确的路径是什么.必须将库添加到安装在全局 python 安装中的路径似乎也是错误的,特别是因为我没有使用虚拟环境(它在 docker 容器中等等).

问题

如何修复 VS Code 以使其识别此导入,并且我可以使用转到定义"探索这些 tensorflow 函数/类/等?

解决方案

tldr;

TensorFlow 以 pylint & 的方式定义了它的一些模块.pylance 无法识别.这些错误不一定表示设置不正确.

修复:

  • pylint:pylint 警告被安全忽略.
  • Intellisense:我目前所知道的修复 Intellisense 的最佳方法是用它们具有别名的模块替换导入(通过在 repl 中将别名导入为 x 然后运行 ​​help(x)).因为在我的例子中别名的目标是一个内部名称,所以您可能不想将这些更改签入源代码管理.不理想.

详情

关于 linting:似乎 tensorflow 以工具无法理解的方式定义其模块.此外,该包似乎是另一个包的某种别名.例如:

将 tensorflow.compat.v1 导入为 tftf.estimator.RunConfig()

上面的代码给出了pylint警告并破坏了智能感知.但是,如果您在 REPL 中手动导入上述内容并运行 help(tf),则会显示以下包,您可以改用它:

 将 tensorflow_core._api.v1.compat.v1 导入为 tftf.estimator.RunConfig()

第二个示例不会导致 pylint 警告.此外,智能感知功能(转到定义、Ctrl+单击等)也适用于第二个示例.

然而,基于 _api,看起来第二个包名是一个内部命名空间,所以我猜最好只使用这个内部名称进行本地调试.>

确认/门票

The Summary

I have a python import that works when run from the VS Code terminal, but that VS Code's editor is giving warnings about. Also, "Go to Definition" doesn't work.

The Problem

I have created a docker container from the image tensorflow/tensorflow:1.15.2-py3, then attach to it using VS Code's "Remote- Containers" extension. Then I've created the following file in the container.

main.py:

import tensorflow.compat.v1 as tf
print(tf.__version__)

This runs fine in the VS Code terminal, but the Editor and the Problems pane both give me an unresolved import 'tensorflow.compat' warning. Also "Go to Definition" doesn't work on tf.__version__.

I'm using several extensions but I believe the relevant ones are the Microsoft Python extension (installed in the container), as well as the Remote - Containers extension, and now the Pylance extension (installed in the container).

The Things I've Tried

I've tried this with the default pylint, and then also after installing pylance with similar results. I've also seen some docs about similar issues, but they were related to setting the correct source folder location for modules that were part of a project. In contrast, my code within my project seems to work fine with imports/go-to-definition. It's external libraries that don't seem to work.

Also, for the sake of this minimal example, I've attached to the container as root, so I am guessing it's not an issue of elevated permissions.

I've also tried disabling all the extensions except the following, but got the same results:

  • Remote - Containers (local)
  • Remote - WSL (local)
  • Python (on container)
  • Jupyter (on container, required by Python for some reason)

All the extensions above are on the latest versions.

I've also fiddled around with setting python.autocomplete.extraPaths, but I'm not sure what the right path is. It also seems like the wrong thing to have to add libraries to the path that are installed in the global python installation, especially since I'm not using a virtual environment (it being in a docker container and all).

The Question

How do I fix VS Code so that it recognizes this import and I can use "Go to Definition" to explore these tensorflow functions/classes/etc?

解决方案

tldr;

TensorFlow defines some of its modules in a way that pylint & pylance aren't able to recognize. These errors don't necessarily indicate an incorrect setup.

To Fix:

  • pylint: The pylint warnings are safely ignored.
  • Intellisense: The best way I know of at the moment to fix Intellisense is to replace the imports with the modules they are aliasing (found by importing alias in a repl as x then running help(x)). Because the target of the alias in my case is an internal name, you probably don't want to check in these changes to source control. Not ideal.

Details

Regarding the linting: It seems that tensorflow defines its modules in a way that the tools can't understand. Also, it appears that the package is an alias of some kind to another package. For example:

import tensorflow.compat.v1 as tf
tf.estimator.RunConfig()

The above code gives the pylint warning and breaks intellisense. But if you manually import the above in a REPL and run help(tf), it shows you the below package, which you can use instead:

import tensorflow_core._api.v1.compat.v1 as tf
tf.estimator.RunConfig()

This second example does not cause the pylint warning. Also the Intellisense features (Go to definition, Ctrl+Click, etc) work with this second example.

However, based on the _api, it looks like that second package name is an internal namespace, so I'm guessing it is probably best to only use this internal name for local debugging.

Confirmation/Tickets

这篇关于VS Code/Pylance/Pylint 无法解析导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆