Fixing Invalid Syntax error when running python from Visual Studio Code

Invalid Syntax error when running python from Visual Studio Code is an error which occurs because of a bug in VS Code.

In the blog post I will attempt to explain why this error is taking place and how to solve it, I will also add other solutions that could solve the error for you.

Exploring the Invalid Syntax error when running python from Visual Studio Code

This is an error which occurs because of a bug in VS Code.

Please do not mix between multiple errors. Check if the error message looks like the error message bellow.

                                                                       #
SyntaxError: invalid syntax
                                                                       #

Bellow I will present multiple solutions some have worked for me and others have worked for other devs.

Solution 1 : Correctly close the python interpreter

If you click on run python file in the terminal and the python interpreter opens up and gets stuck.

The solution is to use the command bellow to close the python interpreter.

                                                                       #
exit()
                                                                       #

Now, try to run the file in the terminal and everything should work fine. But if that is not the case for you , please try the method bellow which might work.

Solution 2 : Disable terminal.integrated.inheritEnv in the settings in VS Code

Another solution is to disable terminal.integrated.inheritEnv in the settings in VS Code.

To do that, follow these steps.

First, go to the upper right in Vs code and click on file. Go to Preferences and then click on Settings.

The settings window will open.

Type inheritEnv in the search bar. You should see : Terminal › Integrated: Inherit Env.

UNCHECK It , and get out of the settings. That is it, this is all you have to do for everything to work just fine on the next run of your script.

I hope the fixes above fixed your problem, good luck with your python Journey and consider donating to my Kofi account if you wish to help. Thank you.

Summing-up : 

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/