Skip to content

Run Python file in Terminal with error "& was unexpected at this time." #13466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bububut opened this issue Aug 17, 2020 · 27 comments
Closed

Run Python file in Terminal with error "& was unexpected at this time." #13466

bububut opened this issue Aug 17, 2020 · 27 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster

Comments

@bububut
Copy link

bububut commented Aug 17, 2020

Environment data

  • VS Code version: 1.48.0
  • Extension version (available under the Extensions sidebar): v2020.8.101144
  • OS and version: Windows 10 Pro 2004 build 19041.450

Expected behaviour

right click in Python file and choose "Run Python File in Terminal" to get the file run

Actual behaviour

it inserts a '&' symbol at the beginning of the command and the commands failed with "& was unexpected at this time."

my terminal is cmd.exe

It's the same problem as #5916
I believe it has been solved and that issue is closed, don't know why it happens again now

@bububut bububut added triage-needed Needs assignment to the proper sub-team bug Issue identified by VS Code Team member as probable bug labels Aug 17, 2020
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Aug 17, 2020
@ericsnowcurrently
Copy link
Member

@bububut, thanks for letting us know about this. I was not able to reproduce the problem with the following steps:

  1. change my default terminal to "cmd.exe"
  2. right-click in the .py file's editor window
  3. select "Run Python File in Terminal"

Here's what I see in my terminal pane:

C:\Users\...\my-workspace>"C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python37_64/python.exe" c:/Users/my-workspace/hello-world.py
Hello World!

What is the actual text in your terminal pane?

@ericsnowcurrently ericsnowcurrently added the info-needed Issue requires more information from poster label Aug 17, 2020
@bububut
Copy link
Author

bububut commented Aug 20, 2020

@ericsnowcurrently I cannot reproduce the problem myself after I restarted vscode today, so I'll close the ticket. Thanks for your help!

@bububut bububut closed this as completed Aug 20, 2020
@ghost ghost removed the triage label Aug 20, 2020
@ashishmahalka
Copy link

I am facing the same problem today. I have the below vs code version.
Version: 1.51.1 (user setup)
Commit: e5a624b788d92b8d34d1392e4c4d9789406efe8f
Date: 2020-11-10T23:34:32.027Z
Electron: 9.3.3
Chrome: 83.0.4103.122
Node.js: 12.14.1
V8: 8.3.110.13-electron.0
OS: Windows_NT x64 10.0.18363

image

@huntfx
Copy link

huntfx commented Feb 1, 2021

I just randomly started getting the issue in 1.50.1

B:\Projects\Code\Python\project>& py "b:/Projects/Code/Python/project/script.py"
& was unexpected at this time.

B:\Projects\Code\Python\project>& "C:/Program Files/Python37/python.exe" "b:/Projects/Code/Python/project/script.py"
& was unexpected at this time.

It's a bit annoying trying to fix, as all the issues keep getting closed without any actual solution

@vtasca
Copy link

vtasca commented Feb 18, 2021

Getting exact same issue today, this doesn't seem to have been resolved.

Version: 1.53.2 (user setup)
Commit: 622cb03f7e070a9670c94bae1a45d78d7181fbd4
Date: 2021-02-11T11:48:04.245Z
Electron: 11.2.1
Chrome: 87.0.4280.141
Node.js: 12.18.3
V8: 8.7.220.31-electron.0
OS: Windows_NT x64 10.0.19041

@huntfx
Copy link

huntfx commented Feb 18, 2021

Someone very helpfully mentioned a fix which worked in my case btw.
You just need to re-select your terminal via the dropdown menu.

image

@pasquale90
Copy link

For me the issue resolved by redirecting the console path to the target file that I would try to execute. Both cmd and python console in VSCODE are targeting the same target path.

@chupocro
Copy link

chupocro commented May 6, 2021

Same here.

VS Code: 1.56.0
Python extension: v2021.4.765268190

Everything worked well until some recent update. Now there isn't Select Default Shell command anymore, there is Select Default Profile where Command Prompt could be selected but the problem is Python extension is (instead of activate.bat) trying to run a PowerShell virtualenv activation script when using cmd and is adding & in front of the command.

Example:

path_to_project>& path_to_project/env/Scripts/Activate.ps1
& was unexpected at this time.

@Cubostar
Copy link

Cubostar commented May 6, 2021

For me the issue resolved by redirecting the console path to the target file that I would try to execute. Both cmd and python console in VSCODE are targeting the same target path.

How do you redirect the console path?

@LSgeo
Copy link

LSgeo commented May 7, 2021

#16175 is the currently open issue about this - I'm getting the same issue. Temporarily need to use powershell as the integrated terminal.

@plusk-dev
Copy link

