Why You Should Use Python if __name__ == "__main__"

Поділитися
Вставка
  • Опубліковано 31 лип 2024
  • In this video, I show you how and why you should always use if _name_ == '__main__' in python and answer the question of why there is no Python main function. Additionally, you will see how to prevent Python modules from executing using Python __main__.
    ⏬ Python Cheat Sheets: k0nze.gumroad.com
    👉 Website Article for this Video: k0nze.dev/posts/python-name-main
    💌 Support me on Patreon: / k0nze
    ☕️ Buy me Coffee: www.buymeacoffee.com/k0nze
    🌍 Check out my Website: k0nze.dev
    💻 Join our Discord: discord.k0nze.dev
    👔 Follow me on LinkedIn: linkedin.k0nze.dev
    Recording Gear
    Camera - Sony A6400: geni.us/zRw1
    Lens - Viltrox AF 23mm f1.4: geni.us/RVrguGd
    Microphone - Rode Wireless Go: geni.us/RBAOIx
    Keyboard - Keychron K2 v2: geni.us/DPK87Xn
    ⏰ Timestamps:
    0:00 Intro
    0:17 Python does not have a main function
    0:45 How does the Python interpreter work?
    2:13 Prevent Python import from execution
    2:33 Python _name_
    4:14 Summary

