38

I'm trying to run a Python script from Visual Studio code, but the script fails to run and crashes with a SyntaxError pointing to the comment at the beginning of launch.json.

launch.json:

{
    // 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 | Default",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${file}",
            "cwd": "${workspaceFolder}",
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": [
                "RedirectOutput"
            ]
        }
    ]
}

Terminal Output:

File ".../.vscode/launch.json", line 2
    // Use IntelliSense to learn about possible attributes.
     ^
SyntaxError: invalid syntax

settings.json:

{
    "python.pythonPath": "${workspaceFolder}/venv/bin/python"
}

I was working on my Windows machine earlier and all of this worked perfectly fine. For some reason, VSCode is trying to run the launch.json file through Python and // is an invalid comment syntax in Python. If I remove the comments, I get this error:

Traceback (most recent call last):
  File ".../.vscode/launch.json", line 8, in <module>
    "stopOnEntry": false,
NameError: name 'false' is not defined

If I use Python's False, I don't crash but nothing happens and my script does not run. It seems very much like launch.json is being parsed by Python erroneously. Any fix for this?

1
  • Please do not edit solution announcements into the question. Accept (i.e. click the "tick" next to it) one of the existing answer, if there are any. You can also create your own answer, and even accept it, if your solution is not yet covered by an existing answer. Compare stackoverflow.com/help/self-answer Commented Jun 6, 2022 at 18:40

4 Answers 4

59

I found my problem. I did not update the program key to always point to my main.py. Instead, the current open file was being executed as a Python script -- launch.json Changing the program key or navigating to a different file solved the problem. Obvious once you notice it!

For Example:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "main.py",
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

7

Solution 1

I consider that an easier solution is:

  1. Close the launch.json on the editor group
  2. Open the python file such as main.py to be debugged
  3. [Run]-[Start Debugging] (F5)

As Nick mentioned, when focusing on the launch.json in the editor, the debug system runs on the launch.json itself, not a python file.

Solution 2

Modify the "program" in the launch.json as below:

"program": "${workspaceFolder}/main.py",

It corresponds to

the program key to always point to main.py

as Nick said.

Note that the above modification may not work well if the main.py places in a deep directory.

1 Comment

That kinda makes sense, although in my case I didn't even have launch.json open. I opened it, switched to my .py module, then launched the debugger. It worked after that. I don't get it, but I won't "look a gift horse in the mouth".
4

Closing launch.json if it is open for editing may solve the issue

If launch.json is the latest open file, VSCode may be trying to run launch.json as a Python module (despite the fact that it's clearly not a Python module).

See the NameError in the OP's third screenshot - looks like Python interpreter running against launch.json

(Note: the contribution of this answer is solely to put the crux of Haru's Solution 1.1 and Nick's own self-diagnosis into simple language in the answer's first line)

Comments

-1

write this in your launch.json file [steps to create the launch.json file:

  1. go to the run and debug section
  2. click on create json file
  3. choose the debugger of the language you are using]

Now write this in the launch.json file:

{
// 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: Django",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}\\manage.py",
        "args": [
            "runserver"
        ],
        "django": true,
        "justMyCode": true
    }
]
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.