Visual Studio代码:如何使用参数调试Python脚本

我使用Visual Studio代码来调试Python脚本。

本指南之后,我在launch.json文件中设置了参数:

Enter image description here

但当我按下调试时,它说我的参数未被识别,而Visual Studio代码说:

错误:无法识别的参数

Enter image description here

因为Visual Studio代码使用PowerShell,所以让我们使用相同的参数执行相同的文件:

Enter image description here

所以:相同的文件,相同的路径,相同的参数。在终端中,它可以工作,但在Visual Studio代码中不能工作。

我哪里错了?

136402 次浏览

我认为城市和奥克兰被用作一个单一的论点。也许试着像这样把他们分开……

单参数

    "args": ["--city","Auckland"]

多参数和多值

例如:

--key1 value1 value2 --key2 value3 value4

只需将它们放入args列表按顺序一个接一个

"args": ["--key1", "value1", "value2", "--key2", "value3", "value4"]

在Visual Studio中,您可以以方便而自然方式传递多个参数:

--trail=0 --g=0 --V="HO" --save_interval=10 --verbose=True

我只是不知道为什么他们不会在Visual Studio代码中支持这一点。一个一个地列出论点是很麻烦的,而且有点愚蠢。它们只需将参数字符串传递给Python解析器,一切都可以轻松完成。

--key1 value1 value2 --key2 value3 value4

可以作为

"args": ["--key1=value1", "value2", "--key2=value3", "value4"]

(结合Pawan Kumar和Chunde Huang的两个答案。)

Python项目文件夹路径。vscode中的文件launch.JSON,在Visual Studio代码F5中测试。

{
// 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: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": ["c", "pwd"],
}
]
}

还没有人提到这一点,所以我想我应该提供一个建议,这可能会节省你一些时间,当然也会节省一些理智。我用__abc0数组设置了launch.JSON文件,但是当我运行调试器时,我无法让我的参数显示在终端中。

出于某种原因,我所要做的就是退出并重新启动VS代码。然后它像冠军一样工作。

我还注意到,如果您通过单击调试按钮来运行脚本,如下所示 __abc2,则不传递参数。但是,使用Run -> Start Debugging(或其快捷F5)成功传递了参数。

enter image description here

如果单击";调试Python文件"; 不传递参数,然后添加 ";目的";:launch.JSON文件

中的[";debug-in-terminal"
{"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"args": ["--experimentName", "Debugging"],
"purpose": ["debug-in-terminal"]
}
]
}