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

Viewed 6395

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.

5 Answers

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") If you are using VScode press ctrl+shift+p and search for default terminal and change it to powershell.

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).

I am experiencing the same bug with release 2021.5.842923320 (05-14-2021).

Adding "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" to the settings is a workaround.

Further information can be found in Issue #16175.

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.

This is fixed in VS Code version 1.59.xxx or later.

Related