简体   繁体   English

使用 Visual Studio Code 中的参数运行 Python 程序

[英]Running a Python program with arguments from within the Visual Studio Code

I am running a Python program that takes some command line arguments.我正在运行一个带有一些命令行参数的 Python 程序。 How can I provide these arguments when I am building a program within the Visual Studio Code?在 Visual Studio Code 中构建程序时如何提供这些参数?

You can pass in the arguments into the program by defining the arguments in the args setting of launch.json as defined below:您可以通过在 launch.json 的args设置中定义参数来将args传递到程序中,如下定义:

json
{
    "name": "Python",
    "type": "python",
    "pythonPath":"${config.python.pythonPath}", 
    "request": "launch",
    "stopOnEntry": true,
    "console": "none",
    "program": "${file}",
    "cwd": "${workspaceRoot}",
    "args":["arg1", "arg2"],
    "env": {"name":"value"}
}

Further information can be found on the documentation site here: https://github.com/DonJayamanne/pythonVSCode/wiki/Debugging#args可以在此处的文档站点上找到更多信息: https : //github.com/DonJayamanne/pythonVSCode/wiki/Debugging#args

If you use the Code Runner extension you can add the following to your settings (click on the '{}' icon in the top right corner to get the settings.json file):如果您使用 Code Runner 扩展,您可以将以下内容添加到您的设置中(点击右上角的“{}”图标以获取 settings.json 文件):

"code-runner.executorMap": { "python": "$pythonPath -u $fullFileName xxx" } "code-runner.executorMap": { "python": "$pythonPath -u $fullFileName xxx" }

where xxx is your argument.其中 xxx 是您的论点。 This is a global change so you have to change when working on other files.这是一个全局更改,因此您必须在处理其他文件时进行更改。

在 2.0.0 版本中执行此操作的一种方法是:

"command": "python ${file} --model_type LeNet5 --prior_file conf1.json --epochs 200",

running your script from the command line in the terminal works.从终端的命令行运行您的脚本。

According to vscode site here根据此处的 vscode 站点

it is adding similar它正在添加类似的

"args" : ["--port", "1593"]

in launch.jsonlaunch.json

I am running a Python program that takes some command line arguments.我正在运行一个带有一些命令行参数的Python程序。 How can I provide these arguments when I am building a program within the Visual Studio Code?在Visual Studio Code中构建程序时,如何提供这些参数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM