简体   繁体   English

在 VSCode 中运行任何 Python 脚本的“&”语法错误?

[英]Ampersand "&" syntax error running any Python script in VSCode?

In VSCode, I typically run a Python Script using the Python extension and right clicking a .py script and selecting "Run Python File in Terminal".在 VSCode 中,我通常使用 Python 扩展名运行 Python 脚本,然后右键单击 .py 脚本并选择“在终端中运行 Python 文件”。 Before today this method worked fine, but now I'm experiencing the below issue...在今天之前,这种方法效果很好,但现在我遇到了以下问题......

$ & C:/Users/.../Python/Python37-32/python.exe c:/.../test.py
bash: syntax error near unexpected token `&'

Now I can't run my python scripts using this method.现在我无法使用这种方法运行我的 python 脚本。 I can see the cause of the error, I'm just not sure why VSCode is prefixing that ampersand, and whether it was doing that before or not.我可以看到错误的原因,我只是不确定为什么VSCode 会在该 & 符号前加上前缀,以及它之前是否这样做过。

I was playing around with virtualenv, and perhaps this is the cause, but I'm experiencing this issue even when the virtual environment is not active.我在玩 virtualenv,也许这就是原因,但即使虚拟环境不活跃,我也会遇到这个问题。 I've restarted VSCode and my PC, it appears to be broken.我重新启动了 VSCode 和我的电脑,它似乎坏了。

Instead of downgrading, you can also change the default terminal from cmd to powershell.除了降级,您还可以将默认终端从 cmd 更改为 powershell。
To do so, go to your settings.json and replace the "terminal.integrated.shell.windows" parameter为此,请转到您的settings.json并替换"terminal.integrated.shell.windows"参数
from the path to cmd (eg "C:\\Windows\\System32\\cmd.exe" )从路径到 cmd(例如"C:\\Windows\\System32\\cmd.exe"
to the path to powershell (eg "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" ) If you are using VScode press ctrl+shift+p and search for default terminal and change it to powershell.到 powershell 的路径(例如"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" )如果您使用的是 VScode,请按 ctrl+shift+p 并搜索默认终端并进行更改到powershell。

Fixed this by downgrading my version of the Python extension to the previous version.通过将我的 Python 扩展版本降级到以前的版本来解决这个问题。 It appears as though the latest version (released only hours ago) is bugged (for me at least).似乎最新版本(仅在几个小时前发布)被窃听(至少对我而言)。

I am experiencing the same bug with release 2021.5.842923320 (05-14-2021).我在 2021.5.842923320 (05-14-2021) 版本中遇到了同样的错误。

Adding "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" to the settings is a workaround.在设置中添加"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"是一种解决方法。

Further information can be found in Issue #16175 .更多信息可以在问题 #16175中找到。

Confirmed.确认的。 I have that problem too.我也有这个问题。 Repeating the run command in TERMINAL and manually removing "&" at the beginning of the run command will let the code run.在 TERMINAL 中重复运行命令并手动删除运行命令开头的“&”将使代码运行。

I had the same issue with the additional & character in front of the python command.我对 python 命令前面的附加 & 字符有同样的问题。 This bug has been fixed meanwhile with release 2019.5.18678 (5 June 2019) of the Python extension.此错误已在 Python 扩展的 2019.5.18678(2019 年 6 月 5 日)版本中得到修复。

这在 VS Code 版本 1.59.xxx 或更高版本中已修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM