Invalid Syntax error when running python from inside Visual Studio Code
Solution 1
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.
Solution 2
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.
Solution 3
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.
Solution 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.
Solution 5
Disable terminal.integrated.inheritEnv in settings. This was suggested by VSCode for me and it worked.
Andree Wille
Web Developer based in Hamburg, Germany. Currently focussing on TypeScript, JavaScript and Reactjs
Updated on December 28, 2021Comments
-
Andree Wille over 2 years
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.
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?
-
TJ Trapp over 5 yearsrestarting vs code fixed this for me after installing pylint
-
TJ Trapp over 5 yearsi had to restart vscode but yea installing pylint worked 4 me
-
lode almost 5 yearsworked for me, but restarting vscode after having pylint installed worked too!
-
Joaquin684 almost 5 yearsThanks a lot. I think this should be considered a bug, not intuitive at all.
-
needfulthing over 4 yearsYep, a restart is required for pylint to work properly after installation. I had the same problem with false error messages before.
-
Pramesh Bajracharya almost 3 yearsYeap, worked for me too. I confidently ignored this suggestion from VSCode earlier. Now, I am here having that ahhh... moment.