[英]`SyntaxError: invalid syntax` when starting Python script in VS Code on macOS
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
.我正在尝试从 Visual Studio 代码运行 Python 脚本,但该脚本无法运行并崩溃,并且
SyntaxError
指向launch.json
开头的注释。
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
: settings.json
:
{
"python.pythonPath": "${workspaceFolder}/venv/bin/python"
}
I was working on my Windows machine earlier and all of this worked perfectly fine.我早些时候在我的 Windows 机器上工作,所有这些工作都非常好。 For some reason, VSCode is trying to run the
launch.json
file through Python and //
is an invalid comment syntax in Python.出于某种原因,VSCode 试图通过 Python 运行
launch.json
文件,而//
在 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.如果我使用 Python 的
False
,我不会崩溃,但什么也不会发生,我的脚本也不会运行。 It seems very much like launch.json
is being parsed by Python erroneously.看起来很像
launch.json
被 Python 错误地解析了。 Any fix for this?有什么解决办法吗?
I found my problem.我发现了我的问题。 I did not update the
program
key to always point to my main.py
.我没有更新
program
密钥以始终指向我的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.相反,当前打开的文件是作为 Python 脚本执行的
launch.json
更改program
密钥或导航到不同的文件解决了这个问题。 Obvious once you notice it!一旦你注意到它就很明显了!
I consider that an easier solution is:我认为更简单的解决方案是:
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.正如 Nick 提到的,在编辑器中关注 launch.json 时,调试系统在 launch.json 本身上运行,而不是 python 文件。
Modify the "program" in the launch.json as below:修改launch.json中的“程序”如下:
"program": "${workspaceFolder}/main.py",
It corresponds to它对应于
the program key to always point to main.py
程序键始终指向 main.py
as Nick said.正如尼克所说。
Note that the above modification may not work well if the main.py places in a deep directory.请注意,如果 main.py 放在较深的目录中,上述修改可能无法正常工作。
Closing launch.json
if it is open for editing may solve the issue如果它可以编辑,则关闭
launch.json
可能会解决问题
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).如果
launch.json
是最新打开的文件,VSCode 可能会尝试将launch.json
作为 Python 模块运行(尽管它显然不是 Python 模块)。
See the NameError
in the OP's third screenshot - looks like Python interpreter running against launch.json
请参阅 OP 的第三个屏幕截图中的
NameError
- 看起来像 Python 解释器针对launch.json
运行
Update: need to read it a bit carefully (and look at the code), but the accepted answer is a more permanent solution for most use cases - though I think VS Code's Python plugin should at least warn when trying to run a non- .py
file as Python code更新:需要仔细阅读(并查看代码),但对于大多数用例来说,公认的答案是一个更永久的解决方案——尽管我认为 VS Code 的 Python 插件在尝试运行非
.py
文件作为 Python 代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.