45

i have a python file with the following content saved on my machine:

types_of_people = 10
x = f"There are {types_of_people} types of people"

binary = "binary"
do_not = "don't"
y = f"Those who know {binary} and those who {do_not}."

print(x)
print(y)

print(f"i said: {x}")
print(f"I also said: '{y}'")

hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}"

print(joke_evaluation.format(hilarious))
w = "This is the left side of ..."
e = "a string with a right side."

print(w + e)

When i open this file with Python 3.7 from within Visual Studio Code i get the following error:

/usr/local/opt/python/bin/python3.7 /Users/andree/Desktop/test.py
  File "<stdin>", line 1
    /usr/local/opt/python/bin/python3.7 /Users/andree/Desktop/test.py
    ^
SyntaxError: invalid syntax

In the following screenshot you can see the command i use to run the file and also which python extension i use.

run python file from within Visual Studio Code

But running the file from within my terminal with python3 test.py works just fine.

Does anyone know what the problem is when running it from within VS Code?

Andree Wille
  • 928
  • 1
  • 9
  • 22
  • 1
    I believe something may be funky with the Python interpreter settings/path in VSC. Can you confirm these? – DeepSpace Jul 26 '18 at 13:47
  • my terminal uses /usr/local/bin/python3 and vs code uses /usr/local/opt/python/bin/python3.7. But when i use the path vs code uses as command in my terminal it works as well – Andree Wille Jul 26 '18 at 13:50
  • Well, VSC tries to execute the line `/usr/local/opt/python/bin/python3.7 /../test.py` in the python interactive shell which is obviously wrong. Not sure what's up with that. – DeepSpace Jul 26 '18 at 14:15
  • Just make this thread clear: If you are stacked in Python interpreter (case when you have in terminal ">>") enter/write "exit()", "quit()", or select "**Ctrl-Z**" – Robert Kosulic Nov 20 '20 at 19:54

10 Answers10

127

Think this is a bug of VS Code.

When you use "run selection/line in python terminal" command, VS Code starts python interpreter and doesn`t quit it after completion.

You should use exit() command in python interpreter window to end python session.

After that "run python file in terminal" will work fine.

Hossein
  • 3,083
  • 3
  • 16
  • 33
Pavel
  • 1,271
  • 2
  • 6
  • 3
17

Looks like this is a bug in VS Code.

When i create a new file, assign python language to it and then save it then it works when i run the python file from within the editor.

But when i create a new file, assign python langauge but dont save it, execute "Run Selection/Line in Python Terminal" afterwards save it and then run "Run Python file in Terminal" it doen't work. So this seems to be an VS Code related issue.

Andree Wille
  • 928
  • 1
  • 9
  • 22
5

The problem for me was that I accidentally used Shift + Return which executed the python program, when in fact I meant to hit CTRL + Return to move to the next line without touching the mouse.

Using exit() command in the console worked.

zmag
  • 7,825
  • 12
  • 32
  • 42
amesj7595
  • 55
  • 1
  • 4
4

It's a probable bug in VS code. I don't know why there hasn't been a patch for this. After typing exit() in the terminal, the rerun should work fine. You could also try Ctrl+F5 to run in a debug mode.

yash
  • 122
  • 8
3

Disable terminal.integrated.inheritEnv in settings. This was suggested by VSCode for me and it worked.

2

I got the same issue, simply restart the Vs-Code it works for me !!

1

I experienced this issue when attempting to change my default terminal settings. I continually ran into a situation where the "Run Python File in Terminal" command would result in syntax errors while the "Run Selection/Line in Python Terminal" command would error but still display the results. Irritating to say the least.

Here's the settings I utilized to resolve the syntax errors issue.

Note: Enabling Pylint did not resolve my issue, in fact it continued to pop-up even after selecting to enable it. These specific user/workspace/folder settings resolved that issue for me too.

Note: Since the terminal defaults to Powershell, you have to type Python to enter manual commands directly into the python terminal and exit() to close it to allow the python file to run properly again.

USER SETTINGS

{
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
}

WORKSPACE SETTINGS

"settings": {
    "terminal.integrated.shell.windows": "C:\\Python3.7.2\\python.exe",
}

FOLDER SETTINGS

"python.linting.pylintEnabled": true,
"python.pythonPath": "C:\\Python3.7.2\\python.exe",
TechBrad
  • 87
  • 1
  • 3
0

I found a fix for this, install "pylint". I had a pop-up message in Visual Studio that asked me to download this extension. I did and after that I was able to run my code!

0

I got the same issue, but the code ran for me when I ran it using 'Start without debugging'. This can also be done with the shortcut CTRL + F5.

Zoe
  • 27,060
  • 21
  • 118
  • 148
killerG
  • 106
  • 5
0

I found the issue is generated by trailing spaces after the loop functions. So what I do to alleviate it is add an empty print () statement at the very end of the script

Polto
  • 95
  • 1
  • 10