Visual Studio代码:使用参数运行python文件 [英] Visual Studio Code: run python file with arguments
问题描述
是否有任何简单的方法可以在VS Code中使用参数运行python文件?
我知道我可以在 launch.json
文件和 args
关键字。但是,只是因为我想使用不同的参数,每次修改 launch.json
还是很烦的。
VsCode仅支持一个launch.json文件,但是它支持两个或多个配置,它们显示在左侧菜单/窗格的下拉列表中(而不是无配置)。 / p>
这里是说明,请单击DEBUG窗格中上面红色标记的Config按钮:
单击它,它将创建带有调试配置的launch.json文件。编辑此文件,并以此密钥对格式添加 args ,并为不同的 args 添加多个,包括变量替换!
{
//使用IntelliSense进行学习关于可能的属性。
//悬停以查看现有属性的描述。
//有关更多信息,请访问:https://go.microsoft.com/fwlink/?linkid=830387
版本: 0.2.0&,
配置:[[
{
name: Python:当前文件(集成终端),
type: python,
; request: launch,
program: $ {file},
args:[
--username, Jeremy,
--account, Stackoverflow
],
console: integratedTerminal;
},
{
name: Python:当前文件(集成终端),
type: python,
请求:启动,
程序: $ {file},
args:[ $ {env:USERNAME}] ,
控制台: integratedTerminal例如,
}
]
}
在Python脚本中放置一个断点在 def main(...)
下的第一行,然后按F5或单击开始调试。
Is there any easy way to run a python file inside the VS Code with arguments?
I know I can add a custom configuration in the launch.json
file with the args
keyword. However it is annoying to modify the launch.json
every time just because I want to use different arguments.
VsCode only supports one launch.json file however it's supports two or more configs, and they appear in the left-hand menu/pane's drop down list (instead of "No Configurations").
Here's the instructions, click the Config button in the DEBUG pane circled in red above:
Click it and it creates a launch.json file with debugging configurations. Edit this file and add the args in this key-pair format AND add multiple for different args including Variable Substitution!
{
// 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 (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"args": [
"--username", "Jeremy",
"--account", "Stackoverflow"
],
"console": "integratedTerminal"
},
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"args": ["${env:USERNAME}"],
"console": "integratedTerminal"
}
]
}
Put a breakpoint in your Python script, for example on the first line under def main(...)
and then press F5 or click Start Debugging.
这篇关于Visual Studio代码:使用参数运行python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!