Python Tutorial: if __name__ == ‘__main__’

hey guys how’s it going today I want to take a look at something that you’re probably gonna see a lot in Python and that is this line right here if double underscore name is equal to double underscore main so what is this doing and why do people use it well let’s go ahead and comment this code out and just right off the bat let’s print out this double underscore name variable so if I save this code and run it you can see that I just ran this first module dot PI and what I got whenever I printed out the double underscore name was double underscore main and actually to make this more clear I’m going to go ahead and put in that this is the first modules name variable and let me do a format and pass in that name variable now whenever I save that and run it now you can see down here first modules name and then we have double underscore main so what exactly is going on here well whenever python runs a file it first goes through before it even runs any code it sets a few special variables and name is one of those special variables and when python runs a python file directly it sets this name variable equal to main and that’s what we’re doing here we’re running this file we’re running this code from the file first module pi now we can also import modules now whenever we import a module it’s going to set this named variable to the name of the file so in this case its first underscore module so let’s go ahead and see what this looks like so I’m going to go over here to second module PI and I’m going to import first module and save that and whenever you import a file it runs that code and so it’s going to come in here and it’s going to print out this line and whenever we ran this line from first module PI we got first modules name is double underscore main whenever we run second module that PI and import that module now you can see that it says first modules name is first module and the reason it’s doing that is because this file is no longer being run directly by Python it’s being imported and we can actually take a look at second module dot PI’s let me copy this from first module and let me paste this into second module dot PI change this to second so now I have second modules name and let me run this so now you can see that it’s importing first module and first modules name from this file is first module because it being imported second modules name is main because python is running this file directly and now this brings us back to the code that we first started with so now I can uncomment out this code and now we can see why something like this would be useful because we can check if double underscore name is equal to main which pretty much is saying is this file being run directly by Python or is it being imported so let me bring this down and cut this out and I’m going to paste this into this main method here save that so now if I run this file first module that PI I’m running it directly so I just ran it and you can see that it went through and it said if double underscore name is equal to main run this main method came into the main method and print it out first modules name is main now if I go back here to second module dot PI and I run this code you can see imported first module but only printed out second modules name has double underscore main now the reason that it didn’t print first modules name is because now we have this check in place if double underscore name is equal to main now this whenever it rent came through and ran this code it’s saw that double underscore name was equal to first module and not main so then it never made it to this main method and it never printed out this statement here and just to illustrate this further I can go into first module here I can type in and an else condition and then let me take out I’m just going to take out this main method and from here I’m going to do a print statement that says run directly and then in the else I’m gonna say run from import save that and whenever I run this file first module dot PI whenever I run it directly you can see that this file was the main method got run directly I come over here now the second module dot PI and I import first module and run this you can see it says run from import because it hits this else statement and you can see that it’s being imported so hopefully that clears up any confusion that you may have had about this statement here if name is equal to main the reason this is used is because you know sometimes there is code that you only want to run whenever you’re running this as the main file and sometimes there’s code that you only want to run whenever it’s being imported and one more thing here if we bring back this main method and then I’m gonna put in a print statement here I’m just going to put first modules main method and save that and then run that within here and then also up above this I’m going to put in a few print statements that is just a C print this will be run this will always be run so save that and then whenever I run this you can see this will always be run first modules main method one reason that you might want to put all of your code inside of method like this instead of just putting all the code that you want to run down here in this conditional if you make this method and then within this conditional where it says if double underscore name is equal to main and then run this what that allows us to do is if we come over here to second module dot PI and let me rerun this now you can see that an imported first module and it says this will always be run and that’s because it is outside of that main method so it’s going to get run regardless even if it’s important and then second modules name is main now with that main method if we wanted to run that from second module dot PI then we can actually come in here and call that directly so I can save that and now if I rerun this file now you can see that it’s importing first module and it’s not meeting this condition so it doesn’t run the main method on the import but then if we want to run that anyway then we can say first module dot main then it comes in here prints that out and then continues on with the rest of the code in second module dot PI so I really hope this video was useful and explained this well enough if you have any further questions you know feel free to ask in the comment section below you know be sure to subscribe for future videos and thank you for watching

