1
00:00:00,540 --> 00:00:03,150
Hello and welcome to this video.

2
00:00:03,150 --> 00:00:11,130
In this video I'm going to introduce you to what packages are in Python.

3
00:00:11,130 --> 00:00:14,040
So what are packages in Python.

4
00:00:14,040 --> 00:00:21,900
Packages are basically folders or directories that contains modules.

5
00:00:22,110 --> 00:00:28,200
They also contain a special python file which is code in it.

6
00:00:28,260 --> 00:00:29,660
Dot P Y.

7
00:00:29,850 --> 00:00:35,590
So it has a double on the score on either side.

8
00:00:35,590 --> 00:00:36,210
The.

9
00:00:36,300 --> 00:00:46,680
In it the P Way of file can be empty the file basically is used to tell Python that the directory or

10
00:00:46,680 --> 00:00:56,080
the folder contains a python package which can be imported just like a module.

11
00:00:56,160 --> 00:01:08,090
The packages are a convenient way to organize your modules and they can also be nested in each other

12
00:01:08,100 --> 00:01:17,320
so you can have a package inside a package creating a package is fairly straightforward.

13
00:01:17,410 --> 00:01:23,280
If you're going to do it manually you create a directory inside that directory.

14
00:01:23,470 --> 00:01:29,080
You create a python file and name it on the score on the score in it.

15
00:01:29,080 --> 00:01:40,300
On the score on the score dot P Y when creating a package using the PI charm IDC it automatically adds

16
00:01:40,690 --> 00:01:42,610
the neat file for you.

17
00:01:42,640 --> 00:01:51,460
So let's create a package somewhere right click on my directory here go new and go Python package and

18
00:01:51,610 --> 00:01:55,680
I'm just going to give the package a name.

19
00:01:55,780 --> 00:01:59,200
I just call it healthy.

20
00:01:59,900 --> 00:02:02,060
That's the name of my package.

21
00:02:02,080 --> 00:02:02,810
Our click.

22
00:02:02,840 --> 00:02:06,460
Okay so I've now created a package called Healthy.

23
00:02:06,460 --> 00:02:12,810
I can see the pie charm as automatically added this in it though.

24
00:02:12,850 --> 00:02:17,500
P Why file so you can also nest on the packages.

25
00:02:17,530 --> 00:02:24,310
So if I wanted to create another package inside this package are just right click and go new and then

26
00:02:24,640 --> 00:02:32,860
the python package and that will automatically add another in it the P Y file.

27
00:02:32,860 --> 00:02:41,700
So once you have created the package you can add any number of Python files into that package.

28
00:02:41,710 --> 00:02:48,340
So what I want to do I want to create a module inside this package called Healthy someone a right click

29
00:02:48,640 --> 00:02:59,310
and go new go python file and I'm just going to create a module I'll call it foo after below.

30
00:02:59,320 --> 00:03:03,260
So that is the name of my module.

31
00:03:03,310 --> 00:03:14,140
Now inside the module I can add a simple function so I just say the F and I'll call the function say

32
00:03:15,490 --> 00:03:26,440
fruits and inside the parentheses I can just passing a parameter which would be a name that you supply

33
00:03:26,500 --> 00:03:33,900
for the fruits so tab down and I'll just add a print statement inside that print statement.

34
00:03:34,240 --> 00:03:45,010
I can just see reference the name the name parameter under a comma and then I would just add a tax to

35
00:03:45,010 --> 00:03:48,940
say name and then you say whatever name you pass in as a fruit.

36
00:03:48,940 --> 00:04:02,530
It will say if for example you pass in the name of Apple which you say apples are good for you.

37
00:04:02,590 --> 00:04:10,930
So this is basically a module that I've created inside my package.

38
00:04:10,930 --> 00:04:20,910
So if you look at this foo here which is a module is nested inside this package called healthy once

39
00:04:20,910 --> 00:04:30,660
you have created a package you can access the module within that package the same way you would access

40
00:04:31,170 --> 00:04:37,380
a normal module by using the import or the from keyword.

41
00:04:37,650 --> 00:04:41,730
So before you can use a module you have to import it.

42
00:04:41,850 --> 00:04:51,150
Now my module that I'm trying to use is inside a package so I'll need to type in import followed by

43
00:04:51,150 --> 00:05:02,170
the name of the packet which is called Healthy and then add a dot followed by the name of the module.

44
00:05:02,170 --> 00:05:13,840
So I have now imported the module I want to use by first adding the name of the package and then I use

45
00:05:13,840 --> 00:05:17,780
it dot followed by the name of the module.

46
00:05:17,800 --> 00:05:26,620
So now I can then access the content of the module.

47
00:05:26,620 --> 00:05:35,090
So the way I can do that I can just say help me which is the name of the package.

48
00:05:35,220 --> 00:05:45,750
Dot foo foo is the name of the module and then dot again which is a name of a function inside that module.

49
00:05:46,290 --> 00:05:57,870
And I can now pass in a value which is an argument for the name parameter inside this function called

50
00:05:57,880 --> 00:06:02,980
fruits which is inside this module here.

51
00:06:02,980 --> 00:06:07,240
So this is what I'm giving the value for inside here.

52
00:06:07,240 --> 00:06:13,000
I've given it a value of apples so I'm just going to save my file.

53
00:06:13,000 --> 00:06:18,010
Now if I play this I should get a response.

54
00:06:18,010 --> 00:06:20,770
It says apples are good for you.

55
00:06:20,800 --> 00:06:28,720
So if we go back into the module I created this is the module for the package and this is what it's

56
00:06:28,720 --> 00:06:31,570
printed out the name which is what I have supplied.

57
00:06:31,570 --> 00:06:37,930
I've supplied apples and then it appends that text to it which is this.

58
00:06:37,930 --> 00:06:43,030
So this is one way of accessing your module.

59
00:06:43,060 --> 00:06:48,690
Another way to access a module is by using the from key word.

60
00:06:48,700 --> 00:06:52,230
So what I'm going to do I'm just going to comment out.

61
00:06:52,270 --> 00:06:53,770
Line one here.

62
00:06:53,770 --> 00:07:01,600
So then that code does not run and here on line three I'm going to add the from key words I'm going

63
00:07:01,600 --> 00:07:13,470
to say from the package code healthy I want to import the module Code Foo.

64
00:07:13,590 --> 00:07:14,080
All right.

65
00:07:14,100 --> 00:07:20,700
So that import set and then on line 5 I can modify this code here.

66
00:07:20,860 --> 00:07:23,950
All I need now I don't need the Healthy bit here.

67
00:07:24,310 --> 00:07:32,410
I can just remove that and just reference the module directory so I'm accessing the function called

68
00:07:32,410 --> 00:07:36,950
fruit which is inside this module called foo.

69
00:07:37,030 --> 00:07:42,840
So now if I save that and run it I should get the same output so I click on play.

70
00:07:43,360 --> 00:07:45,300
I can see gives me the same pain.

71
00:07:45,350 --> 00:07:47,180
Apples are good for you.

72
00:07:47,200 --> 00:07:55,730
So the two main ways you can access a module you use the import or you can use the from keyword.

73
00:07:55,780 --> 00:07:58,300
OK so that's it for this video.

74
00:07:58,300 --> 00:08:00,700
Thank you for watching and bye for now.
