0

I wrote some very simple code:

def yo():
    text = "hi there"
    print(text) 
    print(text) 
  
  
yo()

I ran this in Spyder and online compilers without error. Obviously it spits out:

hi there
hi there

But when I run this in VS Code terminal using the "Run Python file in terminal" play button I get

"SyntaxError: invalid syntax" 

for line 1 (the def line).

When I type yo() into the terminal itself, I get the expected output of:

hi there
hi there

Why is do I get a different result from these? I executed other simple bits of Python in VS Code using the "play" button without issue. It goes without saying that I have the python extension and interpreter installed.

UPDATE: I restarted VS Code and now the file runs without issue. I guess "did you restart the computer" really does solve the issue sometimes...

Andrew
  • 131
  • 1
  • 8
  • You appear to have a couple of leading spaces on that first line. That is indeed a syntax error; indentation *means something* in Python! – jasonharper Oct 13 '21 at 01:13
  • Sorry that was just a copy and paste error. Those leading spaces are not actually in the source code. I've edited my OP to show the correct version of what's in VS Code – Andrew Oct 13 '21 at 01:16
  • Did you try this?https://stackoverflow.com/questions/29987840/how-to-execute-python-code-from-within-visual-studio-code – Python learner Oct 13 '21 at 01:32
  • You run this in VS Code terminal? You should try running in other command line tools or terminals and should get the same error. This should not be a specific issue of Visual Studio Code. In recent Visual Studio Code with Python extension there are some easier ways to run a Python script, which is already introduced by @Pythonlearner – rustyhu Oct 13 '21 at 01:43
  • @rustyhu Yes, I ran this in the VS Code terminal. When I run it in the Python terminal itself I get and invalid syntax error for the recall line (the last line): yo() This is a different line than what VS Code says has the syntax error and again, when I run this in Spyder or https://www.programiz.com/python-programming/online-compiler/ it works fine – Andrew Oct 13 '21 at 01:53
  • @Andrew 1. Did you edit your code and run before you save it? 2. Can you try running a Python `formatter` (such as Autopep8, Black) on your code before you run it? If this works, there must be some whitespaces or indentation differences between the code your post here and what you run on your machine. I can not reproduce the error you report with the code you post. – rustyhu Oct 13 '21 at 01:59
  • I can try that but this is such a simple script... – Andrew Oct 13 '21 at 02:11
  • if you type directly in the terminal you ALWAYS get `error on line 1`, save the file and use `python myfile.py` to get a valid line number – rioV8 Oct 13 '21 at 08:42

1 Answers1

0

Your function - yo(), is being defined, however Visual Studio Code does not know how to run it. To fix this, try adding the if __name__ == '__main__': clause. Here is your full code:

def yo():
    text = "hi there"
    print(text) 
    print(text) 

if __name__ == '__main__':
    yo()

Here is some more information about if __name__ == '__main__':

If that doesn't fix it, you must have some formatting issues or some different settings of Visual Studio Code. You could do the following things.

  1. Make sure you're running the right file
  2. Delete all of the code and paste it in again
  3. Reset your Visual Studio Code settings
  • Make sure your settings for Tab are 4 spaces.
  • Disable terminal.integrated.inheritEnv in Settings

If all else fails, try these:

You should use the exit() command in the terminal to end python session. Then re-run and see if anything works.

Run your code using 'Start without debugging'.

krmogi
  • 2,588
  • 1
  • 10
  • 26
  • That gives me the same error. I'll try messing around with the formatting but this code is literally so simple... anything that isn't on the first line is indented one tab in. I'm new to Python but I'm experienced in other languages so I highly doubt something simple like a space would escape my inspection of the code – Andrew Oct 13 '21 at 02:16
  • @Andrew after some more research, I found out that other people are having the same issue. This seems to be a bug of VScode. Take a look at my updated answer. – krmogi Oct 13 '21 at 02:28
  • If this is a bug I don't know why I received -3 for a valid question... I edited my question because I found another method was working. What are your thoughts based on this update? – Andrew Oct 13 '21 at 02:29
  • 1
    UPDATE: I restarted VS Code and now it suddenly works. Pretty aggravating. Thanks for your help! – Andrew Oct 13 '21 at 02:32
  • your first line in the answer is WRONG. Every line in Python is always executed, `if __name__ == '__main__':` is used to prevent the execution of some code in case you import the file. Your answer is not the solution to the problem, tab settings are unimportant – rioV8 Oct 13 '21 at 08:40