如何在VS Code中通过${command:pickArgs}给Python脚本传多个命令行参数?
解决VS Code中Python调试用${command:pickArgs}传递多参数的问题
有两种可行方案可以解决这个多参数传递的问题:
方案一:调整args配置并确保解释器路径正确设置
直接将launch.json中的args字段设置为不带数组括号的"${command:pickArgs}",配置示例如下:
{ "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "args": "${command:pickArgs}", "console": "integratedTerminal" } ] }
关于你担心的含空格Python解释器路径被拆分的问题:只要通过VS Code左下角的解释器选择器设置默认解释器,而非手动在launch.json硬写路径,VS Code会自动处理带空格的路径,不会出现拆分问题。若确实需要手动指定路径,给路径加上双引号即可,比如:
"python.defaultInterpreterPath": "\"C:\\Program Files\\Python311\\python.exe\""
方案二:自定义多参数输入项(更灵活)
在launch.json中添加inputs配置,自定义支持多参数输入的交互项,再在args中引用它:
{ "version": "0.2.0", "configurations": [ { "name": "Python: Current File with Args", "type": "python", "request": "launch", "program": "${file}", "args": "${input:multiArgs}", "console": "integratedTerminal" } ], "inputs": [ { "id": "multiArgs", "type": "promptString", "description": "输入多个参数,用空格分隔", "default": "" } ] }
调试时输入4 7,VS Code会自动将其拆分为两个独立参数,sys.argv[1]为'4',sys.argv[2]为'7',转int不会报错。
内容的提问来源于stack exchange,提问作者Lee Hachadoorian
相关产品推荐
相关产品推荐

