1
00:00:05,350 --> 00:00:09,700
Welcome back everyone in this lecture we're now finally going to discuss how to write your own modules

2
00:00:09,730 --> 00:00:11,580
and packages.

3
00:00:11,810 --> 00:00:15,800
So we understand how to install external packages that people uploaded to pie pie.

4
00:00:15,920 --> 00:00:20,940
Let's explore how to create own modules and packages and to clarify some of the semantics here.

5
00:00:20,970 --> 00:00:23,510
Module's it's really just a PI script.

6
00:00:23,690 --> 00:00:28,850
So it's a fancy way of saying here's a postscript that I'm using in another PI script.

7
00:00:28,850 --> 00:00:33,550
So that act of using it in another PI script allows you to call it a module.

8
00:00:33,560 --> 00:00:38,120
Again this is really just semantics but packages are then a collection of modules.

9
00:00:38,120 --> 00:00:42,830
However there's a key that postscript called underscore underscore in its underscore underscored the

10
00:00:42,860 --> 00:00:48,830
pie that needs to be placed inside of a folder to let Python know that this collection of that postscripts

11
00:00:48,890 --> 00:00:51,510
should be treated as an entire package.

12
00:00:51,530 --> 00:00:54,180
So we're going to show all of this through some examples.

13
00:00:54,200 --> 00:00:58,250
Keep in mind since we're dealing with postscripts here we're not going to be able to show these examples

14
00:00:58,310 --> 00:00:59,300
through the notebook.

15
00:00:59,300 --> 00:01:05,950
Instead we're going to need to do is use subline text editor or any other idea that works without PI

16
00:01:05,960 --> 00:01:09,250
files in order to create the scripts and folders.

17
00:01:09,260 --> 00:01:12,560
Let's get started by opening up Sublime Text Editor.

18
00:01:12,560 --> 00:01:16,220
All right so we're going to start off by creating two files here.

19
00:01:16,280 --> 00:01:22,880
The first one will save it to the desktop will say file new file or we can actually save the don't currently

20
00:01:22,880 --> 00:01:30,610
have save as and on my desktop I'm going to say this as my module not pi.

21
00:01:30,680 --> 00:01:35,900
So this is the module and remember all that module is just the top high script being called in from

22
00:01:35,900 --> 00:01:37,030
another file.

23
00:01:37,460 --> 00:01:42,060
So let's create the other file saying you file and this one we're going to save it.

24
00:01:42,140 --> 00:01:48,860
Also on the desktop in the same location as my module that pie and I will say my program that pi and

25
00:01:48,920 --> 00:01:49,930
save that.

26
00:01:50,320 --> 00:01:56,600
So inside of my module what I'm going to do is just create a very simple function will say F and then

27
00:01:56,600 --> 00:02:09,690
say my func and all this does is it prints out hey I am in my module PI control s to save that.

28
00:02:10,100 --> 00:02:15,510
And then in my program this is another dot PI script and I want to use the code from my module.

29
00:02:15,680 --> 00:02:19,790
So here we can get the idea that if you have a really large script you're not going to put everything

30
00:02:19,790 --> 00:02:21,460
in one giant pie file.

31
00:02:21,470 --> 00:02:27,050
Instead it will be nice to split it up between multiple dot py files which means you split up between

32
00:02:27,050 --> 00:02:31,370
different modules and then later we'll see how he can aggregate bunch of modules to create a package

33
00:02:32,150 --> 00:02:42,800
so far or do in my program say from my module import and then we're going to import my funk and let

34
00:02:42,800 --> 00:02:44,000
me make sure I spell that right.

35
00:02:44,000 --> 00:02:44,230
OK.

36
00:02:44,240 --> 00:02:46,840
My underscore phunk.

37
00:02:47,080 --> 00:02:49,630
And that means I can now call it here.

38
00:02:49,690 --> 00:02:52,710
So I'll say my phunk I'm going to say this.

39
00:02:52,750 --> 00:02:55,510
And let's see if it works by going to our command line.

40
00:02:55,510 --> 00:02:57,140
So here I am now at my command line.

41
00:02:57,160 --> 00:02:59,530
Remember I save these files at my desktop.

