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