Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Syntax error when running python from inside Visual Studio Code

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?

like image 208
Andree Wille Avatar asked Jul 26 '18 13:07

Andree Wille


People also ask

How do I fix SyntaxError invalid syntax in Python?

You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

Why is my Python code saying invalid syntax?

Syntax errors are produced by Python when it is translating the source code into byte code. They usually indicate that there is something wrong with the syntax of the program. Example: Omitting the colon at the end of a def statement yields the somewhat redundant message SyntaxError: invalid syntax.


1 Answers

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.

like image 101
Pavel Avatar answered Oct 04 '22 00:10

Pavel