100 thoughts on “Python Tutorial: if __name__ == ‘__main__’”

  1. Worked with a dozen languages but never Python. Conditional execution of code based on pragmas or precompiler directives is sometimes necessary but always fraught. Aren’t you bound to come unstuck one day? An imported library of code which could potentially run differently depending on how it’s invoked sounds like a debugging nightmare.

  2. Wonderful explanation. As everyone has said, this is one of the best explanations of this topic.

    One thing to add though. I know many here are well versed in Python will know this, but for those new to Python here is an example of why you would employ the 'if _name_ == "__main__" in a module. When you build a module, you will want to test it and make sure it works to spec. The beauty of it is that you never have to remove it and it gives you an easy way to test out your module before importing it into other Python files.

  3. This could of been explained alot easier, not sure why you had to persist with main first name, module main method, etc

  4. I understood this part because of you that
    _name_ is set equal to _main_ only when the code Is run directly
    If you are importing first module from second module then variable _name_ will not get set equal to _main_ for first module because first module Is not being run directly.

    Thanks man I'm very happy that I understood this, god bless you.

  5. Coming up from a C++ and Java world I use the "if _name_ == '__main__':" functionality almost religiously, even though most of the time I dont need it.

  6. What I’m wondering is why doesn’t Python just add a simple _main_ variable that is set to true iff __name__==‘__main__’?
    I see that this is a well-established idiom in Python, but it’s pretty clunky, and it makes it feel like it’s not an intended use case.

  7. Thanks Corey this was wonderful!! Just a question when I can use this? Any example from any of your practical programs?

  8. This would have much better if there were concrete examples that use this, not just some simple print statements. This video took 8 minutes to not really describe anything useful.

  9. Although this type of explanation best of best, we should not keep feel excited all the time from his video. Gotto to carry on the evolution in good <<<<<<<<ecological niche>>>>>>>>>>

  10. "__doubleUnderbar__" can be contracted verbally to "dunder variables" (double-underbar). Save some time in the presentation. I'm not making this up – I heard it somewhere and I use it all the time.

  11. If there's one thing that gets on my nerves in programming tutorials is that little tiny piece on the left missing so you can't see the first two characters

  12. Hi Corey. I am enjoying your lessons. How can I get Column names of a table from Sql Server 2008 in Python3.7?

  13. I wish this tute was around when I started in Python! Yes, IMO many newbies trying to import a module they have written get surprised that the import runs it and are desperately trying to find out how to stop it! And get surprised how silly the work around is. If it had been me, I would have added a Python command for Importing without running. This _name_ business is ugly.

  14. For modules that are normally imported, it seems like a good use for this would be to run unit tests from main() when the module is run directly. Does anyone do that?

  15. This is a great start!

    What situations/context would you use this statement?

    And you mentioned when importing a file?

    Still a bit fuzzy on what context to use this statement.

    Thanks in advance for responding.!

  16. Hi Corey,

    Could you please try to make a video on multiprocessing and multithreading concepts and how to use them.

  17. When I found this out at first I couldn't see the usefulness of it but I realized that I could test my modules separately and not have it called within my main program.

    Great explanation as always.

  18. its used to not execute the module instantly after you import them . thats the main purpose then. why such a simple thing has to be so complicated to explain?

  19. Great explanation! I completed a graduate program in data science and none of the professors explained this as clearly.

  20. I'm learning Python on my own and haven't reached the underscored 'thingies', but just watching this video, I understood the whole concept!! Thumbs up!! thx

  21. Wonderful explanation!

    So it looks like this is like setting private members for a module in a project where you have multiple modules. In OOP, as you have private variables to a class similarly in a Project, you have private data (variables/functions) which you want to hide from other modules that import this module. Therefore, this construct allows you to do it the way Python handles Private variables in it's OOP constructs.
    Sorry to the reader if these statements sound repetitive though. 😛

  22. You rock. I'm so lucky, I looked down out of confusion while watching a tutorial because this was used (but I had no clue what it was).. my eyes just happened to see this video suggested, noice

  23. If this file is not imported run this batch of code . If it is imported run whatever else is there is to run. That summarizes it, I think …

  24. The absolute clear and concise explanation I've seen on this in 5 years off and on of playing and working with Python. Well done!

  25. The concept is very simple but can anyone actually give a real world example of why this is useful? Maybe I am unimaginative but I just don't see what the point of this is? 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *