Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code: How debug Python script with arguments

I'm using Visual Studio Code in order to debug a Python script.

Following this guide, I set up the argument in the launch.json file:

Enter image description here

But when I press on Debug, it says that my argument is not recognized and Visual Studio Code says:

error: unrecognized arguments

Enter image description here

As Visual Studio Code is using PowerShell, let's execute the same file with the same argument:

Enter image description here

So: the same file, same path, and same argument. In the terminal it is working, but not in Visual Studio Code.

Where am I wrong?

like image 977
Francesco Mantovani Avatar asked Oct 07 '22 12:10

Francesco Mantovani


People also ask

How do you run a Python script with arguments in VS Code?

Connect to it from within VS Code using the Python "Remote Attach" debug configuration (using the default host and port settings). You can just control+c and then re-run the command with changed arguments using your shell history and command line editing facilities, for each debug run.

How do you debug Python command line arguments?

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.

How do I debug a Python script in VS Code?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.

How do you pass 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.


1 Answers

I think the --City and Auckland are used as a single argument. Maybe try separating them like so...

Single argument

    "args": ["--city","Auckland"]

Multiple arguments and multiple values

Such as:

--key1 value1 value2 --key2 value3 value4

Just put them into the args list one by one in sequence:

"args": ["--key1", "value1", "value2", "--key2", "value3", "value4"]

like image 184
Pawan kumar Avatar answered Oct 14 '22 13:10

Pawan kumar