Python Tutorial: if __name__ == ‘__main__’

///Python Tutorial: if __name__ == ‘__main__’

Python Tutorial: if __name__ == ‘__main__’

FavoriteLoadingAdd to favorites

In this video, we will take a look at a common conditional statement in Python:
if __name__ == ‘__main__’:

This conditional is used to check whether a python module is being run directly or being imported.

If you enjoy these videos and would like to support my channel, I would greatly appreciate any assistance through my Patreon account:
https://www.patreon.com/coreyms

Or a one-time contribution through PayPal:
https://goo.gl/649HFY

If you would like to see additional ways in which you can support the channel, you can check out my support page:

You can find me on:
My website – http://coreyms.com/
Facebook – https://www.facebook.com/CoreyMSchafer
Twitter – https://twitter.com/CoreyMSchafer
Google Plus – https://plus.google.com/+CoreySchafer44/posts
Tumblr – https://www.tumblr.com/blog/mycms

By |2017-05-01T12:23:17+00:00April 26th, 2017|Programming Video Tutorials, Python Video Tutorials|50 Comments

50 Comments

  1. preetham dasari April 26, 2017 at 9:10 pm - Reply

    Thank you!!

  2. Satadhi Halder April 26, 2017 at 9:11 pm - Reply

    that was totally worth it thanks !

  3. Yilei Zhang April 26, 2017 at 9:11 pm - Reply

    Very clear. Was looking at Linda, but this is much clearer. Thanks!

  4. xx482 April 26, 2017 at 9:12 pm - Reply

    thanks for helping me understand the concept.

  5. Bobby Amoroso April 26, 2017 at 9:13 pm - Reply

    That’s what I needed… Thanks so much!

  6. Senseblend April 26, 2017 at 9:13 pm - Reply

    what?

  7. GhostWavves Evoluzion April 26, 2017 at 9:14 pm - Reply

    U using sublime ?

  8. Ashok nuthalapat April 26, 2017 at 9:15 pm - Reply

    Thanks much Corey 🙂

  9. rgarciaf071 April 26, 2017 at 9:16 pm - Reply

    Can you please make a video on how to apply composition pattern on Python, your videos are quite concise and easy to understand so I think you could make one 🙂

  10. Billy Wilson Arante April 26, 2017 at 9:18 pm - Reply

    +Corey Shafer thank you!

  11. Layla Zhang April 26, 2017 at 9:19 pm - Reply

    Amazing video! I was so confused after reading this popular stack overflow post: http://stackoverflow.com/questions/419163/what-does-if-name-main-do (so many run-on sentences, no idea how people understood this guy lol)

  12. Darius April 26, 2017 at 9:20 pm - Reply

    Best explanation ever. Thanks. Very clear and simple.

  13. Richard Beddoes April 26, 2017 at 9:20 pm - Reply

    Yup great video. Thanks!

  14. Divya Nukala April 26, 2017 at 9:24 pm - Reply

    Awesome explanation….

  15. jaswanth reddy April 26, 2017 at 9:24 pm - Reply

    awesome explanation. Thanks a ton!

  16. Людмила Корнилова April 26, 2017 at 9:25 pm - Reply

    THANK YOU, MAN!

  17. Greg M April 26, 2017 at 9:25 pm - Reply

    Which package did you use to get the definitions as you type like S.format at 0:58?

  18. Ben Sassoon April 26, 2017 at 9:26 pm - Reply

    Great video. Thank you

  19. Ronald Coe, Jr. April 26, 2017 at 9:27 pm - Reply

    Very clear and concise. Nice work, sir.

  20. Kier Bunnyhood April 26, 2017 at 9:32 pm - Reply

    Well done. Even I got it this time 🙂

  21. Sumit Singhal April 26, 2017 at 9:34 pm - Reply

    You are simply Awesome Man…!!

  22. Hamid S April 26, 2017 at 9:35 pm - Reply

    maybe you should have mentioned global vs local to clear up the main() at the end

  23. Just another _ April 26, 2017 at 9:36 pm - Reply

    Thank you so much!! was so confused before and this reaally helped for an exam tomorrow! Appreciate it 🙂

  24. joga gaja April 26, 2017 at 9:36 pm - Reply

    Great man, you know to explain in crystal clear way. Cheers

  25. Christian Liin Hansen April 26, 2017 at 9:38 pm - Reply

    Totally perfect explained with nice and clear voice.

  26. Yuchao Jiang April 26, 2017 at 9:39 pm - Reply

    Thank you. very clear.

  27. Andy Wilson April 26, 2017 at 9:40 pm - Reply

    Very helpful video, excellent examples and demonstration. Thanks!

  28. Zhen Cao April 26, 2017 at 9:42 pm - Reply

    Nice man. Thank you for share your knowledge.

  29. Inhale Death April 26, 2017 at 9:43 pm - Reply

    You explain very good! I realized often, how easy most things are, but people forget to mention little things that make everyting confusing. I am one of these people who tend to have a lot of open questions after a video. Not so here. Subscribed! Go on, you are way better than many reknown tutors.

  30. Sni PeO April 26, 2017 at 9:43 pm - Reply

    Very clear 🙂 Tnx Bro 🙂

  31. DanielG00 April 26, 2017 at 9:44 pm - Reply

    How did you get a console at the bottom?

  32. Da Wei April 26, 2017 at 9:46 pm - Reply

    Sorry to raise this rookie’s question.
    In the video you use a method in print as :
    print ‘first is: {}’. format(__name__)
    After having played with it a bit I found it to be the same with
    print ‘first is: %s’ %(__name__)

    I wonder if there is any difference between them and could you briefly introduce a bit about the advantage (or some tricks) of using "{} and .format"?

  33. monika manwani April 26, 2017 at 9:48 pm - Reply

    Crystal clear explanation…thanks..hope u r coming with more videos like this

  34. Abu Yusuf Ansari April 26, 2017 at 9:50 pm - Reply

    Hi, Corey.
    I have a question in mind.
    Why should I use this "__name__/__main__" thing in my code?
    Does it entail any advantage in my code?

    Thank you.

  35. ado man April 26, 2017 at 9:50 pm - Reply

    Thank you.

  36. Brandon April 26, 2017 at 9:50 pm - Reply

    ¡Thank you!

  37. Dave Ko April 26, 2017 at 9:51 pm - Reply

    excellent thank!

  38. Js jS April 26, 2017 at 9:52 pm - Reply

    very good video, thank you.

    note: please name example methods something like abc123

    that would eliminate any confusion surrounding "def main()" and "__main__"

    one thing that should have been mentioned:

    if __name__ == ‘__main__’ :

    is a good place for self-contained test code when you are developing a module.
    that way you do not have to do an import to test the methods in the module

    simple example:

    def abc123(myData):
    return myData + 25 # extremely complicated data processing … lol

    if __name__ == ‘__main__’ :
    print ( abc123(12) ) # check if method returns the correct value

  39. Anton Demkin April 26, 2017 at 9:53 pm - Reply

    Thank you so much for explaining this. Noew i really understand why this construction is used.

  40. Shams Azad April 26, 2017 at 9:54 pm - Reply

    Thank you

  41. Rahul Roy April 26, 2017 at 9:54 pm - Reply

    Nice Explanation, thanks a lot

  42. prudhvi theja April 26, 2017 at 9:54 pm - Reply

    It was also good to note that in the final example, at https://youtu.be/sugvnHA7ElY?t=492
    if the print statement in the first_module was

    def main():
    print("first module’s name is ",format(__name__))

    Even when this main was called from second_module, the __name__ value is ‘first_module’ and not __main__

  43. Unknown April 26, 2017 at 9:56 pm - Reply

    finally! thank you so much for this video. very well explained. you now have a new subscriber.

  44. Ian Chong April 26, 2017 at 9:58 pm - Reply

    this is awesome!!!!

  45. Shahzad R. April 26, 2017 at 9:59 pm - Reply

    You are amazing…

  46. Nor Otany April 26, 2017 at 10:00 pm - Reply

    Good to know, but I won’t learn much python if I have to spend almost nine minutes learning such simple lessons.

  47. Peter McQuaid April 26, 2017 at 10:01 pm - Reply

    Great video!

  48. iggirex April 26, 2017 at 10:02 pm - Reply

    clear concise to the point. I’m subscribed.

  49. Christian Johansen April 26, 2017 at 10:03 pm - Reply

    Very good explanation. Easy to understand for us non-developers.

  50. kareem sakr April 26, 2017 at 10:03 pm - Reply

    the goto channel for python

Leave A Comment

*