Python's if __name__ == "__main__" syntax error SyntaxError: invalid syntax

Encountered a python error today:

File "main.py", line 107
   if __name__ == "__main__"
                           ^
   SyntaxError: invalid syntax

After a search, the sayings on the Internet are basically missing a space if __name__ after if.
But I have spaces here, and manually deleting the spaces and reinserting the spaces will not work.

Finally, I checked for a long time, because there are many commented codes in the middle, just delete those codes. Later, for the first time, I found that it was because there was a blank line in the middle of a certain function, just delete the blank line. Presumably the previous redundant code may have also introduced blank lines.

In short the solution:

  1. Check if there is a missing space if __name__ after if.
  2. Check function for extra blank lines
  3. Check the commented redundant code, deleting it may have unexpected effects.

Guess you like

Origin blog.csdn.net/toopoo/article/details/126372300