42
00:02:59,530 --> 00:03:02,730
So let's see the to the desktop change directory.

43
00:03:03,020 --> 00:03:10,330
And now I should be able to call Python and then call my program that PI enter and I see hey I and my

44
00:03:10,330 --> 00:03:11,620
module pi.

45
00:03:11,830 --> 00:03:17,920
So we were able to successfully call the other function from the other module the py file into my program

46
00:03:17,920 --> 00:03:24,310
that pi and this is the very basic way of how you would actually format the PI scripts in order to not

47
00:03:24,310 --> 00:03:26,740
have everything in a single script.

48
00:03:26,740 --> 00:03:31,510
Now at a certain point even this is going to start to be too much and now you're going to want to aggregate

49
00:03:31,900 --> 00:03:36,940
modules together to form and package and that way you can have different folders for different packages.

50
00:03:36,940 --> 00:03:40,550
So let's show you how to create a package's.

51
00:03:40,690 --> 00:03:47,110
So we're going to come back here to my program that pi in my module top pie and will open up and say

52
00:03:47,170 --> 00:03:54,970
new file and then we're going to save this Save As and here at my desktop I'm going to do a few things.

53
00:03:56,330 --> 00:03:59,540
The first thing I'm going to do is create a new folder here.

54
00:04:01,020 --> 00:04:06,410
And this folder will call it my main packet.

55
00:04:08,480 --> 00:04:15,670
Enter And then inside of this folder we're going to create another new folder and we'll call this sub

56
00:04:15,670 --> 00:04:16,590
package.

57
00:04:16,870 --> 00:04:20,040
So we're going to be creating lots of files and folders here.

58
00:04:20,140 --> 00:04:26,110
So right now we have at my desktop my module up high in my program that pi and I've also created my

59
00:04:26,110 --> 00:04:30,500
main package folder and inside of that I have my sub package.

60
00:04:30,580 --> 00:04:35,110
Now since these are just directories or folders we need to let Python be able to understand that we

61
00:04:35,110 --> 00:04:39,660
want to treat these directories not just as a normal directory but as an actual package.

62
00:04:39,700 --> 00:04:47,250
So to do that inside both my main package and sub package I'm going to have to create a file called

63
00:04:47,310 --> 00:04:50,030
underscore underscore in it.

64
00:04:50,090 --> 00:04:58,770
I NIIT underscore underscore that PI save that and make sure you have this in it py file in your package

65
00:04:59,430 --> 00:05:02,250
now you don't actually need to write anything in this file.

66
00:05:02,280 --> 00:05:07,080
It just needs to be there so that when Python goes searching through these packages it understands that

67
00:05:07,080 --> 00:05:09,960
it's not just the normal directory it's an actual package.

68
00:05:09,990 --> 00:05:13,530
So we're going to create one more files a new file.

69
00:05:13,710 --> 00:05:21,120
Now we're going to say this under some package will create another underscore underscore in it underscore

70
00:05:21,120 --> 00:05:23,400
underscore the PI file.

71
00:05:23,400 --> 00:05:25,600
And again this one is going to be blank as well.

72
00:05:26,440 --> 00:05:28,390
So we have both of these.

73
00:05:28,390 --> 00:05:32,740
Notice here how sublime is telling us which are referring to this one in my main package and this one

74
00:05:32,740 --> 00:05:33,650
in the package.

75
00:05:33,670 --> 00:05:34,600
So we're going to close these.

76
00:05:34,600 --> 00:05:36,320
We're not going to be working with them anymore.

77
00:05:36,490 --> 00:05:38,900
We just need to have them name there.

78
00:05:39,390 --> 00:05:39,860
OK.

79
00:05:39,880 --> 00:05:51,860
So I had my module and my program what I'm going to do now is say file new file and I will say save

80
00:05:51,860 --> 00:06:05,710
as an inside of my main package and I'm going to create some main script that PI save that and then

81
00:06:05,750 --> 00:06:08,350
we'll also do a file new file.

82
00:06:08,610 --> 00:06:11,940
And let's put another PI script inside of my sub package.

