`SyntaxError: invalid syntax` when starting Python script in VS Code on macOS
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)
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!
Solution 1
I consider that an easier solution is:
- Close the launch.json on the editor group
- Open the python file such as main.py to be debugged
- [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.