[英]Visual Studio Code launch.json settings for debugging Python modules
Currently I have my file structure like this:目前我的文件结构是这样的:
├── Utilities
| ├── __init__.py
│ ├── module1.py
├── main.py
├── global_var.py
In main.py
and module1.py
I have already written import global_var
, and everything goes well when I run main.py
.在main.py
和module1.py
我已经写了import global_var
,当我运行main.py
时一切顺利。
However, when I tried to debug or run module1.py
itself, it always shows但是,当我尝试调试或运行module1.py
本身时,它总是显示
Exception has occurred: ModuleNotFoundError发生异常:ModuleNotFoundError
No module named 'global_var'没有名为“global_var”的模块
And I have to manually move module1.py
to the same folder with global_var.py
so that it can run successfully.而且我必须手动将module1.py
移动到与global_var.py
相同的文件夹中,以便它可以成功运行。
I would like to know how to set the launch.json
to stop moving the files.我想知道如何设置launch.json
以停止移动文件。 Here's my launch.json
right now:这是我现在的launch.json
:
{
"name": "Python: Modules",
"type": "python",
"request": "launch",
"program": "${file}",
"cwd": "${workspaceFolder}",
"console": "integratedTerminal"
}
I don't know what the name of the parent folder of file ' global_var.py
' is, so I temporarily named it folder_aa
.我不知道文件“ global_var.py
”的父文件夹的名称是什么,所以我暂时将其命名为folder_aa
。
Since they are not in the same folder, Visual Studio Code cannot find the path, so you could tell it the path of the file you want to import:由于它们不在同一个文件夹中,Visual Studio Code 找不到路径,因此您可以告诉它您要导入的文件的路径:
Add the line of settings to the launch.json
file of .vscode
file:将设置行添加到launch.json
文件的.vscode
文件中:
"env": {"PYTHONPATH": "${workspaceRoot}"},
Visual Studio Code will find the root directory (the project folder name) of the current project according to "${workspaceRoot}"
. Visual Studio Code 会根据"${workspaceRoot}"
找到当前项目的根目录(项目文件夹名称)。
Use ' from folder_aa import global_var
' instead of ' import global_var
'.使用 ' from folder_aa import global_var
' 而不是 ' import global_var
'。
Visual Studio Code will find file 'global_var.py' from folder 'folder_aa'. Visual Studio Code 将从文件夹“folder_aa”中找到文件“global_var.py”。
I created a project similar to the directory structure you provided, and through the above operations, it can be successfully imported.我创建了一个类似你提供的目录结构的项目,通过以上操作,可以成功导入。
My environment: Python 3.8.3;我的环境:Python 3.8.3; Visual Studio Code 1.47.3;视觉工作室代码 1.47.3; OS: Windows_NT x64 10.0.18362操作系统:Windows_NT x64 10.0.18362
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.