Python Tutorial: if __name__ == '__main__'

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys how's it going today I want to take a look at something that you're probably going to 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 when 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 at 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 dot 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 dot 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 that'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 dot 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 the imported first module but only printed out second modules name is 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 its 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 in the 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 to the second module dot PI and I import first module and run this you can see it says run from import because it hit 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 the 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 it 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
Info
Channel: Corey Schafer
Views: 1,223,541
Rating: 4.9383602 out of 5
Keywords: Python, Programming, Computer Science, Video Tutorial, Tutorial, How to, How-to, Software, Software Engineering, Development, Python (Programming Language), name, main, Python Tutorial, Python Tutorials
Id: sugvnHA7ElY
Channel Id: undefined
Length: 8min 43sec (523 seconds)
Published: Sun Mar 22 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.