简体   繁体   中英

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

I am running a Python program that takes some command line arguments. How can I provide these arguments when I am building a program within the 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:

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

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.executorMap": { "python": "$pythonPath -u $fullFileName xxx" }

where xxx is your argument. 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

it is adding similar

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

in launch.json

I am running a Python program that takes some command line arguments. How can I provide these arguments when I am building a program within the Visual Studio Code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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