英文:
Running VSCode interactive window on WSL with relative imports
问题
我试图使用pyplot的imshow函数来可视化图表。Windows的WSL不支持GUI,所以建议的方法是通过VSCode的交互窗口来实现。然而,鉴于我的项目结构,唯一能运行我的代码的方式是使用终端(python3 -m mycode.predict),但由于WSL的原因,什么都不会绘制。以下是我的导入部分:
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的“播放”按钮运行或作为交互窗口运行时仍然出现ModuleNotFoundError: No module named 'vision'的错误,尽管自动完成工作,没有任何突出显示问题(“在工作区中没有检测到问题”)。我在这里做错了什么?以下是我的launch.json配置:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "mycode",
"env": {"PYTHONPATH": "${workspaceFolder}/vision/"}
}
]
}
英文:
I'm trying to visualize plots using pyplot imshow. Windows' WSL does not work with GUIs so the advised way of doing so is via VSCode's interactive window. However, given my project structure,
$projectFolder
├── mycode
│ ├── __init__.py
│ ├── model.py
│ ├── dataset.py
│ ├── train.py
│ └── predict.py
└── vision
└── references
└── detection
├── utils.py
├── transforms.py
├── engine.py
└── etc
the only way I can run my code is using the terminal (python3 -m mycode.predict), but then again nothing is plotted because of WSL. Here are my imports:
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
I followed this exactly but still get ModuleNotFoundError: No module named 'vision' when trying to run via the VSCode 'play' button and/or as interactive window, even though autocomplete works and nothing is highlighted ("No problems have been detected in the workspace"). What am I doing wrong here? Here's my 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/"}
}
]
}
答案1
得分: 1
我建议您使用 .env 文件。以下是一张表格,解释了 .env 和 launch.json 之间的区别:
| 运行选项 | .env 设置 | launch.json 设置 |
|---|---|---|
| 在终端中运行 Python 文件 | 否 | 否 |
| 在交互式窗口中运行 | 是 | 否 |
| 通过 F5 调试 | 是 | 是 |
| "调试 Python 文件" (*) | 是 | 否 |
如果您使用交互式窗口,那么 launch.json 中的设置将不起作用。
您可以在工作空间下创建一个 .env 文件,然后将 "python.envFile": "${workspaceFolder}/.env" 添加到您的 settings.json 中。
.env
PYTHONPATH=您的文件夹路径
英文:
I suggest you using .env file. Here is a table which explains the difference between .env and launch.json:
| Run option | .env setting | launch.json setting |
|---|---|---|
| Run Python file in terminal | no | no |
| Run in interactive Window | yes | no |
| Debug via F5 | yes | yes |
| "Debug Python File" (*) | yes | no |
If you use interactive Window, the settings in launch.json will not work.
You can create .env file under your workspace then add "python.envFile": "${workspaceFolder}/.env", to your settings.json.
.env
PYTHONPATH=PATH/TO/YOUR/FOLDER
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论