Hello guys.
So we are going to continue the discussion with respect to Python.
And in this video we are going to discuss about modules and packages.
Now in Python.
Uh, you know already it is an open source programming language.
And as you all know that it has lot of packages.
You know which through which you can actually use those packages.
Uh, there'll be lot of different, different inbuilt functions that will be available in those packages
so that you don't even have to write the code completely from scratch.
Right.
So just by using this modules and packages, uh, you can actually use the functions that are available
over there and quickly write down the code.
Right.
So here uh, modules and packages helps organize and reuse the code.
So let me just show you, uh, and let me talk about some of the important modules that we specifically
use.
And there are a lot of modules over there.
And as you will be seeing as I will be probably going ahead with more tutorials, you'll be seeing different
different packages that I will be using.
So first of all, I would like to show you how do we import a package.
Right.
So this math Right.
So let's say I'm importing math.
This math is just like a package or a module.
And inside this I'll just say that this is a package.
And inside this you have a lot of inbuilt functions, specifically mathematical operations that you
really want to do.
Um it will be available over here.
So in order to import any package we just need to go ahead and write import and that the package name
okay.
And uh, let's say if this package is not installed, that we then we have to properly install this
particular package.
Okay.
I'll show you one of the example.
Let's say that I want to use numpy.
NumPy is an amazing package which works with array.
I'll show you how to install it and probably go ahead, you know.
So here I have imported math.
Now math has a lot of inbuilt function.
Let's say I'll go ahead and write math s q r t.
So this is uh basically a function which will do the square root of a number.
So if I give the number over here and if I execute it here, you'll be able to see that I'm able to
get the square root of 16.
Okay.
Similarly, uh, you'll be seeing I will have different different functions inside math.
Right.
Let's say I want to use square root also.
Right.
So how do I import square root function.
Uh from this particular package.
So I can just go ahead and right from the package name.
Right.
So let's say from math import numpy import s q r t.
And let's say I want to use one more function that is available inside math.
So it will be pi okay.
So there are two functions that are available inside math okay.
And if you want to probably go ahead and check it out you can just go ahead.
Right.
Math dot.
And these are all the inbuilt functions that are available uh with respect to this particular package.
Right.
Math dot.
And here you will be able to see it okay.
So let's say uh I want to use the square root of pi.
So I've imported both this functionalities over here.
Now I can directly call this functionality or call this function.
So I will just go ahead and write a script.
Let's say I want the square root of 16.
And then I want the square root of 25 let's say.
Or I want also to probably print, you know, what is the pi value.
Okay.
So pi everybody knows how much is the value right.
3.142.
So if I go ahead and execute over here you'll be able to see square root of 16 is four, square root
of 25 is five.
And uh pi is definitely 3.141.
So similarly in every packages you may be having multiple functions which you can directly use it.
And in order to import it, you just need to remember this particular keyword from the package name.
Import any function that you want that is available in that particular package.
Okay.
Now let's say that you don't have a package right now.
Let's say there is one more package which is called as numpy.
So I will go ahead and use numpy.
I will talk more about numpy as we go ahead in the upcoming session.
So there will be a detailed video on numpy and pandas.
NumPy is specifically a library that will be used whenever you want to create arrays.
You want to apply different operation with respect to the arrays so that where this particular package
will be used.
So if I am using numpy, let's say I'll go ahead and write import numpy as np.
Now what this NP is NP is nothing, but it is an alias.
Okay, alias.
I don't even have to write every time numpy dot.
You know, I can directly use np dot so that my code.
I don't have to completely write the entire package name, you know.
So that is where we specifically use the alias.
Now let's say that I am importing this and if I import this I'm getting an error.
No module named numpy, because obviously I have not installed numpy.
When you create an environment by default, uh, in Python, a virtual environment, some of the packages,
some of the modules will get uh, installed, but not every module will get installed.
Right.
So let's say that I want to probably go ahead and import numpy.
Uh, and for that I need to install.
So what I will do in the requirement dot txt I will go ahead and write numpy okay and I will save this.
Okay.
Now I will go to my terminal.
Okay.
Now let me just go ahead and show you in my terminal.
So my terminal will be over here and I am in my command prompt.
I am inside my Python basic.
So I will go CD dot dot I will go back because this is entirely this requirement.
Dot txt is present in this Python folder right now.
I'll go ahead and write pip install minus r requirements.txt.
This is one way that I can directly write, or I can also go ahead and write pip install numpy.
Okay, so if I go ahead and install pip install numpy you'll be able to see that hey, my installation
will start taking place and this will basically get installed in my venv environment.
Okay.
So this is how uh, things you basically do.
And now you can see my numpy has got installed.
Okay.
Now I'll close this okay.
And what I will do I will go again back to my statement.
Now let's go ahead and import this okay.
Now here you'll be able to see that my import is successful okay.
Now let me just show you one simple operation where I will be trying to create an array.
And here I will give one, two three, four.
So if I go ahead and execute I will basically get an array.
We'll discuss more about the numpy.
But since we are just discussing about basic, uh, Python modules and packages, we are discussing
over here right now, this is like there'll be a lot of inbuilt modules.
Okay, let's say from math I want to import every module.
Right.
So what I can do is that I will go ahead and write from math import star.
Star basically means it is importing all the functions that are available in math.
Right?
I don't have to specify.
Right.
Right now I can directly go ahead and call s q r t.
Okay.
Let's say I will go and call security.
So security is an inbuilt function that is available in math.
And since we have then import star automatically I'll be able to see the output.
So here you can see I'm getting the answer as four right.
And similarly I can go ahead and probably print my pi okay.
And up.
Also I will just go ahead and use a print statement.
Okay.
Print statement.
And then we are uploading this.
And here you can just see by import star.
That basically means we are able to access all the functions that are available inside this particular
uh, package or module, which is called as math perfect.
Uh, I hope, uh, you're able to understand till here there are different different libraries that
we'll be seeing when we'll be doing the coding.
But just get an idea in this particular video, what I'm actually trying to show you is that how do
you import a package or a module, a inbuilt module that is already available in with packages that
are already available, like math, numpy?
If there is no number, if there is no library as such, you can probably install it.
Okay.
Now the most important thing how do I probably create my own custom package?
Okay.
Now let me show you one thing over here okay.
Let me just go ahead and create one folder.
And this will basically be my package.
Now let's say that this is my source code that I'm going to write.
Okay.
I want to define my own custom.
Uh, you can basically say custom module or custom package, which I should be able to call in any other
files.
Okay.
Because here you can see that I'm importing math.
Right.
And this math may be available somewhere, right.
It is available in our V and V environment.
All the packages are available and we are importing from there right now.
Similarly, I can also go ahead and create my own custom things.
Right.
So let's first of all start.
So let's say this is my package folder.
And here I'm going to create some modules.
So first file that you actually require in this is init underscore underscore init.py okay.
So this.py file why this init uh underscore underscore init.py is a it is a special file.
Okay.
So here I'll just go ahead and write a comment for you so that you will be able to understand okay.
So this is a special file okay init underscore underscore p y.
Or let me just use a multi line okay.
And I will go and use this.
See this okay I will close it over here.
Now this init underscore underscore underscore init dot uh underscore underscore dot pi is a special
file used in Python to define packages and initialize the namespace.
Okay.
So let's say that I have actually created this.
Now let me just go ahead and create any file that I want.
Let's say I will just go ahead and write, uh, you know, um, one operation or let's say that I am
creating a calculator.
And so here my maths dot pi file is there.
Okay.
Now inside this maths dot pi file what I will do I will just go ahead and define a function.
Let's say I'm going to write definition addition a comma b okay.
So what this function basically does is that we are just going to return a plus b okay.
We know that in math also write some operation is also the inbuilt sum operation is there.
But I'm just trying to show you.
So inside this package you will be able to see that math dot pi file is there.
And inside that there is a function which is called as addition.
Now very important thing that you really need to understand over here this package folder dot math.
Right.
It becomes our module.
Right.
Since we are using this underscore underscore init dot pi.
And always remember inside a folder, if you want to initialize this entire folder and define packages
right or define modules, you have to probably use underscore underscore init.py even though it is empty.
Just keep it empty.
But you need to have this file.
Now I'll show you how I will call this particular function by importing this right from this particular
package.
Okay, now this is my entire package.
Now I'll go back to my import dot Ipynb.
Now here I will go ahead and write from packages.
Right.
So see from package I'm getting dot.
What is my file name.
Maths.
Right.
Import right from package dot math.
See from package dot math.
And remember this needs to be in the same folder location.
Then only you'll be able to call it right.
So from package dot math right.
Dot math is basically this folder I'm going to import addition.
So let me just go ahead and copy this.
And here you'll be able to see I will just go ahead and copy this right now.
After calling this I can just go ahead and call this function right.
And here I can give two comma three.
And if I execute it, you'll be able to see that I'm actually able to get the output.
So I hope you are getting an idea right.
How do you probably create your own custom module.
Right.
And from package dot maths how I am importing addition.
Right.
And uh, if you don't want to write like this okay, let's write it in another way from package.
Uh, import math.
I can also write like this.
Okay, I import math.
Now, in order to call this, I can go ahead and write maths dot addition.
Okay.
And here I can call two comma three.
Right.
So this how you can probably create any number of files inside this particular package folder.
Right.
Since I have underscore underscore underscore underscore dot pi.
When I create only this file then only this becomes a package or a module.
Right.
And then I can actually call this anywhere that I want.
I can build my own custom function.
I can build any number of custom function.
Let's say I'll go ahead and write definition subtraction subtraction.
And here I'll give a comma B.
And uh let me just go ahead and write return a, return a minus b.
So once I have updated subtraction, you know, so what I will do I will let me just show you how to
call C in Ipy and B obviously you know how to call it right by just importing addition maths, whatever
things you specifically require.
But uh, mainly when we are developing projects, we have to call all these files in some py file.
So let me just go ahead and create one more py file.
And here I will just go ahead and write test.py okay.
Uh test.py already exist.
Let me just check.
Okay.
Uh, here we will create, uh, test dot p y.
So test dot p y is over here.
Now, in this test dot p y, I will try to call from this particular package.
So how do I call it.
So here I will write from package dot maths okay.
Import star okay.
Now what I can do I can basically call my addition.
So I will go ahead and write print Addition.
And here I'm going to say two comma three.
And let me just go ahead and write print.
Subtraction with three comma four okay.
Or four comma three okay.
Now I will just go ahead and show you how to execute this so I can directly execute dot pi file.
Always make sure you have to be inside this folder itself.
Write this five modules if you want to execute any test.py file, like any py file, you have to make
sure that you have to go to that folder location and run it.
So here I will go just go to CD five modules okay.
Now in order to execute it I will write Python test.py.
Now see here I am able to get an answer for addition.
I'm getting two comma three for subtraction I'm getting one.
Okay, so uh, this was just an idea where I've actually created the packages.
My own custom packages, math.py file, any number of function that I want to.
And I've shown you how to call it in ipynb file.
Along with that I will also have shown you how to call it in test.py file.
So uh, this in short talks about, uh, you know, uh, the most important thing that how an import
is basically done.
Right.
Uh, we'll also talk about one important thing, uh, which is called as uh sub uh sub package uh imports.
Okay.
And uh with respect to sub package imports, uh, the thing is that let's say that okay, I have this
packages.
Okay.
Now let me just show you one more thing.
Okay.
I have this packages.
Now I will click this I will create one more folder okay.
And this sorry inside my packages.
Inside my packages okay.
So so this is my package folder.
I'll create one more folder.
Sub packages.
Okay.
Now if I also want to make this as a package and probably call it somewhere here.
Also I have to use underscore underscore init underscore underscore dot pi file.
Okay.
Now I can go ahead and create any operation that I want.
Let's say I want to probably go ahead and write math dot pi okay.
Now math dot pi is nothing.
but, uh, we'll specifically be using this, uh, let's say, uh, instead of math, I'll just use
some other file name so that we'll be able to distinguish it, let's say mult dot pi.
So this package is responsible in, you know, performing any multiplication operation.
So let's say I will go ahead and define my function and I'll say hey this is my multiply here I'm going
to give a comma B.
and this is basically going to return a star b I know the functions are very simple, but the core idea
is to understand how to call all these packages.
Now perfect this mult dot pi is present inside the sub package.
Now what I will do in my test dot pi I will try to call this particular package.
Okay.
Now it will be interesting.
So here you will be able to see from from package okay Dot maths dot dot sub package okay.
Sorry.
From package dot sub package dot mult.
Okay I'm going to import my multiply function.
See how step by step we went.
So packages first there were packages.
Then we went to sub packages.
Then we called this mul dot p y.
And remember we are only able to identify it unless and until this underscore underscore innit dot p
y file is present or not.
If this particular file is present then this makes this particular folder as a package okay by default.
So now let me just go ahead and call this over here and let me go ahead and print my multiplication.
So here I will be calling my multiply function with five four comma five.
Now if I just want to execute it, how do I execute it?
I'll just go ahead and write Python test.py.
Okay.
So here you can see initially I got five one and 20 right.
So addition uh three plus two is five four minus three is one and four uh multiplied by five is 20.
So I hope, uh, you got this specific idea.
I hope you are able to understand about packages and all.
Um, this was, uh, it from my side.
Uh, and here I've shown you both the things how to call default packages that are open source packages
that are already available.
Right.
So I've actually used this numpy math and all.
And the second thing that I have actually shown you how to call any other packages that are available
in any other folders or functions that you are probably defining.
So yes, this was it for my side.
I will see you all in the next video.
Thank you.
Take.