0

I am trying to debug a program with VS code and python but when I try to modify the json to accept an arg it gives me an invalid syntax issue. I tried to follow this link in MS regarding debugging: https://code.visualstudio.com/docs/python/debugging but am not getting anywhere.. I am using the default format and added my arg:

{
    // 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}",
            "args": [
                "..\\4\\shopping\\shopping.csv"
            ],
            "console": "integratedTerminal"
        }
    ]
}

I tried to move it into a different folder (In this case in \4) but I have also tried in the workspace and the shopping folder but without success Can someone help me understand where I am going wrong? thanks

EDIT 1:

adding more details:

this is the full error message:

PS C:\Users\Carlo\source\repos\CS50AI\4\.vscode>  & 'python' 'c:\Users\Carlo\.vscode\extensions\ms-python.python-2020.11.371526539\pythonFiles\lib\python\debugpy\launcher' '50282' '--' 'c:\Users\Carlo\source\repos\CS50AI\4\.vscode\launch.json'
Traceback (most recent call last):
  File "C:\Users\Carlo\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Carlo\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\Carlo\.vscode\extensions\ms-python.python-2020.11.371526539\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\Carlo\.vscode\extensions\ms-python.python-2020.11.371526539\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\Carlo\.vscode\extensions\ms-python.python-2020.11.371526539\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 267, in run_file
    runpy.run_path(options.target, run_name=compat.force_str("__main__"))
  File "C:\Users\Carlo\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 264, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "C:\Users\Carlo\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 239, in _get_code_from_file
    code = compile(f.read(), fname, 'exec')
  File "c:\Users\Carlo\source\repos\CS50AI\4\.vscode\launch.json", line 2
    // Use IntelliSense to learn about possible attributes.
    ^
SyntaxError: invalid syntax

no i do not get sys.argv in python

6
  • Where are you encountering the invalid syntax issue? The line you added for args is highlighted when you have launch.json open? Commented Dec 2, 2020 at 12:26
  • do you get the arguments in sys.argv inside the python script Commented Dec 2, 2020 at 12:51
  • edited post to answer with more info Commented Dec 2, 2020 at 14:17
  • What version of VS Code are you using? (Help > About). I am able to use your launch.json with version 1.51.1 Commented Dec 2, 2020 at 14:57
  • 1.51.1 (user setup) Commented Dec 2, 2020 at 15:00

2 Answers 2

1

According to the information you provided, I reproduced the problem in the terminal:

enter image description here

Reason: When using debugging, we need to open the script that needs to be executed instead of staying in the "launch.json" file.

Solution: Open the python script to be executed and debug it.

result:

enter image description here

Reference: Troubleshooting in VSCode.

Sign up to request clarification or add additional context in comments.

1 Comment

Alternatively specify the path to the Python file in launch.json "program": "${workspaceFolder}/myfile.py",
0

Just remove the first three lines (comments).

{
"version": "0.2.0",
"configurations": [
    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "args": [
            "..\\4\\shopping\\shopping.csv"
        ],
        "console": "integratedTerminal"
    }
]}

3 Comments

different error. does not see the arg Exception has occurred: SystemExit Usage: python shopping.py data File "C:\Users\Carlo\source\repos\CS50AI\4\shopping\shopping.py", line 14, in main sys.exit("Usage: python shopping.py data") File "C:\Users\Carlo\source\repos\CS50AI\4\shopping\shopping.py", line 184, in <module> main()
Could you provide shopping.py file. I think it is causing some errors because this JSON file is working perfectly on my VS code.
i think that this is the part that you are requesting: import csv import sys from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier # Check command-line arguments if len(sys.argv) != 2: sys.exit("Usage: python shopping.py data")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.