1
00:00:05,410 --> 00:00:11,470
Welcome back, everyone, to this lecture on modules and packages when creating complex code bases,

2
00:00:11,680 --> 00:00:14,950
we typically don't just use a single dot pie file instead.

3
00:00:15,130 --> 00:00:20,740
We would like to break up functionality across multiple python files and import our classes or functions

4
00:00:20,950 --> 00:00:21,670
as needed.

5
00:00:22,150 --> 00:00:24,940
Let's explore how to easily do this within Python.

6
00:00:25,180 --> 00:00:26,830
I'm going to jump back to our code editor.

7
00:00:27,430 --> 00:00:33,010
OK, so here I am inside a visual studio code, and I've expanded the explorer because I'm first going

8
00:00:33,010 --> 00:00:36,910
to start off by creating two new files one I'm going to call.

9
00:00:38,050 --> 00:00:40,360
My module, Dot Pi.

10
00:00:41,050 --> 00:00:44,020
And let's do two things inside my module.

11
00:00:44,080 --> 00:00:50,800
I'll define a function that let's say is super useful, like the use or, let's say, useful.

12
00:00:52,510 --> 00:01:00,250
Function and all this does is this kind of really silly function, just says print using the useful

13
00:01:00,640 --> 00:01:02,080
underscore funk.

14
00:01:02,800 --> 00:01:03,070
All right.

15
00:01:03,340 --> 00:01:04,760
So I have that available for me.

16
00:01:04,780 --> 00:01:06,790
It's a function within my module, that pie.

17
00:01:07,270 --> 00:01:11,550
I will also later on in this file, define a class.

18
00:01:12,100 --> 00:01:14,260
We'll call it useful class.

19
00:01:15,190 --> 00:01:17,770
And let's just say it has a very simple method.

20
00:01:18,520 --> 00:01:25,420
You just instantiate it, let's say in it, and you instantiate it with some message you want to relay

21
00:01:25,420 --> 00:01:25,840
later.

22
00:01:25,940 --> 00:01:33,820
So say salt, that message is equal to message, and then it's going to have a method called, let's

23
00:01:33,820 --> 00:01:35,320
say, report.

24
00:01:36,600 --> 00:01:41,820
Takes and self and report just prints out the message itself, says self that.

25
00:01:42,900 --> 00:01:43,440
Message.

26
00:01:44,070 --> 00:01:44,430
OK.

27
00:01:44,940 --> 00:01:49,770
So notice two things I have here, I have a function that is really useful to me and I want to use it.

28
00:01:49,830 --> 00:01:54,870
It's in my module that PI, as well as a class with a method I can call.

29
00:01:55,470 --> 00:02:02,280
So now we want to be able to use both this function and this class, as well as the methods associated

30
00:02:02,280 --> 00:02:04,170
with that class in another file.

31
00:02:04,290 --> 00:02:04,920
How do we do that?

32
00:02:05,520 --> 00:02:11,460
I'm going to create a new file and now let's call it my program Dot Pi.

33
00:02:12,030 --> 00:02:18,420
And so my program that PI wants to be able to use functionality from my module that PI notice right

34
00:02:18,420 --> 00:02:22,560
now that they're actually located within the same folder called Django Lectures.

35
00:02:22,860 --> 00:02:27,240
Later on, we're going to show you how to create essentially a package directory that you can then use

36
00:02:27,240 --> 00:02:29,040
that an import from other locations.

37
00:02:29,880 --> 00:02:35,130
But what I can do for right now, because they're located in the same directory that is, they're both

38
00:02:35,130 --> 00:02:36,540
located under Django lectures.

39
00:02:36,840 --> 00:02:38,730
I can say the following I can say from.

40
00:02:40,290 --> 00:02:41,190
My module.

41
00:02:42,460 --> 00:02:47,170
Import and then I can actually import the function useful function.

42
00:02:47,710 --> 00:02:50,860
So I say import useful func.

