1 00:00:00,510 --> 00:00:01,110 Welcome back. 2 00:00:01,380 --> 00:00:06,760 We just learned about module's, but let's say our project is getting even bigger. 3 00:00:07,560 --> 00:00:08,820 We have this main file. 4 00:00:09,060 --> 00:00:10,320 We have some utility. 5 00:00:10,470 --> 00:00:20,460 But let's say that we're now working for Microsoft and we have a new, let's say, shopping functionality, 6 00:00:20,460 --> 00:00:25,150 an entire, let's say, shopping area where you can buy Microsoft T-shirts. 7 00:00:25,890 --> 00:00:30,990 Well, we can now create a folder so we can add folders to our project. 8 00:00:31,620 --> 00:00:40,980 And inside of this folder, let's say we have a new file and we'll call this the shopping. 9 00:00:47,260 --> 00:00:49,180 So now this shopping cart pie. 10 00:00:50,450 --> 00:00:56,600 Is a folder in our project, you see that our project is getting bigger and bigger, and let's say that 11 00:00:56,600 --> 00:01:05,750 here we just have a function called by and we give it an item and let's say here we we have a carte 12 00:01:05,780 --> 00:01:07,010 variable that. 13 00:01:08,110 --> 00:01:14,830 Let's say adds, it's not a very sophisticated buying system, but for our example, it should be fine 14 00:01:14,830 --> 00:01:17,300 and then we just return the cart. 15 00:01:18,890 --> 00:01:30,170 Now, in here we have this by function and let's say in our main file, so up here we want to also import 16 00:01:30,500 --> 00:01:33,080 our shopping cart by function. 17 00:01:33,620 --> 00:01:34,460 How can we do that? 18 00:01:35,860 --> 00:01:38,080 Well, if we tried to do shopping. 19 00:01:39,700 --> 00:01:43,210 Cart like this, remember, that's the name. 20 00:01:45,960 --> 00:01:49,650 You see that I get an underlining says imported but unused. 21 00:01:50,040 --> 00:01:51,970 OK, so does that mean I can use it? 22 00:01:51,990 --> 00:01:54,210 Let's see, say, shopping cart. 23 00:01:54,420 --> 00:01:55,410 And if I click, run. 24 00:01:57,930 --> 00:02:04,740 Module not found error, no module named shopping cart, you see, what we've created here is what we 25 00:02:04,740 --> 00:02:06,210 call a package. 26 00:02:07,640 --> 00:02:16,820 A package is simply a folder, and we learned about module, which is these files, these Python files, 27 00:02:16,980 --> 00:02:26,390 a package is a level up, a package is a folder containing modules so you can have a package with multiple 28 00:02:26,390 --> 00:02:27,720 modules inside of them. 29 00:02:28,220 --> 00:02:34,400 So this shopping cart is a module, but this shopping is a package. 30 00:02:35,350 --> 00:02:41,270 So the way it works when we import from a package is we say the package name. 31 00:02:41,440 --> 00:02:43,030 So what's the package name? 32 00:02:44,400 --> 00:02:48,540 It's shopping, and then we do a dot. 33 00:02:49,920 --> 00:02:51,390 So dot shopping cart. 34 00:02:52,730 --> 00:02:57,200 So package name and then the module name. 35 00:02:58,530 --> 00:03:03,870 Now, if we do this, let's see if this works, let's say I do shopping. 36 00:03:04,830 --> 00:03:06,150 Doctor shopping cart now. 37 00:03:07,130 --> 00:03:09,530 If I print this, let's make this a little bit smaller. 38 00:03:12,250 --> 00:03:19,630 All right, now I have access to the shopping cart module by importing the package shopping, which 39 00:03:19,630 --> 00:03:25,600 is the folder and then the shopping cart, and you notice once again we have a paid cash that was created. 40 00:03:31,100 --> 00:03:37,580 So let's see if we can use the buy function, so I'm going to say buy and then item, I'll say I'm buying 41 00:03:38,450 --> 00:03:39,200 just an apple. 42 00:03:40,210 --> 00:03:40,990 If I click, run. 43 00:03:42,890 --> 00:03:43,410 There you go. 44 00:03:43,460 --> 00:03:47,660 I get my Apple list and I was able to import it from a package. 45 00:03:49,450 --> 00:03:56,440 Now, here's the thing, I want to try and copy what we've just done into a real life scenario, instead 46 00:03:56,440 --> 00:03:58,890 of using this website, replug it. 47 00:03:58,900 --> 00:04:00,870 Let's actually create our own project. 48 00:04:01,180 --> 00:04:04,620 I'm going to use Peja and I recommend you use pajamas. 49 00:04:04,630 --> 00:04:07,180 Well, just for these next couple of videos. 50 00:04:08,990 --> 00:04:13,220 Let's open up Iran and let's just create a new project here. 51 00:04:15,350 --> 00:04:20,750 Now we can call it whatever we want, so let's just call it module's for now for our project, he can 52 00:04:20,750 --> 00:04:21,950 see where it's being saved. 53 00:04:22,280 --> 00:04:26,150 And remember, here we get to pick what interpreter we want. 54 00:04:26,810 --> 00:04:30,260 We're going to talk about this virtual env later on in the videos. 55 00:04:30,440 --> 00:04:36,740 But the key thing is to make sure that our base interpreter is using Python three, in my case, the 56 00:04:36,740 --> 00:04:40,820 Python three that we've downloaded and use our local bin. 57 00:04:41,100 --> 00:04:45,290 If you're on a Mac or Linux, if you're on Windows, this path will probably be different. 58 00:04:46,700 --> 00:04:49,130 Now we can just create a project. 59 00:04:53,020 --> 00:04:54,970 And we see here we have our module's. 60 00:04:56,080 --> 00:05:01,260 Project now we're going to ignore all these folders for now, we're going to talk about them later on. 61 00:05:01,750 --> 00:05:04,910 The key here is that we want to create a new file. 62 00:05:05,740 --> 00:05:08,560 I'm going just right click here. 63 00:05:14,310 --> 00:05:18,110 And we can just call it, let's say, made dot by. 64 00:05:19,970 --> 00:05:23,940 And notice here that I created in a folder called V and V. 65 00:05:24,140 --> 00:05:25,550 We'll get back to that shortly. 66 00:05:26,270 --> 00:05:30,650 Now, let's make this a little bit bigger so we can see and we're going to copy whatever we've just 67 00:05:30,650 --> 00:05:31,450 done here. 68 00:05:32,270 --> 00:05:33,920 So I'm going to copy. 69 00:05:36,670 --> 00:05:40,420 The main file like this that did not copy. 70 00:05:40,450 --> 00:05:41,220 Let's try that again. 71 00:05:45,000 --> 00:05:50,640 Like that, and then we're also going to create a few more files, so we'll create a folder. 72 00:05:53,000 --> 00:05:56,030 So in here, I'm just going to say, right click say new. 73 00:05:57,510 --> 00:06:04,140 And right away, you see that it python package, I'm going to click on Python package or I can create 74 00:06:04,140 --> 00:06:05,870 a director if I want as well. 75 00:06:07,960 --> 00:06:12,610 And when I click here, let's name the package like we've done here as shopping. 76 00:06:14,870 --> 00:06:17,930 All right, we get our shopping folder. 77 00:06:19,590 --> 00:06:26,770 And in here, let's right click, say, new new file, and we will call this the shopping cart, DCPI 78 00:06:26,940 --> 00:06:27,780 So shopping. 79 00:06:29,020 --> 00:06:30,870 Cart that by. 80 00:06:33,060 --> 00:06:35,700 And then finally, we'll add our utility file. 81 00:06:35,940 --> 00:06:39,660 I know we're doing a lot, but, hey, I'm trying to prove a point here. 82 00:06:39,690 --> 00:06:41,260 Trust me, this is going to be important. 83 00:06:42,030 --> 00:06:43,440 So utility pie. 84 00:06:44,250 --> 00:06:44,770 There we go. 85 00:06:45,090 --> 00:06:47,280 So we have the same setup that we have here. 86 00:06:47,640 --> 00:06:49,040 Let me copy this over as well. 87 00:06:52,180 --> 00:06:54,400 All right, and we copy. 88 00:06:55,450 --> 00:06:56,890 Let's make this bigger like that. 89 00:06:57,980 --> 00:07:04,880 All right, so we have our files just like we have in our Reppel, except this time we're actually using 90 00:07:04,880 --> 00:07:05,750 an ID. 91 00:07:06,590 --> 00:07:13,430 Now, the reason I wanted to transfer over to the ID is that there's an extra file here that we didn't 92 00:07:13,430 --> 00:07:14,750 see in the Reppel. 93 00:07:15,200 --> 00:07:18,570 And the Reppel actually hides this away from us. 94 00:07:19,400 --> 00:07:24,290 You see this underscore in it DCPI file and it's completely empty. 95 00:07:24,320 --> 00:07:25,510 There's nothing in here. 96 00:07:26,210 --> 00:07:34,040 One of the rules of a package of a Python package is that on the root of this package, you have to 97 00:07:34,040 --> 00:07:38,000 have an underscore, underscore in it, underscore, underscore DCPI. 98 00:07:38,930 --> 00:07:40,530 Now, why is that? 99 00:07:41,450 --> 00:07:46,610 Well, because the interpreter is going to read this and say, oh, this is a python package. 100 00:07:47,450 --> 00:07:48,700 Remember how when I right. 101 00:07:48,710 --> 00:07:55,490 Click here, I said new and then I had an option of a directory or a python package by clicking Python 102 00:07:55,490 --> 00:07:58,500 package, it automatically added this in it for us. 103 00:07:59,090 --> 00:08:06,590 So although here everything worked without a net, this is just something that Reppel hides underneath 104 00:08:06,590 --> 00:08:06,950 the hood. 105 00:08:07,310 --> 00:08:13,700 Any time you want to create a package, you'll see that we need to have an init file and it can just 106 00:08:13,700 --> 00:08:14,690 be completely empty. 107 00:08:15,740 --> 00:08:20,750 Now, the beauty here with pie charts is that it does it for you, which is really, really nice. 108 00:08:21,710 --> 00:08:24,770 OK, so let's run this code and make sure that it still works. 109 00:08:25,310 --> 00:08:27,310 So this is our main pie. 110 00:08:28,160 --> 00:08:33,380 I can simply run the code here by going to run and then selecting run. 111 00:08:34,620 --> 00:08:35,970 Let's run our main file. 112 00:08:38,140 --> 00:08:40,720 And we get in there, let's zoom in here. 113 00:08:43,710 --> 00:08:51,210 And we see that model shopping, the shopping cart has no attribute buy, and that is because we did 114 00:08:51,210 --> 00:08:53,030 not import the buy function here. 115 00:08:53,070 --> 00:08:54,350 So let's do that. 116 00:08:54,390 --> 00:08:56,910 Let's go to shopping cart by. 117 00:08:57,760 --> 00:08:58,870 Make sure I copy that. 118 00:09:00,420 --> 00:09:01,140 Paste it in. 119 00:09:03,450 --> 00:09:07,030 Let's zoom that in and by the way, I'm zooming here on my touchpad. 120 00:09:07,050 --> 00:09:09,060 I can just use two fingers to zoom in here. 121 00:09:10,060 --> 00:09:14,350 If I click run again, and this time once you ran it once, you can just click on Run Again. 122 00:09:15,720 --> 00:09:23,310 And there we have it, we have Apple right here as the output and it says process finished with zip 123 00:09:23,310 --> 00:09:24,840 code zero in programming. 124 00:09:24,840 --> 00:09:28,650 When something exits with code zero, it means there were no errors. 125 00:09:28,650 --> 00:09:30,690 If you get one, well, that's usually an error. 126 00:09:32,340 --> 00:09:41,580 All right, so we have everything working and this is now are set up, we have our main file, we have 127 00:09:41,580 --> 00:09:47,640 our other modules like utility, and then we also have our packages and their packages because they 128 00:09:47,640 --> 00:09:50,400 have this added file and we have different modules in them. 129 00:09:51,640 --> 00:09:58,390 And as projects get bigger and bigger and we use IDs like petroleum, we organize our functionality, 130 00:09:58,780 --> 00:10:06,430 our grouping into these packages and modules and really, really good developers are able to organize 131 00:10:06,430 --> 00:10:10,120 code in really nice packages. 132 00:10:10,120 --> 00:10:10,870 That makes sense. 133 00:10:10,870 --> 00:10:12,700 Really nice modules that make sense. 134 00:10:13,750 --> 00:10:16,180 This is a skill that is really hard to teach. 135 00:10:16,420 --> 00:10:21,990 It just comes with practice by looking at other people's projects, by working on projects. 136 00:10:22,360 --> 00:10:26,110 But the key thing is that you want to do what makes sense. 137 00:10:26,800 --> 00:10:32,350 And when we start working on some of our final projects, we have over 10 projects coming up that we're 138 00:10:32,350 --> 00:10:33,010 going to work on. 139 00:10:33,220 --> 00:10:38,400 Now with those ones, you're going to start to learn different ways to organize code as you follow along. 140 00:10:38,410 --> 00:10:39,550 So don't worry. 141 00:10:40,810 --> 00:10:44,740 Let's take a break and explore module's a little bit more in the next video.