Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Using Databases in Python Meet Peewee Queries Are Your Friend

Nicholas Steenson
Nicholas Steenson
3,323 Points

Getting a Syntax Error at "main__':"

I keep getting this error:

File "students.py", line 32 if name == "main": ^ SyntaxError: invalid syntax

This even occurs when I run the code copied and pasted into the workspace directly from the project files!


I think what you are looking for is a test that this code is being run from the command line. That test is:

if name == 'main': do_something_now


3 Answers

Nicholas Steenson
Nicholas Steenson
3,323 Points

Ryan Carson : Yes, it would appear in my title I did, though I think that was more to abbreviate here,. In my code (and Ron Fisher 's code too) the underscores just made the enclosed titular/headers or bold perhaps, I'm not that great with markup.

BUT the problem remains. I mentioned that I simply download the completed code from the project files, then copy and paste it into a Treehouse Workspace, which presumably shouldn't have the same errors I'm experiencing unless they aren't related to my code, and they do have those errors.

OR at least they did yesterday, as when I restarted my browser and tried again this morning, the error wasn't present, even without altering any of my workspace code!

Odd quirk of my browser perhaps!

Thanks for the help either way guys.

You forgot the __ before main. Like this:

if __name__ == '__main__':

That is VERY interesting. Last night when I pasted that response into the comment box, the __'s were there on both front and back. Then when I look at it this morning they are gone. I literally cut and pasted it from a piece of code I have so I don't understand why it was removed. Hmmmmm!?


Nicholas Steenson
Nicholas Steenson
3,323 Points

Thanks for the pointer, I had tried to abbreviate it here for brevity. But I was copy and pasting the entire code-set from the finished .py in the attached tutorial docs, and still had the error until I restarted my PC, the browser, or just the next day :-P

Alex Koumparos
Alex Koumparos
Python Development Techdegree Student 36,862 Points

Hi Nicholas,

I know that this is a pretty old answer, so you've probably moved on from this problem but in case anyone else stumbles into the same problem:

The most likely reason for you to get a syntax error at the end of the following valid code

if __name__ == "__main__":

is because you have a syntax error earlier in the code, but it's not until the interpreter gets to the end of this statement that it realises that there is a problem. A common syntax error you might have is a missing close parenthesis on the last line of your code before this statement. Something like the following would trigger the problem you describe:

print("Hello, world!"

if __name__ == "__main__":
    print("hello again")

As for the other problem that's come up in this thread, the case of the disappearing underscore characters, this is because this site uses Markdown and Markdown treats text enclosed in double underscores as strong emphasis (typically rendered as bold). You might not have realised this when you were reviewing the Markdown Cheatsheet linked at the bottom of every Comment page because the Cheatsheet only lists ** as code for strong emphasis, but if you check out the official syntax documentation for Markdown, you'll see that ** and __ are equivalent. To avoid having Markdown apply styling to your code snippets you need to wrap the code in backticks (as per the Cheatsheet).

Hope that helps,