КОМЕНТАРІ • 73

  • @k0nze_builds
    @k0nze_builds  2 роки тому +2

    Get your FREE Python Cheat Sheets: k0nze.gumroad.com

  • @amitkoushik5504
    @amitkoushik5504 Рік тому +11

    To the point explanation.... always ignore why it's written in python but now you cleared that. Keep making such high quality content ❤

  • @OZTutoh
    @OZTutoh 6 днів тому

    As a python beginner in 2024, I say thank you for this tutorial. There are so many wack articles out there trying to explain 'if __name__ == "__main__":'.
    I rewound the video a few times but I understand it now.

  • @ilshatabdulmanov2140
    @ilshatabdulmanov2140 Рік тому +4

    I've been struggling with this notion of __name__ == "__main__"
    for a long time.
    Thanks a lot for a clear and concise explanation !

  • @kools67
    @kools67 Рік тому +2

    great video K0nze!! - by showing us what happens with the two codes running after the import, you made me understand and why you need a 'guard' to block off other lines of codes running in the imported module - well done!!

  • @ekarata.361
    @ekarata.361 Рік тому +2

    After watching multiple videos explaining this concept, I finally understand it from you. Thanks!

  • @prakashgadupoodi7549
    @prakashgadupoodi7549 Рік тому

    Clean, Clear and Crisp. Million thanks for the great video!

  • @tw9535
    @tw9535 11 місяців тому

    I have watched several videos on this subject. Yours is the very best.
    Thank you!

  • @femi2873
    @femi2873 Рік тому

    thanks, i watched other videos on this topic, but your video was the best and i was able to clearly understand the concept you were explaining

  • @moyz2469
    @moyz2469 2 роки тому +2

    Awesome video K0nze! Excellent explanation!

  • @fidi7400
    @fidi7400 10 місяців тому

    Thanks so much for this video, I spent hours trying to understand the main method from other resources and could not get it until now!

  • @apple_vlogs10
    @apple_vlogs10 Рік тому

    before this video i went through 3-4 tutorials to understand this point , it got cleared in your video
    great explanation !!

  • @andrecruzrodrigues5157
    @andrecruzrodrigues5157 Рік тому

    best explanation ever after lots of videos. GJ bro! thanks 🖥

  • @iamdedlok
    @iamdedlok 2 роки тому

    🖥watched till the end :) haha thanks for the nice explanation!

  • @clintwestwood4545
    @clintwestwood4545 2 роки тому +5

    Only video I understood on this matter. You have the skills of a true teacher. Also, the scenery is lovely. Nice nature background :D

  • @alexbecks5199
    @alexbecks5199 Рік тому

    Straight to the point! Thanks 👍🏽 🖥

  • @jonhanson8925
    @jonhanson8925 11 місяців тому

    Wow! I finally know! Thanks for the explanation! 🖥

  • @shakhzod8583
    @shakhzod8583 Рік тому

    🖥 Wonderful video) Thank you very much !

  • @LexOnTheWeb
    @LexOnTheWeb Рік тому

    🖥️ Thanks for the explanation!

  • @giladharnik2499
    @giladharnik2499 Рік тому

    Great video! Thanks 🖥️

  • @sarkhanmammadli9673
    @sarkhanmammadli9673 Рік тому

    thank you for the clear explanation!

  • @chuckwm
    @chuckwm Рік тому

    🖥Great explanation.

  • @Sherloklol
    @Sherloklol Рік тому

    i can see clearly now... the confusion is gone...ty!

  • @ioannismesionis6639
    @ioannismesionis6639 Рік тому

    Great video! How did you format the terminal showing this arrow-like format for your path?

  • @yoyaaaa4111
    @yoyaaaa4111 Рік тому

    Thank you👍

  • @Ren-io4sx
    @Ren-io4sx Рік тому +1

    only comprehensive explanation out there!

    • @kools67
      @kools67 Рік тому

      it was a very good explanation - especially when it ran both codes after the import - you can see what is clearly happening and why you need a 'guard', as he says...I am soo happy that i understand this now and I can carry on with my python learning : )

  • @STYX1966
    @STYX1966 Рік тому

    🖥Thank's. Now I get it!

  • @matiasmatu_tdf6837
    @matiasmatu_tdf6837 Рік тому

    ty. Excelent

  • @education.online_frevryone
    @education.online_frevryone Рік тому +1

    Understood

  • @runner6994
    @runner6994 Рік тому

    Great explanation. I get it. c💻

  • @kristinamwangekan2471
    @kristinamwangekan2471 11 місяців тому

    Thank you

  • @farukTheProBro
    @farukTheProBro 2 роки тому

    thanks man :)

  • @jossec1344
    @jossec1344 5 місяців тому

    thank you mate

  • @antonywmiller
    @antonywmiller Рік тому

    🖥 thanks

  • @peytonmulder8059
    @peytonmulder8059 3 місяці тому

    I feel silly, I just asked this question on the previous video I watched 🤣 🖥

  • @Medx111
    @Medx111 2 роки тому

    Keep it up

  • @ranjanthapa3964
    @ranjanthapa3964 2 роки тому +1

    well explain, thank you🙂🙂

  • @strade740
    @strade740 8 місяців тому

    💻🖥bro u are great

  • @Marcusram
    @Marcusram Рік тому

    Thanks! I finally understand this disgrace 😅

  • @rajatpathak4499
    @rajatpathak4499 2 місяці тому

    brilliant

  • @emperor8716
    @emperor8716 6 місяців тому

    So basically: code in this block will only run if you run that file directly. explained in 1 sentence.

  • @harispilton5614
    @harispilton5614 4 місяці тому

    Hi, I like to buy your cheat sheets but via Pay Pall. Is this possible?

  • @minhly650
    @minhly650 2 роки тому +1

    So what is the difference between two following code if I just execute them as script alone?
    def greeting():
    print('Hello World!')
    greeting()
    and
    def greeting():
    print('Hello World!')
    if __name__ == "__main__":
    greeting()
    Thanks

    • @kychemclass5850
      @kychemclass5850 2 роки тому

      No difference - if all you're running is that code at once (i.e. from one python file or manually entered at the command line). The file that's initially run and pulls modules/libraries into it is decreed as __main__. so each version of your code will be __main__.

  • @MunirLoyed
    @MunirLoyed Рік тому

    So simply it means don't run imported code unless it is called.

  • @kychemclass5850
    @kychemclass5850 2 роки тому

    WHERE do you add it? At the bottom after functions or at the top? (requires indenting all the time)

    • @kools67
      @kools67 Рік тому +2

      i think you add it to the start of the programme where it needs to begin - so after you have set up your functions or imported any modules add it after those - i hope i am correct : )

    • @kychemclass5850
      @kychemclass5850 Рік тому

      @@kools67 Tq

  • @RaftxoWysocki
    @RaftxoWysocki Рік тому

    Is there a way to avoid executing my_module code without editing it adding if __name__=="__main__" ?
    🖥🖥👍

  • @moyz2469
    @moyz2469 2 роки тому +1

    🖥

  • @a-man-lol
    @a-man-lol 2 роки тому

    good video (computer emoji)

  • @Mooodf
    @Mooodf Рік тому

    bester mann

  • @TilenKow
    @TilenKow Рік тому

    💻

  • @LifeOf97
    @LifeOf97 Рік тому

    🖥️

  • @stevenjoyner903
    @stevenjoyner903 Рік тому

    STEVEN BRIAN JOYNER

  • @DIGmbl
    @DIGmbl 5 місяців тому

    Why not "dunder"? Instead of "underscore, underscore"?
    At least it is faster.

  • @eisbar2polar246
    @eisbar2polar246 11 місяців тому

    🙃

  • @nutinmyass
    @nutinmyass 6 місяців тому

    In my case I want code in the module to run when it’s imported, so I don’t use this.
    Is that valid, or does that mean I’m making a poor design choice somewhere?

    • @k0nze_builds
      @k0nze_builds  6 місяців тому

      If your module is not runnable standalone, you don't need to add it.

  • @si0p
    @si0p Рік тому

    I have one question if the developer doesnot use if __name__=='__main__' statement while making my_module.py then either you add if __name__=='__main__' in main_script.py or not add , it will not work as you said.
    So developer must have to use the above statement so that we can use module and got expected resutl.
    So, again question is that if developer has already used the above statement in my_module.py then why should we use that statement again in main_script.py. As we know using it in main_script.py doesnot make any sense. CORRECT ME IF I AM WRONG because your example didnot completely made the sense.
    ----------------my_module.py-------------------
    def abc(a,b):
    return a+b
    print('hello')
    ---------------main_script.py--------------
    import my_module
    if __name__=='__main__' :
    print(test.abc(5,4))
    NOTE: I think we actually add that statement in main_script.py because we make our own function before starting if __name__ == '__main__' statement in main_script.py. So I think we do so inorder to use that main_script.py as module in future.

  • @VenkateshGowdSomasamudram
    @VenkateshGowdSomasamudram 10 місяців тому

    🖥

  • @education.online_frevryone
    @education.online_frevryone Рік тому

    💻

  • @breemudi
    @breemudi Рік тому

    🖥

  • @lemonade1960
    @lemonade1960 Рік тому

    🖥

  • @sherwinantony3512
    @sherwinantony3512 Рік тому

    🖥

  • @jakubwilgocki319
    @jakubwilgocki319 Рік тому

    🖥