36

Good evening, I have just installed PyTorch 0.4.0 and I'm trying to carry out the first tutorial "What is PyTorch?" I have written a Tutorial.py file which I try to execute with Visual Studio Code

Here is the code :

from __future__ import print_function
import torch

print (torch.__version__)

x = x = torch.rand(5, 3)
print(x)

Unfortunately, when I try to debug it, i have an error message : "torch has no rand member"

This is true with any member function of torch I may try

Can anybody help me please?

iacob
  • 20,084
  • 6
  • 92
  • 119
Clém Grt
  • 471
  • 2
  • 5
  • 10

4 Answers4

81

In case you haven't got a solution to your problem or someone else encounters it.

The error is raised because of Pylint (Python static code analysis tool) not recognizing rand as the member function. You can either configure Pylint to ignore this problem or you can whitelist torch (better solution) to remove lint errors by adding following to your .pylintrc file.

[TYPECHECK]

# List of members which are set dynamically and missed by Pylint inference
# system, and so shouldn't trigger E1101 when accessed.
generated-members=numpy.*, torch.*

In Visual Studio Code, you could also add the following to the user settings:

"python.linting.pylintArgs": [
"--generated-members=numpy.* ,torch.*"
]

The issue is discussed here on PyTorch GitHub page.

kHarshit
  • 11,362
  • 10
  • 52
  • 71
6

Fast solution from pylint no member issue but code still works vscode

Press: CTRL + Shift + P

Click on "Preferences: Open Settings (JSON)"

Add this line into JSON : "python.linting.pylintArgs": ["--generate-members"]
Red One
  • 107
  • 1
  • 5
  • 1
    PyLint has no parameter `generate-members`. Did you mean to pass use `--generated-members torch` as in the answer above? – Christopher Dec 17 '21 at 13:25
  • 2
    I only added the line `"python.linting.pylintArgs": ["--generate-members"]` to the settings.json file and that was sufficient for me. Hope it helps – Red One Dec 19 '21 at 12:39
2

If anyone is still facing the problem then here is the solution that worked for me. Go to vs code settings, file>preferences>settings or use shortcut ctrl+, and search for python.linting.pylintPath. Modify the pylint path, Go to your anaconda installation directory>pkgs>pylint>scripts and copy paste the path to the settings and add pylint at the end of the path, something like this (anaconda installation directory)\pkgs\pylint-2.4.4-py37_0\Scripts\pylint

-6

In VS Code, one can select flake8 by

Ctrl + Shift + P -> Select linter -> flake8

Source

AbbasTari
  • 1
  • 3