1 00:00:00,270 --> 00:00:06,210 Let's learn about different ways that we can actually import these modules, up until now, we've only 2 00:00:06,210 --> 00:00:11,340 learned about import, but sometimes things can get pretty hectic. 3 00:00:11,820 --> 00:00:16,350 For example, we can actually have another package inside of another package. 4 00:00:17,200 --> 00:00:24,390 If, for example, I create a new folder here and say new python package, we'll call this more. 5 00:00:25,390 --> 00:00:32,920 Shopping click, OK, you see that I've initiated another package inside of a package that has its own 6 00:00:33,070 --> 00:00:33,790 in it file. 7 00:00:34,920 --> 00:00:44,610 And let's move this now to let's do refractor move and we'll move it inside of the more shopping folder, 8 00:00:44,610 --> 00:00:45,930 shall say more. 9 00:00:48,810 --> 00:00:55,780 I click, OK, you'll see that I've moved this module over here, so I have a package inside of a package. 10 00:00:56,950 --> 00:01:02,740 And the way we would access this is to say simply shopping dot more. 11 00:01:04,300 --> 00:01:06,340 Shopping dot, shopping cart. 12 00:01:07,420 --> 00:01:10,210 And let's copy that over and do the same here. 13 00:01:12,290 --> 00:01:13,520 And now when I click run. 14 00:01:14,520 --> 00:01:20,310 You'll see that it still works, but this is getting a little bit too crazy. 15 00:01:21,230 --> 00:01:26,900 You can imagine as we have more and more packages that we constantly have to do this dot, this dot, 16 00:01:26,900 --> 00:01:28,460 and this is getting a little. 17 00:01:29,520 --> 00:01:31,290 Too long and ugly. 18 00:01:32,010 --> 00:01:33,360 So how can we better this? 19 00:01:35,250 --> 00:01:41,160 Well, one way of doing this in a cleaner fashion is to do something like this instead of saying import, 20 00:01:41,160 --> 00:01:43,410 we say from this. 21 00:01:44,940 --> 00:01:53,580 Part, I want to import the function, so here I'm saying, hey, from package, name, package, name, 22 00:01:54,360 --> 00:01:58,050 module, import, whatever function I want. 23 00:01:58,050 --> 00:02:00,570 In our case, we want to import the buy function. 24 00:02:01,440 --> 00:02:04,440 So you see here, I can just do that and in my code. 25 00:02:06,210 --> 00:02:10,560 I can just run by as if it was part of this main file. 26 00:02:12,260 --> 00:02:13,370 If I run this. 27 00:02:14,960 --> 00:02:16,910 You'll see that it still works. 28 00:02:17,990 --> 00:02:23,100 Awesome, so that's definitely cleaner and we can do this with utility as well. 29 00:02:23,360 --> 00:02:31,030 I can say from utility import and actually import the two files or the two functions. 30 00:02:31,340 --> 00:02:32,920 So that is multiply. 31 00:02:33,140 --> 00:02:37,220 And if we want another function, we can just add a comma and say divide. 32 00:02:38,770 --> 00:02:41,170 So now we can print Apple. 33 00:02:42,580 --> 00:02:44,950 Or by Apple as well as divide. 34 00:02:47,270 --> 00:02:49,220 Let's say five by two. 35 00:02:50,190 --> 00:02:52,140 And we can also multiply. 36 00:02:54,970 --> 00:03:01,600 And make sure it's a comma here, because it is a function like this, so that if I run this, you'll 37 00:03:01,600 --> 00:03:02,200 see that. 38 00:03:03,190 --> 00:03:03,740 There we go. 39 00:03:04,150 --> 00:03:05,030 This works as well. 40 00:03:05,380 --> 00:03:08,260 So this is a really nice way to clean up your coat. 41 00:03:09,360 --> 00:03:14,970 You can also do something like this where you say from package, name, package, name. 42 00:03:16,100 --> 00:03:23,690 And then import the entire module, so let's say I want to import the shopping cart module, I can say 43 00:03:23,690 --> 00:03:30,410 import, so package dot package or you could have just had have done one package and import the actual 44 00:03:30,410 --> 00:03:31,640 module shopping cart. 45 00:03:32,630 --> 00:03:37,130 So now I would access it like this shopping cart dot by. 46 00:03:38,790 --> 00:03:40,790 And this is going to work as well. 47 00:03:45,220 --> 00:03:51,730 Now, the reason you might want to do this instead of what we had before, is that sometimes you can 48 00:03:51,730 --> 00:03:53,140 get named collisions. 49 00:03:54,190 --> 00:04:02,230 Let me show you, let's say I had a print and then in here we use the max function, which gives us 50 00:04:02,800 --> 00:04:09,850 it's a built in function, we give it an iterator and it finds the max for us so that if I run this, 51 00:04:10,540 --> 00:04:13,690 you'll see that the max in this list is three. 52 00:04:15,020 --> 00:04:24,290 Well, imagine in our utility function, we have a let's say instead of divide or we add a max that 53 00:04:24,290 --> 00:04:25,640 simply says. 54 00:04:26,730 --> 00:04:30,030 Returns and oops. 55 00:04:31,210 --> 00:04:33,670 Well, if I go back to my main DCPI. 56 00:04:36,830 --> 00:04:40,070 Get Max as my function and I click run. 57 00:04:42,530 --> 00:04:43,790 Well, you see, I get an there. 58 00:04:44,810 --> 00:04:51,950 I get Max takes zero positional arguments, but was given, but one was given because now Max doesn't 59 00:04:51,950 --> 00:04:57,980 mean what we thought we meant, at least in this file we've imported Max and well. 60 00:04:59,130 --> 00:05:05,820 Max has a whole different function now, so we can actually override existing functions, so by using 61 00:05:05,820 --> 00:05:12,780 something like this shopping cart, we make sure that we have only one case that we might have named 62 00:05:12,780 --> 00:05:13,260 collision. 63 00:05:13,710 --> 00:05:21,340 But all the properties or all the methods that shopping art has are connected to a shopping cart. 64 00:05:21,540 --> 00:05:27,720 So instead of having multiple functions that could be overridden, I only have one namespace. 65 00:05:28,860 --> 00:05:33,960 And by the way, this is also one of the reasons that I don't recommend doing something like this, 66 00:05:33,990 --> 00:05:35,970 you can actually do import star. 67 00:05:36,690 --> 00:05:41,420 And what this says is, hey, from the utility module, import everything. 68 00:05:41,940 --> 00:05:42,990 And when you do that. 69 00:05:44,440 --> 00:05:50,860 You'll see that once again we get an air because it imports the max as well, and because we don't even 70 00:05:50,860 --> 00:05:53,320 know what's being imported right now, we can't see it. 71 00:05:53,350 --> 00:05:54,940 We have to go to the file to see it. 72 00:05:55,360 --> 00:05:58,330 When we get an air like this, we're going to be like, what's going on? 73 00:05:58,330 --> 00:06:00,820 I thought, Max is working and it's because of this line. 74 00:06:00,970 --> 00:06:05,440 So it's always good to be explicit and say exactly what you want to import. 75 00:06:06,430 --> 00:06:11,890 But let's remove Max for now, but as you can see, modules can be important in different ways. 76 00:06:11,980 --> 00:06:17,830 My recommendation is to always be explicit and say exactly what you want to import. 77 00:06:18,100 --> 00:06:27,160 And the format usually follows either using the import statement followed by the package module or import 78 00:06:27,160 --> 00:06:35,650 the module, or you can do the from statement to give it package, support package and module if you 79 00:06:35,650 --> 00:06:42,700 want, or you can do a module and then saying import and the specific function or the actual module 80 00:06:42,700 --> 00:06:45,760 itself, many ways of doing it depending on your need. 81 00:06:46,240 --> 00:06:53,920 The key takeaway here is that modules and packages help us have good engineering practices and build 82 00:06:53,920 --> 00:06:59,440 big projects in an organized fashion, yet still have all these files talk to each other. 83 00:07:00,410 --> 00:07:03,200 Right, that was a lot let's learn more in the next video.