83
00:06:12,130 --> 00:06:21,230
So we will save file save as an inside of the sub package along that other file We're going to create

84
00:06:21,230 --> 00:06:28,380
the following we will say my sub script that pi.

85
00:06:28,390 --> 00:06:34,720
So now I have this outside module some main script inside of that main package folder.

86
00:06:34,900 --> 00:06:41,800
This subscript inside of that sub package folder and then my main program so we can do is we can start

87
00:06:41,800 --> 00:06:44,860
playing around with this all the way for my program.

88
00:06:44,860 --> 00:06:54,150
So what I'm going to do is inside of my subscript will say if than say sub report and have that print

89
00:06:54,150 --> 00:06:54,860
out.

90
00:06:55,210 --> 00:07:02,490
Hey I'm function inside my subscript

91
00:07:06,190 --> 00:07:11,360
control as the save that we're going to do a very similar thing here in some main script and we will

92
00:07:11,360 --> 00:07:16,240
say the E-F will call this report main

93
00:07:19,000 --> 00:07:20,360
and this will print out.

94
00:07:20,960 --> 00:07:33,270
Hey I am in some main script in main pack which say that it's well OK.

95
00:07:33,330 --> 00:07:35,700
So let's take a look at the rectory right now.

96
00:07:35,850 --> 00:07:38,820
We'll say open a file just so we can explore this.

97
00:07:38,850 --> 00:07:43,860
So at my desktop right now I have this my module that PI won't really be playing with that anymore but

98
00:07:43,860 --> 00:07:46,160
instead I have this my program that pi.

99
00:07:46,320 --> 00:07:49,350
Now we can think of my program that pi as the main PI script.

100
00:07:49,380 --> 00:07:53,490
I want to run and I want to organize my files into packages.

101
00:07:53,580 --> 00:07:58,380
So right now we've explored how to actually create a single module which is just the PI script and you

102
00:07:58,380 --> 00:08:03,810
can imagine we'd be importing from various modules so various scripts that at a certain point it's going

103
00:08:03,810 --> 00:08:06,370
to be too much and we want to organize them into folders.

104
00:08:06,410 --> 00:08:11,600
So the way you would do that is you would have packages which are just directories that have this in

105
00:08:11,930 --> 00:08:12,480
the pie.

106
00:08:12,480 --> 00:08:15,270
So underscore underscore in an underscore underscore that pie.

107
00:08:15,410 --> 00:08:20,040
And that tells Python hey this actual directory it has a bunch of modules in it so you can call them

108
00:08:20,370 --> 00:08:22,950
using a certain syntax which we're about to show you.

109
00:08:23,370 --> 00:08:24,810
So here we have some main script.

110
00:08:24,930 --> 00:08:30,930
You can imagine I could have many more postscripts or many more modules inside of this package and we

111
00:08:30,930 --> 00:08:34,850
can keep going with sub packages as long as they have this in that pie.

112
00:08:34,870 --> 00:08:36,950
And here I can see my subscript.

113
00:08:37,260 --> 00:08:43,290
So I'm going to cancel this and when I'm going to do is inside my program that pi I already know how

114
00:08:43,290 --> 00:08:44,410
to import from module.

115
00:08:44,420 --> 00:08:50,940
But now let's realize how to import from a package to import from a package what I can say is from my

116
00:08:51,090 --> 00:08:53,910
main package.

117
00:08:54,270 --> 00:08:57,350
I can either import the actual module itself.

118
00:08:57,510 --> 00:09:01,790
Remember the module in my main package it was called some main script.

119
00:09:02,180 --> 00:09:12,020
So we'll save some main script and if I wanted to import from a sub package what I would say from my

120
00:09:12,020 --> 00:09:19,800
main package dot sub package and then I can import some module from there.

121
00:09:19,810 --> 00:09:25,720
So in this case it was called My subscript.

122
00:09:25,830 --> 00:09:31,950
So this is now how you can organize not just into a single directory but you can actually organize into

123
00:09:31,950 --> 00:09:33,310
subdirectories as well.

124
00:09:33,330 --> 00:09:38,220
And this is the syntax for that you say dot the name of that sub package import and then import the

