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
Get your FREE Python Cheat Sheets: k0nze.gumroad.com
To the point explanation.... always ignore why it's written in python but now you cleared that. Keep making such high quality content ❤
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.
I've been struggling with this notion of __name__ == "__main__"
for a long time.
Thanks a lot for a clear and concise explanation !
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!!
After watching multiple videos explaining this concept, I finally understand it from you. Thanks!
Clean, Clear and Crisp. Million thanks for the great video!
I have watched several videos on this subject. Yours is the very best.
Thank you!
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
Awesome video K0nze! Excellent explanation!
Thank you Mo!
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!
before this video i went through 3-4 tutorials to understand this point , it got cleared in your video
great explanation !!
best explanation ever after lots of videos. GJ bro! thanks 🖥
🖥watched till the end :) haha thanks for the nice explanation!
Only video I understood on this matter. You have the skills of a true teacher. Also, the scenery is lovely. Nice nature background :D
Thank you so much 😀
wsp mark hhhh
Straight to the point! Thanks 👍🏽 🖥
Wow! I finally know! Thanks for the explanation! 🖥
🖥 Wonderful video) Thank you very much !
🖥️ Thanks for the explanation!
Great video! Thanks 🖥️
thank you for the clear explanation!
🖥Great explanation.
i can see clearly now... the confusion is gone...ty!
Great video! How did you format the terminal showing this arrow-like format for your path?
Thank you👍
only comprehensive explanation out there!
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 : )
🖥Thank's. Now I get it!
ty. Excelent
Understood
Great explanation. I get it. c💻
Thank you
thanks man :)
thank you mate
🖥 thanks
I feel silly, I just asked this question on the previous video I watched 🤣 🖥
Keep it up
well explain, thank you🙂🙂
You're welcome 😊
💻🖥bro u are great
Thanks! I finally understand this disgrace 😅
brilliant
So basically: code in this block will only run if you run that file directly. explained in 1 sentence.
Hi, I like to buy your cheat sheets but via Pay Pall. Is this possible?
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
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__.
So simply it means don't run imported code unless it is called.
WHERE do you add it? At the bottom after functions or at the top? (requires indenting all the time)
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 : )
@@kools67 Tq
Is there a way to avoid executing my_module code without editing it adding if __name__=="__main__" ?
🖥🖥👍
🖥
good video (computer emoji)
bester mann
💻
🖥️
STEVEN BRIAN JOYNER
Why not "dunder"? Instead of "underscore, underscore"?
At least it is faster.
🙃
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?
If your module is not runnable standalone, you don't need to add it.
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.
🖥
💻
🖥
🖥
🖥
🖥