Home > Software design >  Import "torch" could not be resolved
Import "torch" could not be resolved

Time:09-26

Using vscode 1.60.2

Running this code in command line, after executing the "python" command works. So I know that the library is properly installed. This is a problem specifically with vscode. I restarted vscode, and even restarted windows after installing pytorch with pip. Did not fix it.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)

# Images
imgs = ['https://ultralytics.com/images/zidane.jpg']  # batch of images

# Inference
results = model(imgs)

# Results
results.print()
results.save()  # or .show()

results.xyxy[0]  # img1 predictions (tensor)
results.pandas().xyxy[0]  # img1 predictions (pandas)
#      xmin    ymin    xmax   ymax  confidence  class    name
# 0  749.50   43.50  1148.0  704.5    0.874023      0  person
# 1  433.50  433.50   517.5  714.5    0.687988     27     tie
# 2  114.75  195.75  1095.0  708.0    0.624512      0  person
# 3  986.00  304.00  1028.0  420.0    0.286865     27     tie

CodePudding user response:

You might have changed which interpreter you're using for Python. In VSCode go to the command palette and search for Python: Select Interpreter. If there's a default option go with that since that's where you might have installed the module to using pip. Should look something like this. Command palette can be reached by Ctrl Shift p. If that doesn't work set up a virtual environment for your project and install your module there.

enter image description here

  • Related