hey guys welcome to the next blog on python tutorial for beginners in this blog we will discuss how to create modules in Python now in the previous blog I have already discussed briefly about built in modules and how to import a built-in module you use an import keyword and then you use the name of the built in module in Python built-in means this module is already present in Python so we can use these built in module in order to perform some functionality so for example this math module helps us to perform some mathematical operations now first of all what is a module so it turns out that a module is nothing but a Python file now to prove my point let me just hover over this math module and just press control and then hover over it so right now I am pressing control and I'm hovering my mouse over this mat on the pycharm IDE and now I'm going to click it and when I click it you will see this module is nothing but a file right and inside this module we have a number of functions which we can use for example a COS function or a tan function or many different functions which we can use from this math module so a module is nothing but a Python file now if a module is nothing but a Python file why don't we create a module and it turns out that Python allows us to create our own modules so let's create our own modules and let me show you how you can create your own module and right now you can see in my project there is only one file which is hello dot py and it's empty right now now to create a module I am going to just create a new file so right click and then new Python file and this module I'm going to name it as my functions and then press ok and this file is created so now I have two files in my project one is my functions and other is hello dot py so let's say I want to create some functions inside this my functions dot py file so let's create some function I'm going to very quickly create some very basic functions which you already know in Python so so let's say I add function which returns the addition of two numbers and also let's create let's say multiply function so multiply and this is going to just give the product of two numbers so let's say this is our module and it has two functions now somehow I need to use these two functions from my functions dot py file into this hello dot py file now as you can see these two files are in the same directory right so I can import this my functions file using import and then the name of the file which is my functions and that's it so in order to import a module you just write a keyword called import and then you just write the name of the file without the extension py ok so you don't need to provide any extension here and then I can use this function so let's say I want to print my function which is the file name so whatever file name you give here you need to use the file name here dot you will see all the functions in the file are now visible in the other file which is Hallowed py so let's use this add function and I'm going to just give two parameters here and also let's use the multiply function from my functions file so the file name and the method name or function name and let's give two parameters here also and that's it so it's that simple to create your own modules and import them in to other files in Python now when I run this program is going to give me the addition and the product of these two argument which I have provided here now let's say your my functions file is in some other directory so let me create a directory so I'm going to right click here and then I'm going to create a directory and I'm going to name it as dir and then click OK and now I'm going to move this my functions dot py file into the new directory so I'm going to just right click and cut from here this file and I'm going to just paste this file into the dir directory ok so let me just click OK here and as soon as I do this you will see here my charm is intelligent enough to refactor this code and now it has given us the correct import ok so if this my functions file is in some directory then you use this type of notation so you write from and then your directory name and then import keyword and then your file name so you can see this is the directory name and this is the file name so I use from directory name import file name and when I run the code once again it's going to give me the same answer now there is one more way of writing the same notation and that is using the import so use import keyword and then write the name of your directory dot the name of your file so directory dot the file name and then you use this name in place of your my function name ok and once again when you run the code it runs fine now sometimes you might feel that this is a law name and Python have the solution for it also so it turns out that you can rename your file using a keyword called as so after the import so import directory name dot file name as and then you can provide your name for example my functions I will just name it as MF ok and now I can use this MF name instead of this big name and this will also work fine so either you can import from your file which is inside the directory using this notation or you can use this notation you can also write as in front of this notation also so from directory name import file name as whatever name you want to give here and this you can use to call your function using this dot notation now this was the easy stuff and I have just written here the two functions and then imported these two functions into the other file but what if if I have multiple classes I haven't shown you how to import files which have classes in them so let's see how to import the Python files which have classes inside them so I'm going to close these two files and in the last blog I have shown you how to use inheritance in Python and we have created this class polygon and two other classes which is triangle and the rectangle class and this might be right now the perfect candidate to import into some other file so I'm going to create two more file one is the rectangle file so I'm going to just right click and create a Python file and I'm going to name it as rectangle and I'm going to create one more file with the name of triangle so right-click new Python file and the name is triangle dot py and let me create the fourth file which I'm going to name it as main and this will be our main file in which we will import all these classes so in my polygons class I will just leave this polygon class and I will just transfer this rectangle class into the rectangle dot py file and this triangle class into triangle dot py file and also this was the instantiation of the rectangle and triangle class which we have done so I will transfer this code into our mean dot py file ok so now we have a polygon dot py file in which we have the polygon class and then we have the rectangle class inside rectangle dot py and also we have the triangle class into the triangle dot py file and rest of the code we have inside the main dot py now when you go to the rectangle dot py you will see that it's inheriting from the polygon class that means we need to import polygon class into the rectangle dot py file so how to import it you can use from and then the file name which is polygon and then use import keyword and use the name of the class which is polygon and you will see that this error is gone and same we will do inside the triangle dot py file in order to import the polygon class into that triangle dot py file so this is the file name so from file name import the class name now when I go to the polygon class we don't need to import anything here because it's a superclass and there is no import needed here let's go to the main dot py file and here you will see red squiggly line under rectangle and triangle class because we haven't imported this triangle dot py and the rectangle py into our main Python file so the procedure is same from your file name which is first of all rectangle import the name of the class which is rectangle same we will do for the triangle class so from the triangle file named triangle dot py and then import and then the name of the triangle class and you will see as soon as you do this the error is gone so now there is no red squiggly line under any of the code so now let's run the code so I'm going to just go to the main dot py file and I'm going to right click on this main dot py file and I'm going to just click on run mean which is going to run the program and it's going to print the result which is the area of rectangle and the area of the triangle so in this way you can import the py file which have classes inside them so this is how you create your own modules in Python and import them into other Python files I hope you have learned something new this time and I will see you in the next blog
Post a Comment