125
00:09:38,220 --> 00:09:38,970
modules.

126
00:09:39,060 --> 00:09:43,500
And if you want you can go even further and actually import only specific functions.

127
00:09:44,900 --> 00:09:46,660
So we'll show you how to do that in just a second.

128
00:09:46,670 --> 00:09:50,200
But let's actually call the functions from some main script.

129
00:09:50,300 --> 00:09:56,030
So from some main script right here we want to call the function that's inside that script so if we

130
00:09:56,030 --> 00:10:01,950
click back here we realized that that one is just called Report main.

131
00:10:01,970 --> 00:10:07,750
So coming back or I'll say some main scrip report main close parentheses.

132
00:10:07,790 --> 00:10:12,530
So we're actually executing the function report Main which is under some main script module which is

133
00:10:12,560 --> 00:10:14,280
under my main package.

134
00:10:14,300 --> 00:10:18,480
Then we also want to if we look at my subscript that has the subreport.

135
00:10:18,620 --> 00:10:27,360
So let's run that as well saying my subscript call sub report open close princes.

136
00:10:27,600 --> 00:10:31,810
So let's save that and then let's run my program that pi at the command line.

137
00:10:32,040 --> 00:10:33,600
So we can see what happens.

138
00:10:33,600 --> 00:10:39,300
All right so now here back on my desk top up the command line I will say Python call my program that

139
00:10:39,300 --> 00:10:46,230
pi and I hit enter and I see hey I am some main script in main package and then hey I'm a function inside

140
00:10:46,230 --> 00:10:48,090
my subscript and that's it.

141
00:10:48,090 --> 00:10:52,770
Now you've successfully not only created a module in the first produce lecture but you also created

142
00:10:52,770 --> 00:10:55,010
packages and sub packages.

143
00:10:55,110 --> 00:11:00,660
And in general the idea here is just how do you organize various Paice scripts as you begin to grow

144
00:11:00,660 --> 00:11:01,810
better and better in Python.

145
00:11:01,860 --> 00:11:04,210
You're going to write more and more Python code.

146
00:11:04,320 --> 00:11:07,840
So at the beginning you'll create a couple of modules to all be in the same folder.

147
00:11:07,860 --> 00:11:13,710
No big deal then as it starts getting more and more unwieldy to have it all in the same folder all you

148
00:11:13,710 --> 00:11:18,660
need to do is create directories to have that special underscore underscore in it underscore underscore

149
00:11:18,720 --> 00:11:19,780
that py file.

150
00:11:19,860 --> 00:11:24,730
That just tells Python hey this is now a package and it allows you to run.

151
00:11:25,140 --> 00:11:25,980
What we see here.

152
00:11:26,040 --> 00:11:28,190
This my main package my main.

153
00:11:28,200 --> 00:11:29,820
That's a package.

154
00:11:29,820 --> 00:11:35,460
Notice here we were actually importing modules so you can see here that I'm just saying from some package

155
00:11:35,460 --> 00:11:40,980
name import some module what you could do as well is import functions.

156
00:11:40,980 --> 00:11:46,980
If you only intend to to use a single function so you could say from my main package import some main

157
00:11:46,980 --> 00:11:47,780
script instead.

158
00:11:47,820 --> 00:11:58,700
You could do say from my main package dot some main script import report main Whoops no open close friends

159
00:11:58,740 --> 00:12:04,330
there and then instead of calling some main script you would just say report name.

160
00:12:04,410 --> 00:12:10,010
So that's the same thing and we can come back and confirm that at our command line coming back here.

161
00:12:10,300 --> 00:12:11,860
We run this again and we see.

162
00:12:11,880 --> 00:12:13,900
Hi I am some script again.

163
00:12:14,110 --> 00:12:18,560
It's up to you whether you want to import entire modules or just specific functions.

164
00:12:19,000 --> 00:12:19,480
OK.

165
00:12:19,660 --> 00:12:24,460
That's the basic idea of packages and modules if you have any questions feel free to post to community

166
00:12:24,460 --> 00:12:27,170
forums and we'll be happy to help you out there.

167
00:12:27,190 --> 00:12:28,150
I'll see you at the next lecture.
