1 00:00:00,428 --> 00:00:09,359 [MUSIC] 2 00:00:09,359 --> 00:00:13,190 Hi there, my name is Megan and I'm a teacher here at Treehouse. 3 00:00:13,190 --> 00:00:17,170 In this workshop, we're going to be talking about dunder main. 4 00:00:17,170 --> 00:00:21,790 Dunder main is going to become your best friend when you start working with more 5 00:00:21,790 --> 00:00:23,820 than one Python file. 6 00:00:23,820 --> 00:00:26,884 The best way to understand what dunder main is and 7 00:00:26,884 --> 00:00:29,640 why you need it is to jump into the code. 8 00:00:29,640 --> 00:00:30,190 So let's go. 9 00:00:31,310 --> 00:00:33,416 I'm going to create two files here. 10 00:00:33,416 --> 00:00:39,584 Our main file I'm going to call app.py to follow typical conventions. 11 00:00:39,584 --> 00:00:46,460 And the other one I'm just gonna call dm for dunder main, .py, perfect. 12 00:00:46,460 --> 00:00:50,087 Now, typically, when you start needing more than one file for 13 00:00:50,087 --> 00:00:53,237 your Python projects, your app.py file will control 14 00:00:53,237 --> 00:00:57,350 the application while other files hold necessary code. 15 00:00:57,350 --> 00:01:03,367 It helps keep things less cluttered and also makes it a lot easier to find things. 16 00:01:03,367 --> 00:01:05,557 app.py is called a script, 17 00:01:05,557 --> 00:01:10,990 because it is the file we intend to run from the command line. 18 00:01:10,990 --> 00:01:16,576 The dm.py file is called a module, because we will be using the code 19 00:01:16,576 --> 00:01:21,580 written in this file to import it into our main app.py file. 20 00:01:22,680 --> 00:01:27,820 Modules are meant to be imported into other modules or scripts. 21 00:01:29,400 --> 00:01:33,660 Inside both files, I'm going to add a simple print statement. 22 00:01:33,660 --> 00:01:41,305 So let's do print, and I'm just gonna do hello from dm.py, don't forget to save. 23 00:01:41,305 --> 00:01:45,520 And pretty much the same thing in here, 24 00:01:45,520 --> 00:01:49,492 print("hello from app.py"), save. 25 00:01:49,492 --> 00:01:56,164 Now, if I run both individually, down here, 3, I'll do dm first and 26 00:01:56,164 --> 00:02:02,076 I get hello from dm, because that's what we have in that file. 27 00:02:02,076 --> 00:02:08,424 And then if I run app.py, I get hello from app.py. 28 00:02:08,424 --> 00:02:10,786 So it works exactly as we expect. 29 00:02:10,786 --> 00:02:14,890 But these files are meant to be working together, not apart. 30 00:02:14,890 --> 00:02:19,016 So let's import dm into app.py. 31 00:02:19,016 --> 00:02:26,590 So up at the top, import, dm, I'm gonna hit Save. 32 00:02:26,590 --> 00:02:28,227 And now I'm gonna do the same thing again. 33 00:02:28,227 --> 00:02:32,002 I'm gonna run dm.py first, 34 00:02:32,002 --> 00:02:36,384 and I still get hello from dm.py. 35 00:02:36,384 --> 00:02:39,498 Now let's see what happens when I run app.py. 36 00:02:46,361 --> 00:02:50,670 Now you can see I am getting hello from dm.py. 37 00:02:50,670 --> 00:02:53,414 So it's running this file first and 38 00:02:53,414 --> 00:02:57,356 then it's running what is here inside of app.py. 39 00:02:57,356 --> 00:03:01,469 So I'm getting two print statements printed out to the console, 40 00:03:01,469 --> 00:03:04,950 even though I'm only running one file. 41 00:03:04,950 --> 00:03:06,860 This may not seem like a big problem. 42 00:03:06,860 --> 00:03:11,454 But imagine having a huge website or application with like 100 files, and 43 00:03:11,454 --> 00:03:14,563 all of a sudden there's all these print messages and 44 00:03:14,563 --> 00:03:18,430 all sorts of things just taking over your console. 45 00:03:18,430 --> 00:03:20,631 Now, this is where dunder main comes in. 46 00:03:20,631 --> 00:03:22,548 It looks like this, and we're gonna add it to our dm.py. 47 00:03:22,548 --> 00:03:27,768 I'm gonna space it down here a little bit, 48 00:03:27,768 --> 00:03:31,973 and it's gonna go if dunder name, 49 00:03:31,973 --> 00:03:37,339 which just stands for double underscores, 50 00:03:37,339 --> 00:03:41,413 dunder name equals dunder main. 51 00:03:41,413 --> 00:03:44,810 And then let's Tab this so that it's inside of our conditional. 52 00:03:44,810 --> 00:03:46,550 We don't really need that extra line at the top. 53 00:03:48,170 --> 00:03:52,980 Okay, so now that I have that saved, let me run our two files again. 54 00:03:52,980 --> 00:03:57,490 Now, I'm just gonna make this easy, I'm gonna clear the console just so 55 00:03:57,490 --> 00:03:59,173 we can tell pretty easily. 56 00:03:59,173 --> 00:04:01,280 So I'm just gonna use the up arrow to run dm.py again. 57 00:04:02,320 --> 00:04:07,855 And you can see, when I run dm.py, I still get hello from dm.py. 58 00:04:07,855 --> 00:04:09,591 But now let's run app.py. 59 00:04:09,591 --> 00:04:11,128 I want you to pause and think, 60 00:04:11,128 --> 00:04:14,650 what do you think is going to happen when I run this file? 61 00:04:14,650 --> 00:04:19,376 Just a quick thought before I hit the Enter key. 62 00:04:19,376 --> 00:04:20,420 All right, are you ready to see? 63 00:04:22,040 --> 00:04:27,020 Okay, now we are just getting hello from app.py. 64 00:04:27,020 --> 00:04:30,675 So we are still importing our dm.py file, but 65 00:04:30,675 --> 00:04:34,530 we're no longer running this print statement. 66 00:04:35,640 --> 00:04:41,783 It is only running the print statement here inside of app.py. 67 00:04:41,783 --> 00:04:42,731 In the next video, 68 00:04:42,731 --> 00:04:46,298 we're going to dive into dunder main some more to see why this happens. 69 00:04:46,298 --> 00:04:46,970 See you there.