Visual Studio Code: How debug Python script with arguments

Question:

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?

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"]

Answered By: Pawan kumar

In Visual Studio, you can pass multiple parameter as convenient and natural way:

--trail=0 --g=0 --V="HO" --save_interval=10 --verbose=True

I just do not know why they will not support this in Visual Studio Code. To list arguments one by one is cumbersome and a bit silly. They just pass the arguments string to the Python parser, and things can be done easily.

Answered By: Chunde Huang
--key1 value1 value2 --key2 value3 value4

can be passed as

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

(Combining the two answers by Pawan Kumar and Chunde Huang.)

Answered By: Sreeragh A R

File launch.json in the Python project folder path .vscode, tested in Visual Studio Code F5.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": ["c", "pwd"],
        }
    ]
}
Answered By: florian.isopp

Nobody has mentioned this yet, so I thought I’d offer a suggestion that may save you some minutes and certainly some sanity. I set up my launch.json file with an args array, but I couldn’t get my args to show up in the terminal when I ran the debugger.

All I had to do was quit and restart VS Code for some reason. Then it worked like a champ.

Answered By: ingernet

I also noticed that if you run the script by clicking on the debug button that looks like this
enter image description here, then the arguments are not passed. However, using Run -> Start Debugging (or its shortcut F5) passed the arguments successfully.

Answered By: Sourya Dey

If clicking the "Debug python file" doesn’t pass the arguments then add "purpose": ["debug-in-terminal"] in the launch.json file

{"version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "justMyCode": true,
      "args": ["--experimentName", "Debugging"],
      "purpose": ["debug-in-terminal"]
    }
  ]
}
Answered By: Shashidhar Kudari
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.