WSL中VSCode交互窗口Python相对导入报错排查
问题:WSL环境下VSCode运行代码无法导入vision模块,且无法可视化图像
环境与需求
- 在Windows的WSL环境中,尝试使用
matplotlib.pyplot.imshow可视化图像,但WSL不支持GUI,计划通过VSCode交互窗口实现可视化。
项目结构
$projectFolder ├── mycode │ ├── __init__.py │ ├── model.py │ ├── dataset.py │ ├── train.py │ └── predict.py └── vision └── references └── detection ├── utils.py ├── transforms.py ├── engine.py └── etc
当前情况与问题
- 目前只能通过终端执行
python3 -m mycode.predict运行代码,但这种方式无法弹出绘图窗口。 - 尝试通过VSCode运行按钮或交互窗口运行时,代码自动补全正常,工作区也没有报错提示,但运行时会出现
ModuleNotFoundError: No module named 'vision'错误。
代码中的导入部分
import matplotlib.pyplot as plt import numpy as np import torch from dataset import MyDataset from model import get_instance_segmentation_model from PIL import Image from torchvision.transforms import functional as F from torchvision.utils import draw_bounding_boxes, draw_segmentation_masks from train import get_transform from vision.references.detection import transforms as T
VSCode的launch.json配置
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python: Module", "type": "python", "request": "launch", "module": "mycode", "env": {"PYTHONPATH": "${workspaceFolder}/vision/"} } ] }
已按教程完成配置,但仍出现上述问题,请问哪里操作出错了?
内容的提问来源于stack exchange,提问作者Martim Passos
相关产品推荐
相关产品推荐

