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?
To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.
Just click the Run Python File in Terminal play button in the top-right side of the editor. Select one or more lines, then press Shift+Enter or right-click and select Run Selection/Line in Python Terminal. This command is convenient for testing just a part of a file.
Go to your project properties, either by right-clicking on the project and picking "Properties" or by picking Properties from the Project menu. Click on Debug, then enter your arguments into the "Script Arguments" field. Save.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With