I'm using Visual Studio Code with the inbuilt Debugger in order to debug a Python script.
Following this guide, I set up the argument in the launch.json file:
But when I press on Debug, it says that my argument is not recognized my shell, or rather the argparser, says:
error: unrecognized arguments: --city Auckland
As Visual Studio Code is using PowerShell, let's execute the same file with the same argument:
So: the same file, same path, and same argument. In the terminal it is working, but not in Visual Studio Code's Debugger.
Where am I wrong?
-
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.Chunde Huang– Chunde Huang2019年11月19日 18:59:27 +00:00Commented Nov 19, 2019 at 18:59
-
None of the solutions below worked for me, even after restarting and spending a hour with this! Anyone else has some recommendation? This was working a few months ago for me.Raaka– Raaka2022年11月17日 14:15:42 +00:00Commented Nov 17, 2022 at 14:15
11 Answers 11
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"]
9 Comments
--city Auckland --year 2000?args: ["--my-n-args", "4 5"] does not work: error: argument -m/--my-n-args: invalid int value: '4 5' Edit: Found it myself: args: ["--my-n-args", "4", "5"]args: ["train"] Arguments are just passed to your program by the shell as an array of strings, and here you are defining an array of strings in json format. The Python sys module presents these as a list of strings.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.
4 Comments
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"]
}
]
}
5 Comments
purpose attribute) also worked. This is got to be a bug in VS Code.purpose key--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.)
Comments
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.
Comments
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"],
}
]
}
Comments
Don't forget: Once you edit the launch.json file, CLICK OFF IT, back to the py file you want to debug, THEN hit the debug button! Otherwise, it will try to debug itself! (?)
Comments
Combination of flags and arguments
python3 program.py --flag1 --flag2 --arg1=value1 --arg2=value2
launch.json:
"args": ["--flag1", "--flag2", "--arg1", "value1", "--arg2", "value2"]
Comments
There's one answer that is missing here yet.
There are two ways arguments will be passed:
Wrapped in a string OR, equivalent, with escaped spaces:
"--city Auckland"OR--city\ Auckland
You will get this result when you use a list
"args": ["--city Auckland"]
As two separated arguments with an unescaped space
--city Aucklang
When you use only one string:
"args": "--city Auckland"or as other answers state a list with multiple elements for each word.
For example, for a prompt to insert multiple arguments from a popup use
"args": "{command:pickArgs}"
1 Comment
{command:pickArgs} in code.visualstudio.com/docs/python/debugging#_args. The default launch.json generated Python Debugger: Current File with Arguments using "args": [ "${command:pickArgs}" ] . I changed it to "args": "{command:pickArgs}".Make sure that the launch.json file is selected as the active debug configuration in Visual Studio Code, follow these steps:
- Open the Visual Studio Code editor and navigate to the Debug view by clicking on the Debug icon in the left-hand sidebar or by pressing Ctrl+Shift+D (Windows/Linux) or Cmd+Shift+D (macOS).
- In the Debug view, click on the "Run" button to start the debugger.
- If you have multiple debug configurations defined in your launch.json file, you will be prompted to select the one you want to use. Select the appropriate configuration from the dropdown menu.
- Once you have selected the debug configuration, the debugger will start and your code will be executed according to the settings in the launch.json file.
Comments
You can avoid any configuration and just add the command line arguments to your code temporarily, bit dirty but very practical:
import sys
if __name__ == "__main__":
sys.argv = [__file__, 'first-arg', 'second-arg', 'third-arg']
print('arguments:', sys.argv)
Comments
Explore related questions
See similar questions with these tags.