Hello and welcome to this video.
In this video I'm going to introduce you to what packages are in Python.
So what are packages in Python.
Packages are basically folders or directories that contains modules.
They also contain a special python file which is code in it.
Dot P Y.
So it has a double on the score on either side.
The.
In it the P Way of file can be empty the file basically is used to tell Python that the directory or
the folder contains a python package which can be imported just like a module.
The packages are a convenient way to organize your modules and they can also be nested in each other
so you can have a package inside a package creating a package is fairly straightforward.
If you're going to do it manually you create a directory inside that directory.
You create a python file and name it on the score on the score in it.
On the score on the score dot P Y when creating a package using the PI charm IDC it automatically adds
the neat file for you.
So let's create a package somewhere right click on my directory here go new and go Python package and
I'm just going to give the package a name.
I just call it healthy.
That's the name of my package.
Our click.
Okay so I've now created a package called Healthy.
I can see the pie charm as automatically added this in it though.
P Why file so you can also nest on the packages.
So if I wanted to create another package inside this package are just right click and go new and then
the python package and that will automatically add another in it the P Y file.
So once you have created the package you can add any number of Python files into that package.
So what I want to do I want to create a module inside this package called Healthy someone a right click
and go new go python file and I'm just going to create a module I'll call it foo after below.
So that is the name of my module.
Now inside the module I can add a simple function so I just say the F and I'll call the function say
fruits and inside the parentheses I can just passing a parameter which would be a name that you supply
for the fruits so tab down and I'll just add a print statement inside that print statement.
I can just see reference the name the name parameter under a comma and then I would just add a tax to
say name and then you say whatever name you pass in as a fruit.
It will say if for example you pass in the name of Apple which you say apples are good for you.
So this is basically a module that I've created inside my package.
So if you look at this foo here which is a module is nested inside this package called healthy once
you have created a package you can access the module within that package the same way you would access
a normal module by using the import or the from keyword.
So before you can use a module you have to import it.
Now my module that I'm trying to use is inside a package so I'll need to type in import followed by
the name of the packet which is called Healthy and then add a dot followed by the name of the module.
So I have now imported the module I want to use by first adding the name of the package and then I use
it dot followed by the name of the module.
So now I can then access the content of the module.
So the way I can do that I can just say help me which is the name of the package.
Dot foo foo is the name of the module and then dot again which is a name of a function inside that module.
And I can now pass in a value which is an argument for the name parameter inside this function called
fruits which is inside this module here.
So this is what I'm giving the value for inside here.
I've given it a value of apples so I'm just going to save my file.
Now if I play this I should get a response.
It says apples are good for you.
So if we go back into the module I created this is the module for the package and this is what it's
printed out the name which is what I have supplied.
I've supplied apples and then it appends that text to it which is this.
So this is one way of accessing your module.
Another way to access a module is by using the from key word.
So what I'm going to do I'm just going to comment out.
Line one here.
So then that code does not run and here on line three I'm going to add the from key words I'm going
to say from the package code healthy I want to import the module Code Foo.
All right.
So that import set and then on line 5 I can modify this code here.
All I need now I don't need the Healthy bit here.
I can just remove that and just reference the module directory so I'm accessing the function called
fruit which is inside this module called foo.
So now if I save that and run it I should get the same output so I click on play.
I can see gives me the same pain.
Apples are good for you.
So the two main ways you can access a module you use the import or you can use the from keyword.
OK so that's it for this video.
Thank you for watching and bye for now.