问题
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". 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. 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.
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. I've restarted VSCode and my PC, it appears to be broken.
回答1:
Fixed this by downgrading my version of the Python extension to the previous version. It appears as though the latest version (released only hours ago) is bugged (for me at least).
回答2:
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.
回答3:
Instead of downgrading, you can also change the default terminal from cmd to powershell.
To do so, go to your settings.json and replace the "terminal.integrated.shell.windows"
parameter
from the path to cmd (e.g. "C:\\Windows\\System32\\cmd.exe"
)
to the path to powershell (e.g. "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
)
回答4:
I had the same issue with the additional & character in front of the python command. This bug has been fixed meanwhile with release 2019.5.18678 (5 June 2019) of the Python extension.
来源:https://stackoverflow.com/questions/56456924/ampersand-syntax-error-running-any-python-script-in-vscode