[英]How to debug a Python module in Visual Studio Code's launch.json
My question may seem simple but, I have a module that I launch in a terminal like this:我的问题可能看起来很简单,但是,我有一个在终端中启动的模块,如下所示:
python -m my_module.my_file
How do I debug this in Visual Studio Code?如何在 Visual Studio Code 中调试它?
I have this in my launch.json
( documentation )我的launch.json
(文档)中有这个
"type": "python",
"request": "launch",
"pythonPath": "D:\\ProgramData\\Anaconda3\\envs\\simulec\\python.exe",
"args": [
"-m",
"my_module.my_file",
]
If I don't set the program
option or if I set it to ""
I get "File does not exist" Error.如果我没有设置program
选项,或者如果我将它设置为""
,我会收到“文件不存在”错误。
How can I fix this?我怎样才能解决这个问题?
Actually, there is a very simple option to do this I found by accident while trying to edit the launch.json
file.实际上,我在尝试编辑launch.json
文件时偶然发现了一个非常简单的选项。
"type": "python",
"request": "launch",
"pythonPath": "D:\\ProgramData\\Anaconda3\\envs\\simulec\\python.exe",
"module": "my_module.my_file",
Simply specify the module in the module key "module": "my_module.my_file"
只需在模块键"module": "my_module.my_file"
指定模块"module": "my_module.my_file"
The -m
is not useful any more. -m
不再有用。
In a terminal like this: python -m xyz abc.z3
在这样的终端中: python -m xyz abc.z3
(Please make sure you are opening "the root folder of your project"). (请确保您正在打开“项目的根文件夹”)。
{
// 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": "module",
"type": "python",
"request": "launch",
"module": "xyz",
"args": [
"abc.z3"
]
}
],
}
The answer from @dzada was a bit confusing for me so I tried to rephrase it and add more clarification. @dzada 的回答让我有点困惑,所以我尝试重新措辞并添加更多说明。
To debug a module in file called script_file.py
that exists in a package called packagex
with structure like the following:要调试名为script_file.py
文件中的模块,该文件存在于名为packagex
的包中,其结构如下:
Project_Folder
packagex
script_file.py
Your configuration in the launch.json
file should looks like the following launch.json
文件中的配置应如下所示
"configurations": [
{
"name": "Python: any name like script_file",
"type": "python",
"request": "launch",
"module": "packagex.script_file"
}
]
To add slightly to dzada's answer , which helped me a lot, a Visual Studio Code variable can be used to make this general for debugging any file that is in your module.为了稍微增加dzada 的回答,这对我有很大帮助,可以使用 Visual Studio Code 变量使其通用,以调试模块中的任何文件。
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "my_module.${fileBasenameNoExtension}"
}
]
}
Which is probably what you want to do.这可能是您想要做的。
After some searching, I found that the config can be generalized for any module by the use of the Command Variable extension .经过一些搜索,我发现可以通过使用Command Variable extension为任何模块推广配置。 Assuming that the your workspace folder is the one containing the package, the following configuration would work for any module:假设您的工作区文件夹是包含 package 的文件夹,以下配置适用于任何模块:
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"justMyCode": true,
"module": "${command:extension.commandvariable.file.relativeFileDotsNoExtension}",
"cwd":"${workspaceFolder}"
}
{
"version": "0.2.0",
"configurations": [
{
"name": "module",
"type": "pythonExperimental",
"request": "launch",
"module": "my_package.my_module.${fileBasenameNoExtension}",
},
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.