Syntax Error on Code w/ No Mistakes

Good Morning,

I’m very new to Python, and have run into the following issue (not sure if unique to Visual Studio Code).

I tried some VERY basic code (see below). It works initially (provides correct output of “7”), but when trying to change the code (add another line), if I get a syntax error, I am unable to clear the error.

The copy of the code is below. When I open the file containing this code, it works fine. If I make some changes that happen to include an error, then I get the syntax error message (which is to be expected). However, if I then revert back to the code below (exactly as it’s shown, no changes at all), then I still get syntax errors. I have to close and reopen Visual Studio Code in order for the error to longer appear when I run the code.

Hopefully my explanation makes sense. Ultimately, my problem is the code below sometimes runs fine, other times shows syntax error. Any idea why that might happen?

Thank you!

x = 3
y = 4
sum = x + y
print (sum)

There doesn’t seem to be something in this code that might be causing syntax errors. Perhaps file is not saved after reverting the changes and/or still the old version is being run? It might be helpful to include what exactly error you are getting then.

Two unrelated to the issue notes - python has build in function called sum, so calling variable like that might not be a best choice. You shouldn’t put space between print and (sum).

1 Like

Thank you for the suggestion (and tips)! The save hypothesis sounded very promising, but unfortunately doesn’t seem to have solved issue.

I’ve changed the code to the following

x = 3
y = 4
taco = x + y

Works fine initially, but if I make changes that happen to result in a legitimate syntax error, and then revert back to the presumably error-free code above, it still keeps showing that error (even if I save again). Only solution seems to be to restart Visual Studio Code.

The error I’m receiving is as follows:

SyntaxError: invalid syntax

& C:/Users/jesse/AppData/Local/Programs/Python/Python38-32/python.exe “c:/Users/jesse/Desktop/Coding/Python/Misc Files/”
File “”, line 1
& C:/Users/jesse/AppData/Local/Programs/Python/Python38-32/python.exe “c:/Users/jesse/Desktop/Coding/Python/Misc Files/”
SyntaxError: invalid syntax

If restarting VSCode makes the error go away, then it’s undoubtedly a problem with VSCode, not your Python code.

1 Like

Thank you. I’ll try working in another editor.

It could just be a bad plugin. Are you using any custom linters or other python-related plugins?

If you’re shopping around for a different editor, I recommend giving PyCharm a try.

1 Like

Are you using the terminal in VS Code to call your script, or are you using an external cmd?

1 Like

I believe I have some plugins (or rather modules??) that were added. I can’t recall what, to be honest.

For example, I tried following a tutorial to pull historical stock prices up using Python. I believe that required me to add a module (what exactly, I’m not sure).

And apologies, my terminology is probably all off (not sure if plug in and module are the same in this case).

I’m using the terminal in VS Code

“module” is most likely referring to a python library, whereas “plugin” would be a vscode extension (I forgot it calls them “extensions” and not “plugins”).

Try running code --list-extensions from a terminal and paste in the output here.

1 Like

Ah okay, got it.

Here’s the output I received:

C:\Users\jesse>code --list-extensions

"I 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."

from a StackOverflow thread