1 00:00:00,510 --> 00:00:01,380 Lecturer: Welcome back. 2 00:00:01,380 --> 00:00:03,660 We just learned about modules 3 00:00:03,660 --> 00:00:07,079 but let's say our project is getting even bigger. 4 00:00:07,079 --> 00:00:10,500 We have this main file you, we have some utility 5 00:00:10,500 --> 00:00:14,406 but let's say that we're now working for Microsoft 6 00:00:14,406 --> 00:00:17,855 and we have a new let's say, 7 00:00:17,855 --> 00:00:21,930 shopping functionality, an entire, let's say 8 00:00:21,930 --> 00:00:25,890 shopping area where you can buy Microsoft t-shirts. 9 00:00:25,890 --> 00:00:28,320 Well, we can now create a folder 10 00:00:28,320 --> 00:00:31,650 so we can add folders to our project. 11 00:00:31,650 --> 00:00:36,650 And inside of this folder, let's say we have a new file 12 00:00:37,631 --> 00:00:42,631 and we'll call this the shopping cart dot py. 13 00:00:47,250 --> 00:00:49,270 So now this shopping cart dot py 14 00:00:50,460 --> 00:00:52,823 is a folder in our project, 15 00:00:52,823 --> 00:00:55,770 you see that our project is getting bigger and bigger. 16 00:00:55,770 --> 00:01:00,360 And let's say that here we just have a function called buy 17 00:01:00,360 --> 00:01:02,490 and we give it an item. 18 00:01:02,490 --> 00:01:04,414 And let's say here we 19 00:01:04,414 --> 00:01:09,386 we have a cart variable that let's say adds. 20 00:01:09,386 --> 00:01:13,020 It's not a very sophisticated buying system 21 00:01:13,020 --> 00:01:14,790 but for our example, it should be fine. 22 00:01:14,790 --> 00:01:17,463 And then we just return the cart. 23 00:01:18,900 --> 00:01:23,900 Now in here we have this buy function 24 00:01:23,910 --> 00:01:26,915 and let's say in our main file, So up here 25 00:01:26,915 --> 00:01:31,915 we wanna also import our shopping cart buy function. 26 00:01:33,630 --> 00:01:34,593 How can we do that? 27 00:01:35,850 --> 00:01:40,850 Well, if we try to do shopping cart like this 28 00:01:42,060 --> 00:01:45,003 remember that's the name of our file. 29 00:01:45,960 --> 00:01:48,480 You see that I get an underline that says imported, 30 00:01:48,480 --> 00:01:51,990 but unused. Okay, so does that mean I can use it? 31 00:01:51,990 --> 00:01:54,450 Let's see, I'm gonna say shopping cart. 32 00:01:54,450 --> 00:01:55,563 And if I click run. 33 00:01:57,960 --> 00:02:02,100 Module, not found error, no module named Shopping Cart. 34 00:02:02,100 --> 00:02:06,363 You see what we've created here is what we call a package. 35 00:02:07,650 --> 00:02:12,120 A package is simply a folder. 36 00:02:12,120 --> 00:02:13,830 And we learned about module 37 00:02:13,830 --> 00:02:16,980 which is these files, these Python files. 38 00:02:16,980 --> 00:02:19,440 A package is a level up. 39 00:02:19,440 --> 00:02:23,700 A package is a folder containing modules. 40 00:02:23,700 --> 00:02:25,440 So you can have a package 41 00:02:25,440 --> 00:02:28,230 with multiple modules inside of them. 42 00:02:28,230 --> 00:02:30,390 So this shopping cart is a module 43 00:02:30,390 --> 00:02:34,533 but this shopping is a package. 44 00:02:35,400 --> 00:02:37,770 So the way it works when we import 45 00:02:37,770 --> 00:02:41,430 from a package is we say the package name. 46 00:02:41,430 --> 00:02:43,203 So what's the package name? 47 00:02:44,400 --> 00:02:46,920 It's shopping. 48 00:02:46,920 --> 00:02:48,633 And then we do a dot. 49 00:02:49,950 --> 00:02:51,483 So dot shopping cart. 50 00:02:52,740 --> 00:02:57,333 So package name, and then the module name. 51 00:02:58,530 --> 00:03:01,140 Now, if we do this, let's see if this works. 52 00:03:01,140 --> 00:03:05,940 Let's say I do shopping dot shopping cart. 53 00:03:05,940 --> 00:03:09,723 Now, if I print this, let's make this a little bit smaller. 54 00:03:12,330 --> 00:03:13,500 All right? 55 00:03:13,500 --> 00:03:17,580 Now I have access to the shopping cart module 56 00:03:17,580 --> 00:03:20,400 by importing the package shopping, which is the folder 57 00:03:20,400 --> 00:03:21,780 and then the shopping cart. 58 00:03:21,780 --> 00:03:23,310 And you notice once again 59 00:03:23,310 --> 00:03:28,310 we have a pycache that was created for the shopping cart. 60 00:03:31,080 --> 00:03:33,930 So let's see if we can use the buy function. 61 00:03:33,930 --> 00:03:36,180 So I'm going to say buy and then item. 62 00:03:36,180 --> 00:03:39,303 I'll say, I'm buying just an apple. 63 00:03:40,140 --> 00:03:43,470 If I click run, there you go. 64 00:03:43,470 --> 00:03:44,940 I get my apple list 65 00:03:44,940 --> 00:03:47,793 and I was able to import it from a package. 66 00:03:49,470 --> 00:03:50,700 Now here's the thing. 67 00:03:50,700 --> 00:03:54,270 I wanna try and copy what we've just done 68 00:03:54,270 --> 00:03:56,070 into a real life scenario. 69 00:03:56,070 --> 00:03:58,860 Instead of using this website repl dot it 70 00:03:58,860 --> 00:04:01,230 let's actually create our own project. 71 00:04:01,230 --> 00:04:02,850 I'm going to use PyCharm 72 00:04:02,850 --> 00:04:05,520 and I recommend you use PyCharm as well. 73 00:04:05,520 --> 00:04:09,660 Just for these next couple of videos, let's open 74 00:04:09,660 --> 00:04:13,353 up PyCharm and let's just create a new project here. 75 00:04:15,390 --> 00:04:17,220 Now we can call it whatever we want. 76 00:04:17,220 --> 00:04:20,519 So let's just call it modules for now for our project. 77 00:04:20,519 --> 00:04:22,290 You can see where it's being saved. 78 00:04:22,290 --> 00:04:26,820 And remember here we get to pick what interpreter we want. 79 00:04:26,820 --> 00:04:29,610 We're gonna talk about this virtualenv later on 80 00:04:29,610 --> 00:04:30,450 in the videos. 81 00:04:30,450 --> 00:04:32,402 But the key thing is to make sure that our base 82 00:04:32,402 --> 00:04:35,910 interpreter is using Python three. 83 00:04:35,910 --> 00:04:40,170 In my case, the Python three that we've downloaded in user 84 00:04:40,170 --> 00:04:43,050 local bin, If you're on a Mac or Linux, if you're 85 00:04:43,050 --> 00:04:45,500 on Windows, this path will probably be different. 86 00:04:46,710 --> 00:04:49,263 Now we can just create a project, 87 00:04:53,040 --> 00:04:57,210 and we see here we have our modules project. 88 00:04:57,210 --> 00:04:59,730 Now we're going to ignore all these folders for now, 89 00:04:59,730 --> 00:05:01,740 we're gonna talk about them later on. 90 00:05:01,740 --> 00:05:05,820 The key here is that we wanna create a new file. 91 00:05:05,820 --> 00:05:10,820 I'm going to just right click here and say New Python File. 92 00:05:14,340 --> 00:05:18,213 And we can just call it, let's say main dot py. 93 00:05:20,010 --> 00:05:24,150 And notice here that I created in a folder called V E N V. 94 00:05:24,150 --> 00:05:26,280 We'll get back to that shortly. 95 00:05:26,280 --> 00:05:28,860 Now let's make this a little bit bigger so we can see 96 00:05:28,860 --> 00:05:32,280 and we're going to copy whatever we've just done here. 97 00:05:32,280 --> 00:05:34,000 So I'm going to copy 98 00:05:36,660 --> 00:05:39,390 the main file like this. 99 00:05:39,390 --> 00:05:40,410 Oh, that did not copy. 100 00:05:40,410 --> 00:05:41,460 Let's try that again. 101 00:05:45,030 --> 00:05:46,080 Like that. 102 00:05:46,080 --> 00:05:48,630 And then we're also going to create a few more files. 103 00:05:48,630 --> 00:05:50,793 So we'll create a folder. 104 00:05:53,010 --> 00:05:56,193 So in here, I'm just going to say, right click, say New. 105 00:05:57,510 --> 00:06:01,290 And right away you see that it says Python Package. 106 00:06:01,290 --> 00:06:03,420 I'm going to click on Python Package 107 00:06:03,420 --> 00:06:06,033 or I can create a Directory if I want as well. 108 00:06:08,010 --> 00:06:09,030 And when I click here 109 00:06:09,030 --> 00:06:12,693 let's name the package like we've done here as shopping. 110 00:06:14,910 --> 00:06:18,123 All right, we get our shopping folder. 111 00:06:19,731 --> 00:06:23,280 And in here, let's right click, say New, new File. 112 00:06:23,280 --> 00:06:26,940 And we'll, we'll call this the shopping cart dot py. 113 00:06:26,940 --> 00:06:30,963 So shopping cart dot py. 114 00:06:33,090 --> 00:06:36,000 And then finally we'll add our utility file. 115 00:06:36,000 --> 00:06:38,310 I know we're doing a lot, but hey. 116 00:06:38,310 --> 00:06:39,660 I'm trying to prove a point here. 117 00:06:39,660 --> 00:06:42,030 Trust me, this is gonna be important. 118 00:06:42,030 --> 00:06:44,250 So utility dot py. 119 00:06:44,250 --> 00:06:45,090 There we go. 120 00:06:45,090 --> 00:06:47,640 So we have the same setup that we have here. 121 00:06:47,640 --> 00:06:49,203 Let me copy this over as well. 122 00:06:52,230 --> 00:06:53,460 All right. 123 00:06:53,460 --> 00:06:58,460 And we copy, Let's make this bigger like that, right? 124 00:06:58,500 --> 00:07:01,140 So we have our files, just like we have 125 00:07:01,140 --> 00:07:05,433 in our repl except this time we're actually using an IDE. 126 00:07:06,570 --> 00:07:09,120 Now, the reason I wanted to transfer over 127 00:07:09,120 --> 00:07:12,570 to the IDE is that there's an extra file here 128 00:07:12,570 --> 00:07:15,240 that we didn't see in the repl. 129 00:07:15,240 --> 00:07:19,380 And the repl actually hides this away from us. 130 00:07:19,380 --> 00:07:22,650 You see this underscore init dot py file, 131 00:07:22,650 --> 00:07:24,300 and it's completely empty. 132 00:07:24,300 --> 00:07:26,070 There's nothing in here. 133 00:07:26,070 --> 00:07:28,733 One of the rules of a package 134 00:07:28,733 --> 00:07:33,270 of a Python package is that on the root of this package 135 00:07:33,270 --> 00:07:35,470 you have to have an underscore underscore 136 00:07:35,470 --> 00:07:38,103 init underscore underscore dot py. 137 00:07:38,940 --> 00:07:41,460 Now, why is that? 138 00:07:41,460 --> 00:07:43,800 Well, because the interpreter is going to read this 139 00:07:43,800 --> 00:07:47,028 and say, Oh, this is a Python Package. 140 00:07:47,028 --> 00:07:50,220 Remember how when I right clicked here, I said New 141 00:07:50,220 --> 00:07:54,480 and then I had an option of a Directory or a Python Package? 142 00:07:54,480 --> 00:07:55,920 By clicking Python Package 143 00:07:55,920 --> 00:07:59,100 it automatically added this init for us. 144 00:07:59,100 --> 00:08:04,100 So although here everything worked without a init 145 00:08:04,200 --> 00:08:07,380 this is just something that repl hides underneath the hood. 146 00:08:07,380 --> 00:08:09,231 Anytime you want to create a package 147 00:08:09,231 --> 00:08:11,400 you'll see that we need to have an 148 00:08:11,400 --> 00:08:14,853 init file and it can just be completely empty. 149 00:08:15,720 --> 00:08:18,030 Now the beauty here with PyCharm is 150 00:08:18,030 --> 00:08:21,720 that it does it for you, which is really, really nice. 151 00:08:21,720 --> 00:08:23,310 Okay, so let's run this code 152 00:08:23,310 --> 00:08:25,290 and make sure that it still works. 153 00:08:25,290 --> 00:08:28,200 So this is our main dot py. 154 00:08:28,200 --> 00:08:29,880 I can simply run the code here 155 00:08:29,880 --> 00:08:33,513 by going to Run and then selecting Run. 156 00:08:34,620 --> 00:08:39,150 Let's run our main file, Whoop, and we get an error. 157 00:08:39,150 --> 00:08:40,923 Let's zoom in here. 158 00:08:43,740 --> 00:08:46,830 And we see that module shopping dot shopping cart 159 00:08:46,830 --> 00:08:49,556 has no attribute buy, and that is 160 00:08:49,556 --> 00:08:53,070 because we did not import the buy function here. 161 00:08:53,070 --> 00:08:54,360 So let's do that. 162 00:08:54,360 --> 00:08:56,490 Let's go to shopping cart. 163 00:08:56,490 --> 00:09:01,293 Buy, make sure I copy that, paste it in. 164 00:09:03,480 --> 00:09:04,440 Let's zoom that in. 165 00:09:04,440 --> 00:09:07,050 And by the way, I'm zooming here on my touchpad. 166 00:09:07,050 --> 00:09:09,213 I can just use two fingers to zoom in here. 167 00:09:10,080 --> 00:09:12,660 If I click Run again, and this time once you ran it once 168 00:09:12,660 --> 00:09:14,493 you can just click on Run again. 169 00:09:15,780 --> 00:09:16,890 And there we have it. 170 00:09:16,890 --> 00:09:21,330 We have apple right here as the output. 171 00:09:21,330 --> 00:09:24,030 And it says, Process finished with exit code zero. 172 00:09:24,030 --> 00:09:26,700 In programming, when something exits with code zero 173 00:09:26,700 --> 00:09:28,620 it means there were no errors. 174 00:09:28,620 --> 00:09:30,920 If you get one, well, that's usually an error. 175 00:09:32,370 --> 00:09:34,500 All right, so we have everything working 176 00:09:34,500 --> 00:09:37,173 and this is now our setup. 177 00:09:38,040 --> 00:09:42,660 We have our main file, we have our other modules 178 00:09:42,660 --> 00:09:46,320 like utility, and then we also have our packages. 179 00:09:46,320 --> 00:09:48,750 And they're packages because they have this init file 180 00:09:48,750 --> 00:09:50,650 and we have different modules in them. 181 00:09:51,630 --> 00:09:54,120 And as projects get bigger and bigger 182 00:09:54,120 --> 00:09:58,830 and we use IDEs like PyCharm, we organize our functionality, 183 00:09:58,830 --> 00:10:03,180 our grouping into these packages and modules. 184 00:10:03,180 --> 00:10:07,290 And really, really good developers are able to organize code 185 00:10:07,290 --> 00:10:10,860 in really nice packages that make sense? 186 00:10:10,860 --> 00:10:13,650 Really nice modules that make sense. 187 00:10:13,650 --> 00:10:16,380 This is a skill that is really hard to teach. 188 00:10:16,380 --> 00:10:19,320 It just comes with practice by looking 189 00:10:19,320 --> 00:10:22,350 at other people's projects, by working on projects. 190 00:10:22,350 --> 00:10:26,445 But the key thing is that you wanna do what makes sense. 191 00:10:26,445 --> 00:10:29,798 And when we start working on some of our final projects 192 00:10:29,798 --> 00:10:33,240 we have over 10 projects coming up that we're gonna work on. 193 00:10:33,240 --> 00:10:35,550 Now with those ones, you're gonna start to 194 00:10:35,550 --> 00:10:38,400 learn different ways to organize code as you follow along. 195 00:10:38,400 --> 00:10:39,663 So don't worry. 196 00:10:40,800 --> 00:10:41,633 Let's take a break 197 00:10:41,633 --> 00:10:44,853 and explore modules a little bit more in the next video.