I have a basic python file that I'm experimenting with. I want to launch it with two args in vs code. I've opened the launch.json file from the command window (ctrl+shift+p) but on each run it fails to pick up my args list. What is going on?
{
// 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": ["4", "2"]
}
]
}
The main program:
import sys
def add(num1=0, num2=0):
return int(num1) + int(num2)
def sub(num1=0, num2=0):
return int(num1) - int(num2)
def main():
#Assuming our inputs are valid numbers
print ("args: ",sys.argv)
addition = add(sys.argv[1], sys.argv[2])
print (addition)
subtraction = sub(sys.argv[1], sys.argv[2])
print (subtraction)
if __name__ == '__main__':
main()
When executing in VS Code, the traceback is:
> args: ['c:/Users/chicago/OneDrive/Documents/Code/Python/debugging/pdb 1.py']
> Traceback (most recent call last): File
> "c:\Users\chicago\OneDrive\Documents\Code\Python\debugging\pdb
> 1.py", line 18, in <module>
> main() File "c:\Users\chicago\OneDrive\Documents\Code\Python\debugging\pdb
> 1.py", line 12, in main
> addition = add(sys.argv[1], sys.argv[2]) IndexError: list index out of range
Its probable that vscode isn't picking up your launch configurations. Just a few sanity checks:
Did you confirm that launch.json is located in .vscode/ ?
In addition - did you make sure that you have the right configuration selected when you go to run? In this case: "Python: Current File".