43
00:02:52,040 --> 00:02:57,080
And then here, later on in my code, I can actually then execute useful funk.

44
00:02:58,130 --> 00:03:00,980
So now look what I'm going to do down here at the terminal.

45
00:03:01,130 --> 00:03:02,180
I'm going to say.

46
00:03:03,540 --> 00:03:11,100
Python and I'm going to run my program, the PI, I hit enter and it says using the useful function.

47
00:03:11,520 --> 00:03:19,590
So now I understand that I can actually call functions here in my module, that pi from my program,

48
00:03:19,590 --> 00:03:20,070
that pi.

49
00:03:20,610 --> 00:03:26,520
Note that when you start doing imports, you may actually see Python automatically cash some of this

50
00:03:26,520 --> 00:03:29,490
functionality that way when you run this again in the future.

51
00:03:29,610 --> 00:03:34,530
It should, technically speaking, run faster for our level of understanding of Python, though it can

52
00:03:34,530 --> 00:03:35,490
kind of just ignore it.

53
00:03:35,520 --> 00:03:37,620
Don't delete it, but just go ahead and let it live there.

54
00:03:38,190 --> 00:03:44,220
Now we can also use classes, so instead of importing use func, what I could also do is say from my

55
00:03:44,220 --> 00:03:44,760
module.

56
00:03:46,440 --> 00:03:54,630
Imports and I can import the useful class, and the way this would work is I create my instance of a

57
00:03:54,630 --> 00:03:58,380
class so I can say my instance.

58
00:04:01,150 --> 00:04:03,850
Is equal to an instance, a useful class.

59
00:04:04,120 --> 00:04:07,540
Remember, it needs a message to be defined, so I'll just have this be.

60
00:04:08,170 --> 00:04:08,590
Hello.

61
00:04:09,910 --> 00:04:14,020
And then I'm going to say my instance and then I have a excu report.

62
00:04:14,950 --> 00:04:15,940
Save those changes.

63
00:04:16,930 --> 00:04:19,209
Run that and then it says hello.

64
00:04:19,810 --> 00:04:26,110
So that's how you can actually go from a file that's located in the same directory, like my module,

65
00:04:26,110 --> 00:04:30,970
that pie and then say from my module that is the name of that pie file import.

66
00:04:30,990 --> 00:04:34,090
And you can either import functions or classes or both.

67
00:04:34,270 --> 00:04:39,130
If you want to do multiple imports, you could do something like this comma and then say useful func

68
00:04:39,280 --> 00:04:40,150
and also use that.

69
00:04:40,540 --> 00:04:46,570
So then I could over here actually execute useful func and you can see that works pretty much the exact

70
00:04:46,570 --> 00:04:46,930
same way.

71
00:04:46,960 --> 00:04:47,350
Hello.

72
00:04:47,350 --> 00:04:48,520
And then using that useful func.

73
00:04:49,030 --> 00:04:51,640
Now let's imagine I have another folder.

74
00:04:51,910 --> 00:04:53,020
How would I actually use that?

75
00:04:53,650 --> 00:04:59,770
Well, what I can do is I'm going to create a new folder and we're going to call it my.

76
00:05:01,190 --> 00:05:08,330
Package note that I'm creating another folder, I'm not actually creating another Python file yet wanting

77
00:05:08,330 --> 00:05:10,910
to do in order to use my package.

78
00:05:11,300 --> 00:05:16,640
And let Python know that this directory name, which is technically not a Python file, can also be

79
00:05:16,640 --> 00:05:21,590
included as part of the pathway, is two things inside of my package.

80
00:05:22,010 --> 00:05:26,000
I need to create an empty file called Underscore Underscore.

81
00:05:26,270 --> 00:05:31,070
I end it underscore underscore that PI.

82
00:05:31,880 --> 00:05:37,730
This is going to let Python know that it's OK to use this folder name as part of its directory structure.

83
00:05:37,730 --> 00:05:42,860
When you're importing a file, it's actually labeling everything inside this folder as part of a package.

