Visual Studio Code: How debug Python script with arguments

PythonVisual Studio-Code

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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.

Solution 3 - Python

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.

Solution 4 - Python

--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.)

Solution 5 - Python

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

Solution 6 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrancesco MantovaniView Question on Stackoverflow
Solution 1 - PythonPawan kumarView Answer on Stackoverflow
Solution 2 - PythonSourya DeyView Answer on Stackoverflow
Solution 3 - PythonChunde HuangView Answer on Stackoverflow
Solution 4 - PythonSreeragh A RView Answer on Stackoverflow
Solution 5 - Pythonflorian.isoppView Answer on Stackoverflow
Solution 6 - PythoningernetView Answer on Stackoverflow