1 00:00:00,000 --> 00:00:02,300 Let's talk about modules. 2 00:00:02,300 --> 00:00:05,600 So in Python, there's these two similar but kind of different 3 00:00:05,600 --> 00:00:09,700 concepts. There's this one called a module, and there's one called a package. 4 00:00:09,700 --> 00:00:12,500 In the last few lessons, we were talking about packages. 5 00:00:12,500 --> 00:00:14,700 So now let's talk about modules. 6 00:00:14,700 --> 00:00:19,100 So a module is really just a different '.py' file. 7 00:00:19,160 --> 00:00:23,040 So if you've got another '.py' file, and you're importing anything 8 00:00:23,040 --> 00:00:24,600 from it, that's called a module. 9 00:00:25,700 --> 00:00:28,900 And a package is usually a whole system of files. 10 00:00:28,930 --> 00:00:31,620 It's a whole bunch of modules, or a whole bunch of Python 11 00:00:31,630 --> 00:00:32,799 files put together, 12 00:00:32,810 --> 00:00:35,800 and you can usually install a package with 'pip install', and 13 00:00:35,800 --> 00:00:39,400 then the package name, 'packagename'. 14 00:00:40,000 --> 00:00:43,300 But a module can simply be one file or a few files. 15 00:00:43,360 --> 00:00:47,350 And the idea behind a module is really just to separate your 16 00:00:47,360 --> 00:00:51,490 code into smaller files instead of having one massive Python 17 00:00:51,490 --> 00:00:55,400 file. So we've seen some code in our Jupyter Notebook, and 18 00:00:55,400 --> 00:00:58,000 that code really didn't get too long. 19 00:00:59,000 --> 00:01:02,800 But in real life, you might end up having a file that has 20 00:01:03,080 --> 00:01:06,680 a 1000, 2000, 14000 lines of code in it. 21 00:01:06,690 --> 00:01:09,080 And if you ever see something like that, that is a really 22 00:01:09,080 --> 00:01:12,300 good candidate for pulling out some of that code and putting it 23 00:01:12,300 --> 00:01:13,300 into another file. 24 00:01:13,300 --> 00:01:16,300 So let's go ahead and make a little example here. 25 00:01:16,320 --> 00:01:19,050 I'm going to create a new file, and I'm going to save this 26 00:01:19,050 --> 00:01:21,100 as 'custom_module.py', 27 00:01:21,100 --> 00:01:22,800 and I'm going to put that up just on my Desktop here, 28 00:01:24,200 --> 00:01:27,100 'custom_module.py'. 29 00:01:27,160 --> 00:01:29,080 And I'm going to create a new function. 30 00:01:29,080 --> 00:01:32,200 So 'def my_greeting():', I guess. 31 00:01:32,200 --> 00:01:39,600 And it's just going to say, 'print("Hello from custom_module. 32 00:01:39,600 --> 00:01:42,100 [no audio] 33 00:01:42,100 --> 00:01:44,100 Now I'm going to create another file, but this one is 34 00:01:44,100 --> 00:01:48,000 going to be called 'my_program.py'. 'my_program.py'. 35 00:01:49,000 --> 00:01:52,400 And let's go ahead and oops, there is no folder in there. 36 00:01:52,400 --> 00:01:54,500 So let's hide that. 37 00:01:55,500 --> 00:01:57,300 And let's go ahead and reopen that other file. 38 00:01:57,370 --> 00:02:00,360 So we have 'custom_module' and 'my_program.py' in here. 39 00:02:00,360 --> 00:02:04,800 And let's label these as well, '#custom module', 40 00:02:04,800 --> 00:02:09,800 and this one is, I'll delete that, '#my program'. 41 00:02:10,038 --> 00:02:13,039 So because these live side by side, what I can do is import 42 00:02:13,039 --> 00:02:14,000 one from the other. 43 00:02:14,000 --> 00:02:19,400 So in my program, I can say 'from custom_module import', 44 00:02:19,480 --> 00:02:23,580 and then I can import this particular function, and then I can 45 00:02:23,590 --> 00:02:24,600 run it in here. 46 00:02:24,630 --> 00:02:26,780 'my_greeting()', run. 47 00:02:26,780 --> 00:02:30,900 Now let's go into our terminal here, and I'm going to 'cd' 48 00:02:30,900 --> 00:02:33,200 into my Desktop, because that's where my files are. 49 00:02:33,200 --> 00:02:38,500 And if I do 'ls -la', we'll see we've got 'my_program' from a previous 50 00:02:38,710 --> 00:02:41,330 video, we also have 'custom_module', and 'my_program'. 51 00:02:41,340 --> 00:02:44,010 So let's go ahead and run 'my_program'. 'python 52 00:02:44,010 --> 00:02:49,900 my_program.py', and it says, "Hello from custom_module.py". 53 00:02:49,900 --> 00:02:53,200 So 'my_program' actually doesn't have a 'print' statement in here at all. 54 00:02:53,200 --> 00:02:57,130 What it's saying is from the 'custom_module' import this particular 55 00:02:57,130 --> 00:02:59,700 function. Look at that, 'custom_module.py' 56 00:02:59,700 --> 00:03:02,500 actually has this function, and it ran the 'print' statement. 57 00:03:02,520 --> 00:03:06,420 And that's all there is to making a module. It's really, you're just 58 00:03:06,430 --> 00:03:09,860 moving one bit of Python to another file. 59 00:03:10,800 --> 00:03:13,400 Now, I would like you to give this a shot. Create two Python 60 00:03:13,410 --> 00:03:17,450 files, and import a function from one of those, and run it 61 00:03:17,450 --> 00:03:21,800 in a different file. So you can even copy what I have, 'my_program.py', 62 00:03:21,800 --> 00:03:23,300 and 'custom_module.py', 63 00:03:23,300 --> 00:03:25,800 and you can just have 'my_greeting' in there. 64 00:03:25,800 --> 00:03:28,200 Go ahead and 'import my_greeting', and then execute it. 65 00:03:28,200 --> 00:03:30,700 And again, there's no 'print' statement in here. 66 00:03:30,700 --> 00:03:33,100 It is running the code from this file. 67 00:03:33,100 --> 00:03:35,700 It's just moving it into here, and executing it. 68 00:03:35,760 --> 00:03:37,800 Go ahead and give that a shot. 69 00:03:37,810 --> 00:03:40,350 And once you are done with that, let's head on over to the 70 00:03:40,350 --> 00:03:43,100 next lesson where we are going to create a package.