84
00:05:43,340 --> 00:05:49,970
And now let's actually create a new file here that will be called, let's say, my sub module Dot Pi

85
00:05:50,900 --> 00:05:57,020
and inside my sub module that PI, I will go ahead and create another kind of silly function here.

86
00:05:57,530 --> 00:05:58,280
So we'll say.

87
00:05:59,480 --> 00:06:08,420
My sub funk and all this does is it prints out using a function from.

88
00:06:09,480 --> 00:06:10,830
My sub module, that pie.

89
00:06:12,860 --> 00:06:18,920
OK, so notice the directory structure here, I have my package in order for Python to understand that

90
00:06:18,920 --> 00:06:24,920
this is now a package director I can use, I now have in it and then my sub module, so I come back

91
00:06:24,920 --> 00:06:26,030
to my program.

92
00:06:26,480 --> 00:06:29,420
And then what I can do because it hasn't in at that pie.

93
00:06:29,930 --> 00:06:40,610
I can now say from my package, dot my sub module, import some functionality from that particular file.

94
00:06:41,120 --> 00:06:44,090
So I come here and now that name is my sub func.

95
00:06:44,570 --> 00:06:48,080
So I come here and say, Import my sub func.

96
00:06:48,560 --> 00:06:49,670
So notice the syntax here.

97
00:06:49,670 --> 00:06:56,380
I now say from that director name dot the the actual Python file name import.

98
00:06:56,390 --> 00:06:58,220
Then the class and the function.

99
00:06:58,970 --> 00:07:01,040
Previously, we just did from the module.

100
00:07:01,040 --> 00:07:03,980
Import this now because I have this in it, that pie file.

101
00:07:03,980 --> 00:07:04,880
That's to underscore.

102
00:07:04,950 --> 00:07:08,300
Remember that I can begin to use the directory names themselves.

103
00:07:09,140 --> 00:07:12,080
So here then I can call my sub function.

104
00:07:12,380 --> 00:07:13,460
Have it run an execute.

105
00:07:13,760 --> 00:07:15,860
I'm going to run Python, my program, the pie again.

106
00:07:16,250 --> 00:07:19,310
And now it says, here, hey, I'm using a function for my sub module.

107
00:07:19,310 --> 00:07:19,820
That pie?

108
00:07:20,540 --> 00:07:20,870
OK.

109
00:07:21,260 --> 00:07:27,800
And that's the very basics of what we need to know for modules, imports and packages.

110
00:07:28,190 --> 00:07:31,280
There's technically a lot more that goes into it than what you've seen here.

111
00:07:31,490 --> 00:07:36,440
But just to get us started with Django, this is more than enough later on as we do various other types

112
00:07:36,440 --> 00:07:37,050
of imports.

113
00:07:37,070 --> 00:07:41,180
We'll let you know as we're doing them the specific context and syntax for that.

114
00:07:41,510 --> 00:07:43,280
But this is what you need to know so far.

115
00:07:43,580 --> 00:07:49,670
If the two Python files are in the same directory, you can easily just import them by saying from the

116
00:07:49,670 --> 00:07:53,810
module that pie import whatever classes or functions you're looking for.

117
00:07:54,110 --> 00:07:59,030
If you want them organized, then their directory, which you can begin to do, is simply open up that

118
00:07:59,030 --> 00:07:59,540
folder.

119
00:07:59,870 --> 00:08:06,380
Add in in in it that pie that is empty and then add in the modules like myself module and then say from

120
00:08:06,380 --> 00:08:08,690
that directory that and then the module.

121
00:08:08,990 --> 00:08:10,880
So that's a difference in a package and a module.

122
00:08:11,090 --> 00:08:16,130
The package is that actual directory, which should then include different types of modules, was themselves

123
00:08:16,130 --> 00:08:17,060
are just Python files.

124
00:08:17,630 --> 00:08:18,350
OK, that's it.

125
00:08:18,530 --> 00:08:19,460
I'll see in the next lecture.

