if __name__ == "__main__" in python explained | Advanced python

What’s up, guys? In this article, I am taking you toward advanced python! If you have no knowledge about python and you want to start coding in python then you can see this video first:

https://youtu.be/qHJjMvHLJdg

Everything is explained in a simple way. Even if you are new to this line, you would understand it.

Now I am assuming that you know the basics of python. So, let's get started and start writing our first python program. Open Pycharm and create a new file in it

Note: Keep in mind that the file name should not match any module name.

Reason: Basically whatever module we install is stored and recognized as folders/files by the computer. If you want to see these folders/modules then follow this path:

C:\Users\User\AppData\Local\Programs\Python\Python38-32\Lib\site-packages

Tip: AppData is a hidden folder so you may have to change the settings to show hidden files.

When you import a module it is supposed to be imported from this place. That’s why we don’t name any file by module name because then it will just import the local file which you made by the module name.

Why do we use if __name__ == "__main__"?

Now if you have made your own module type-of file which consists of a lot of functions. If you import that file and it contains print statements then those lines will also play, now we don’t want that. Which is why we use if __name__==”__main__”. It is not just for print statements, we can put all the lines we don’t need in that ‘if’ block.

Explanation:

Basically what is an if statement? It means if this statement is true then play this code.

So what we are saying is if its value is __main__ then run the code block.

It is like saying:

__name__ = "__main__"
if __name__ == "__main__":
    print("Not imported!")

where __name__ is just like a variable whose value is __main__. If we are using the original file and not importing it then ‘if’ statement is true and the code in that ‘if’ block runs, otherwise it doesn’t. Simple!

Let me show you some easy ways to understand:

See, it is compiling both lines because nothing is imported and if the statement is true. Whereas,

Now it is compiling only print statement and the whole ‘if’ block is not compiled because it is imported.

But what if there were an else block? Let me show you:

In the main file else block is not compiled because ‘if’ statement is true. But what if we import it?

It will then play else block because ‘if’ is false for this file. Interesting, isn’t it?

#tut1a.py file as described in the video

import os
def mostimpfunction():
    print("harry is a coder")

print(__name__)

def main():
    print(os.listdir("/"))
    print("Harry is great and he is the king of US")

if (__name__=="__main__"):
    main()

#tut1b.py file as described in the video

import tut1a
tut1a.mostimpfunction()
tut1a.main()

Comments(10)

root2 1 year, 8 months ago
this is amazing harry bhai
tejaslade 1 year, 7 months ago
.
Urmil 10 months ago
amazing content harry bhai
aadvik996 5 months, 4 weeks ago
hello harry bhai
milindeac 1 year, 8 months ago
i have a query why from tkinter import * is not working in spyder IDE is ther any solution for this
sg330 1 year ago
nh
pritish0101 11 months, 1 week ago
Simply  use VS Cod
pritish0101 11 months, 1 week ago
*e
tejaslade 1 year, 7 months ago
1
tejaslade 1 year, 7 months ago
1
Anirudh 1 year, 6 months ago
Harry Bhai Mujhe Python me thode doubt hi Kay me pooch saktha hu
jeevan 1 year, 5 months ago
demo
RohanDas28 1 year, 4 months ago
Amazing Bhai
harry 1 year, 4 months ago
Thanks bhai!
ParisHacker 1 year, 2 months ago
Ye Rohan
yogeshsss 11 months, 3 weeks ago
hey
ParisHacker 1 year, 2 months ago
<b>Hello Harry Bhai</b>
ParisHacker 1 year, 2 months ago
</pre><b>Hello Harry Bhai</b>
ParisHacker 1 year, 2 months ago
<img src onerror="alert('hello Harry Bhai')">

Resources

  1. File - Download here
  2. File - Download here
  3. File - Download here
  4. File - Download here
  5. File - Download here
  6. File - Download here
  7. File - Download here
  8. File - Download here

Course Announcements

Any Course related announcements will be posted here