Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Modules and Packages
Lecture: What is __main__ and when do you use it?

Login or purchase this course to watch this video and the rest of the course contents.
0:01 One entirely non-obvious convention that you quickly learn when you get into Python, is this __main__ concept,
0:08 for controlling when you should execute your code as a program and when your code should behave as a library or a module.
0:15 Let's look at this simple example. So here we have two modules. We have the one that is suppose to run as a program, over here,
0:26 and we have another one which defines a method, a variable and a class that we are going to use,
0:30 and you can see we are importing this or redefining it to something reasonable and we are saying things like "the variable we got out of s",
0:38 so s.method/class/variable, there they are, so the variable we've got out of here is whatever, when we run,
0:46 let's comment this out for just a second, if I go and run this,
0:52 you can see it printed out this code it said the variable value is such and such, great, the variable value is a variable.
0:59 So it looks like we imported this successfully, we worked with some of its data, we could use methods and classes and you know, everything.
1:05 But what is this deal down here, well, this is something you are going to see often in Python
1:12 and you don't have to call this method "main", you can call it "run", call it whatever, but basically we are saying: "Define this function up here
1:18 and in the case where __name__ is main, run this." We'll just print out __name__ really quick, so we'll print this.
1:28 We run it, OK it is __main__, let's go over here, this one, and we'll say print to the same thing, print __name__.
1:36 So the name of this is this great long support module thing, so we are going to run it, well,
1:42 first of all, when I import this, it should trigger this to go. So the name is, this full name here "chapter 6 packages, blah blah blah",
1:50 not a nice name, it wasn't really built to be reused, it was built to be descriptive about what part of the class it fits in, but watch this,
1:57 if I go and run this, let's make this a little more obvious, let's say- so here we have the support library name
2:04 and let's put this as main app name and we'll run this first, so here the support library name is what you expect, main app name is __main__,
2:14 that's not what it says up here but we'll talk about why that is, but if I run the support library, its name is now main.
2:21 So here is the convention: If the thing that is being executed directly, the module or the script that has been executed directly
2:28 regardless of what its real name is, it's called __main__. Everything that is imported up here like so,
2:34 when it's not the target but it's just being imported, it's given its module name, basically the name without the py extension.
2:40 So, what we do in Python is we use this to only conditionally execute code, suppose over here and this support library we had something like this,
2:49 we said, we wanted to ask a question, "while True", we'll say "age = input("How old are you? ")", like so,
2:57 and then we'll convert that to an int, and let's go over here and say age=0,
3:02 let's say "while age is equal or less than 0", OK? What happens here if I go and run this?
3:08 I would like to see I'm going to import this and just use this variable, so let's try, let's try to run this one.
3:15 OK, so it tells us what the library- wait a minute, why are we getting "How old are you?", let's say 0,0,7, oh,
3:26 so that ran and then our code ran, let's make this a little more obvious,
3:31 let's go up here and say print "About to import support lib", I'll say "done importing", so we try and you can see about import support lib
3:42 and then we are just like stuck here, because when you import a module,
3:45 you are literally just executing it top to bottom, executing this defines a class,
3:49 executing that defines a method, executing that defines a variable, but this, this is not what we are going to run,
3:55 we are just trying to expose our variables and methods and so on, this might be if this was actually the script, so you can see this is a problem.
4:03 7 now we are done importing, now this runs, so we could come down here and we could say, we could use our convention,
4:09 we could say "if __name__ == '__main__'", only in that case, only when this is running as a program do we do this, when we do that,
4:18 this won't run, if we import it. So let's try again, about to import, printed out the name here,
4:25 this is not __main__, done importing library, we've got our variable. But if we go over here and run this one, support library name is now __main__,
4:33 "How old are you?" I'm 7, awesome. So here is what that convention is all about, the "__name__ == '__main__'"
4:43 is all about disambiguating the case where your script is being used as a library, as a module in somebody else's code,
4:49 or it's being executed as the target. This is so common that PyCharm has a shortcut for you, you just type main tab and it expands it up.
4:57 we saw that when we import a module its name is just the name of whatever the file is, so in this case it's going to be support for the top class
5:06 when we import it in the bottom. However, if it were executed, it would have a different name,
5:11 it would be "__name__ == '__main__'" and that allows you to disambiguate the times when your script is being run as a program
5:18 and when it's being reused for its functionality.

Talk Python's Mastodon Michael Kennedy's Mastodon