您当前的位置: 首页 >  vscode

Xavier Jiezou

暂无认证

  • 5浏览

    0关注

    394博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

解决vscode中使用pytorch时pylint报错Module torch has no xxx member

Xavier Jiezou 发布时间:2020-10-16 22:46:14 ,浏览量:5

项目场景

安装好了pytorch,写一个简单的测试代码,如下:

import torch
x = torch.rand(5, 3)
print(x)
问题描述

正常输出:

tensor([[0.3506, 0.0131, 0.4908],
        [0.8524, 0.1879, 0.2194],
        [0.0101, 0.6458, 0.9603],
        [0.7522, 0.2765, 0.6378],
        [0.6041, 0.6980, 0.8985]])

但会报错:

Module 'torch' has no 'rand' member

在这里插入图片描述

原因分析

这个错误是pylint报的,所以肯定与它有关。具体可以看github上的第一条评论。

解决方案 方案一(推荐)
  1. Ctrl+Shift+P打开vscode的命令面板,输入settings.json并打开第一项 在这里插入图片描述

  2. settings.json中插入以下代码

"python.linting.pylintArgs": [
        "--errors-only",
        "--generated-members=numpy.*, torch.*, cv2.*, cv.*"
    ]
  1. 插入代码之后记得保存一下 在这里插入图片描述
方案二

打开vscode,在settings中搜索python.linting.pylintPath,将原pylint替换为conda安装路径\pkgs\pylint文件夹\Scripts\pylint 在这里插入图片描述 前提是你用conda安装的pytorch,无论是Anaconda还是Miniconda都行。

温馨提示

Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码。

用方案二之后pylint不知道为什么不会再进行任何提示了,相当于将pylint禁用了,显然这不是我们想要的效果。

而方案一不仅可以解决torch的报错问题,pylint还能继续发挥作用。以下图为例,torch不再报错,而其它错误比如使用了未定义的变量等,pylint还是会正常的提示。 在这里插入图片描述

引用参考
https://pypi.org/project/pylint/
https://github.com/pytorch/pytorch/issues/701
https://stackoverflow.com/questions/50319943/pytorch-error-message-torch-has-no-member
关注
打赏
1661408149
查看更多评论
立即登录/注册

微信扫码登录

0.0383s