无法解析导入 "torch"

Import "torch" could not be resolved

使用 vscode 1.60.2

运行 命令行中的此代码在执行“python”命令后有效。所以我知道库已正确安装。这是 vscode 特有的问题。我重启了vscode,甚至用pip安装pytorch后又重启了windows。没修好。

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

您可能已经更改了为 Python 使用的解释器。在 VSCode 中,转到命令选项板并搜索 Python: Select Interpreter。如果有默认选项,请使用默认选项,因为您可能已经在此处安装了使用 pip 的模块。应该看起来像这样。可以通过 Ctrl+Shift+p 访问命令面板。如果这不起作用,请为您的项目设置一个虚拟环境并在那里安装您的模块。