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

按照本指南,我在启动中设置了参数。json文件:

但是当我按下调试,它说我的参数不被识别,Visual Studio Code说:

错误:无法识别的参数

由于Visual Studio Code使用PowerShell,让我们用相同的参数执行相同的文件:

同样的文件,同样的路径,同样的参数。在终端中它可以工作,但在Visual Studio Code中不行。

我哪里错了?


当前回答

文件发布。在Visual Studio Code 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"],
        }
    ]
}

其他回答

我认为,City和Auckland被用作一个论点。也许试着把它们分开,像这样……

单参数

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

多个参数和多个值

如:

--key1 value1 value2 --key2 value3 value4

只需将它们依次放入args列表中:

“参数”:[”——key1”、“value1”、“value2”,“——key2”,“value3”,“value4”)

文件发布。在Visual Studio Code 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"],
        }
    ]
}

如果单击“调试python文件”没有传递参数,则在启动中添加“purpose”:[" Debug -in-terminal"]。json文件

{"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"]
    }
  ]
}
--key1 value1 value2 --key2 value3 value4

可以传递为

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

(结合帕万·库马尔和黄春德的两个回答。)

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

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

我只是不知道为什么他们不支持Visual Studio Code。一个一个地列出参数既麻烦又有点傻。它们只是将参数字符串传递给Python解析器,事情就可以很容易地完成了。