The Python Oracle

Visual Studio Code: How debug Python script with arguments

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping

--

Chapters
00:00 Visual Studio Code: How Debug Python Script With Arguments
00:42 Accepted Answer Score 328
01:16 Answer 2 Score 130
01:38 Answer 3 Score 40
02:00 Answer 4 Score 16
02:22 Answer 5 Score 10
02:53 Thank you

--

Full question
https://stackoverflow.com/questions/5124...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #visualstudiocode

#avk47



ACCEPTED ANSWER

Score 336


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



ANSWER 2

Score 131


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.




ANSWER 3

Score 15


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




ANSWER 4

Score 11


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.