Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


__name__ == "__main__"

i don't really get what it does or means at all. ( name == "main" ). Are there any courses on here that are focused on this topic? because I've heard it need a whole course on it's own. if there is one hook me up. If not, can i get a brief about it.

note: I've watched many videos about this subject on Youtube but I still don't get it.

1 Answer

Øyvind Andreassen
Øyvind Andreassen
16,839 Points

I think this StackOverflow answer answer it pretty good.

Basically, if __name__ == "__main__" determines if the file is being imported into another file as a module, or if it's running on its own. In the code below, I want my_func to execute as soon as I call the file, but if I need the functions in another project, I can import the function, without running it. If the if-statement wasn't there, the function would execute as soon as the file was being imported.

def my_func():
   return some_value

if __name__ == "__main__":

oh alrighty, I get it. That was actually simple, thanks a lot!