Creating and using modules in python guide for 2021.

Kumar Saptam

Kumar Saptam

We have always imported many libraries in our python program. But have you ever wondered how does these library codes come into my program with just a single line of code?

If yes then this is called modules, if not then this is called witchcraft 😂.

We will learn about how to create custom modules and use custom modules in your python project to help build a structured python project.

Bull Fighting or modules.

Now to start exploring the module we will first start with:

What is a module or library?

The module is a piece of code that you can use anywhere inside another code by just importing it. Or in simple words a reusable piece of code.

How to create a module?

Furious Coding Gif

You can create a module by using functions or classes or both (In most cases). If you don’t know functions then see this very nice post on functions. And if have then let’s jump right into it.

How to create a function in python?

To create a function, you need to use the inbuilt keyword “def” with a successor of the function name and then parentheses pair.

def functionName():
    print("Hello World !")

How to create a class in python?

To create a function, you need to use the inbuilt keyword “def” with a successor of the function name and then parentheses pair.

class className:
    def functionOne():
        print("Hello world from a child function !.")

To create it in a module.

Now you need to create a python file named exampleModule.py and then add above codes into this. To make it into a module. Now your file should look like this.

def functionOne():
    print("Hello World !")

class className:
    def childFunction():
        print("Hello world from a child function !.")

Creating a file to use these modules.

Now we have created our modules in another python file. Now let’s create a new file with name as parent.py and add this code. parent.py

from exampleModule import functionOne
functionOne()

Now if you run this code you can see that hello world is printed and it is not inside this file. Boom You just cerated a module.

Explanation

Here in the first line of code we import the file by using out filename which we created earlier when we were creating functions. Now that code is imported into this code as a function.

Now you may want to know why the childFunction got triggered it's because we didn't import it. So let's import both the class and he function. Then we will use it's child function to print our messages.

from exampleModule import className, functionOne
a=className() #creating a reference to the class
a.childFunction() #the child function from class

functionOne() #the custom function.
                

Now when you will run this code, you can see that our print functions are printed.

How does import works in python?

Graph of import statements.

The import keyword imports any module in a python program. There are many types of import system.

The different types of imports are.

  • Single module import import module
  • Multiple module import import moduleOne, moduleTwo, ModuleThree
  • Class or function import from a file from parent import module

Summary Blocks

  • What are import statements.

    Import statements are inbuilt keyword which imports or adds another python code in your python code.

    from parent import module
  • What are functions and classes ?

    Functions and classes are block of code which are reusable. The main difference between is that classes persists data if you want them to, but functions won't do that.

  • How to create a module?

    To create a module create a new python file with classes or functions or both in them. Then import the file by using from pythonFile import function . And then use the functions in it.

That's it about modules. You just excelled it.

Smiling Cat

If you like it then hit the like button down below, and subscribe to our newsletter.

Comments are loading