I am getting the same issue. Also to mention, that when I try to format my Python files using Shift + Alt + F, it popped an alert:
image
When I click "Yes", the same error occurs
image
Same goes for running python files in the terminal.

@pasquale90
Copy link

@Cubostar Soz but it;s been so long and I really can t remember what was the exact process that I followed... I check my integrated terminal configurations though and I am temped to send you a screenshot.. (ctl-shift-P and type terminal.integrated.default -> edit in settings.json). Make sure you are in the right console directory when you apply changes in your settings (both Workspace and User).
KEEP A BACKUP COPY of the text stored in your settings.json before changing anything..

image

After changing the file, right click on file.py and choose "Open in integrated terminal" option.
pls let me know if that was it.

ps:If that fails, just re edit the file and paste the previous values stored before changing anything.

@csmontt
Copy link

csmontt commented May 13, 2021

I am getting the same error.

@pbthodges
Copy link

pbthodges commented May 13, 2021

Me too
fixed it by adding the line "terminal.integrated.shell.windows": "C:\Windows\System32\cmd.exe" (including double quotes)
as the second to last line, just before the last closing brace in
"C:\Users<yourusername>\AppData\Roaming\Code\User\settings.json"

@AAnusekhar
Copy link

iam also getting the same error

@0yv1nd
Copy link

0yv1nd commented May 14, 2021

Me too
fixed it by adding the line "terminal.integrated.shell.windows": "C:\Windows\System32\cmd.exe" (including double quotes)
as the second to last line, just before the last closing brace in
"C:\Users\AppData\Roaming\Code\User\settings.json"

I did this and edited the file in MS VC, so the full line looked like this (due to special char):

bilde

Worked for me!

@pbthodges
Copy link

Indeed, I didn't notice the double backslashes weren't included when I copied and pasted the string.
I'm trying again out of curiosity.
"terminal.integrated.shell.windows": "C:\Windows\System32\cmd.exe"

@pbthodges
Copy link

"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe"

There, you need 3 backslashes in the original string to generate 2 on the web page.

@ansonnn07
Copy link

"python.condaPath": "C:\\Users\\<your_pc_username>\\anaconda3\\Scripts\\conda.exe",
"python.terminal.activateEnvironment": true,

I had a friend having the same problem today and I told him to add these two lines into the settings.json file and it solved the problem. These settings just let the VS Code terminal know the right path for Anaconda, and also activate the environment automatically when you run your Python file in the terminal.

@ppostnov
Copy link

ppostnov commented May 20, 2021

image
This is helped me for bash terminal. I have added it to global vscode settings -> settings.json

@yMayanand
Copy link

Me too
fixed it by adding the line "terminal.integrated.shell.windows": "C:\Windows\System32\cmd.exe" (including double quotes)
as the second to last line, just before the last closing brace in
"C:\Users\AppData\Roaming\Code\User\settings.json"

yeah this worked for me too.

@Mohamed3nan
Copy link

image
This is helped me for bash terminal. I have added it to global vscode settings -> settings.json

worked for me but .bash_profile is not loading :/

@leo-cube
Copy link

Screenshot (172)>
I just randomly started getting the issue in 1.50.1

B:\Projects\Code\Python\project>& py "b:/Projects/Code/Python/project/script.py"
& was unexpected at this time.

B:\Projects\Code\Python\project>& "C:/Program Files/Python37/python.exe" "b:/Projects/Code/Python/project/script.py"
& was unexpected at this time.

It's a bit annoying trying to fix, as all the issues keep getting closed without any actual solution

i.) SOLUTION: Change the setting back -> Configure terminal settings to -> Integrated
and then come to the file and delete all the other interpreter that you have opened until the whole dialog box is gone.

ii.) Now right click and run on terminal. VS code must be able to print.

@ppostnov
Copy link

image
This is helped me for bash terminal. I have added it to global vscode settings -> settings.json

worked for me but .bash_profile is not loading :/

Do you have bash on your machine ?

@Mohamed3nan
Copy link

image
This is helped me for bash terminal. I have added it to global vscode settings -> settings.json

worked for me but .bash_profile is not loading :/

Do you have bash on your machine ?

yes

@StephD
Copy link

StephD commented Jun 3, 2021

I just had the same problem after removing the line that everyone is talking about.
And yeah, it's fix by putting this line back.
BUT

  1. Without this setting line. The code is succefully executed in the PowerShell of windows, even with the '&' that is a problem.
  2. Like on every screen, microsoft say it's deprecated and will change in the future. So will need to find an alternative solution later.

Would be great to find the actual bug or problem that need to be solved

@brianleect
Copy link

brianleect commented Jun 4, 2021

Just got this error as well. Happened after I couldn't interrupt a process running in terminal using Ctrl+C and forcefully closed it.

Code is able to be ran as per normal using powershell though

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster
Projects
None yet
Development

No branches or pull requests