Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 697
cerebrou Avatar asked Oct 14 '16 07:10

cerebrou


People also ask

How do you pass command line arguments in 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.

How do I run a Python program in Visual Studio Code terminal?

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.

How do I pass command line arguments to Python from VS in debug mode?

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.


2 Answers

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

like image 111
Don Avatar answered Oct 02 '22 07:10

Don


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.

like image 21
Jan Jensen Avatar answered Oct 02 '22 08:10

Jan Jensen