posts found in this keyword

    How To Create Modules In Python

    How To Create Modules In Python

    How To Create Modules in Python is today’s topic. We will see how to create a module and import that module in Python.

    A module is a file containing a set of functions you want to include in your application.

    Python Modules refer to the file containing Python statements and definitions. Any file is a module file in Python if that file contained the Python code and saved as a .py extension.

    Why use Python Modules?

    We use modules to break down a large code base into small manageable and organised files. Furthermore, modules provide the re-usability of code.

    We can define our most used functions in the module and import it, instead of copying their definitions into different programs.

    Code re-usability is one of the great coding principles in any programming language.

    Let’s create a module. Create a file called and add the following code:

    def multiply(x, y):
      op = x * y=
      return op

    The above program returns the multiplication of two digits. So, now is now module file. We can import that file in other file and use that multiply function and get the result.

    Create another file called and write the following code:
    import mod

    So, we have created the in the same directory as, then we imported the inside the file and used the multiply function, passing the two parameters and get the result.

    From our terminal, we can now see the result as follows:

    $ python3

    The result of our application, using the imported module

    How to import modules in Python?

    We can import the definitions inside the Python module to another Python module or Python file or the interactive interpreter in Python.

    We use the import keyword to import module. If we need to import our previously defined module , then we type the following code in the file:

    import mod

    We can also import the direct multiply function from the mod module. See the following code:

    from mod import multiply

    It will also give us the same output.

    Python has the ton of standard modules available.

    You can check out the full list of Python standard modules . These files are in the Lib directory inside the location where you installed Python.

    Standard modules can be imported the same way as we import our user-defined modules. There are no difference.

    Variables in Module

    Python module can contain the functions, as already described, but also variables of all types like lists, dictionaries, objects, etc.

    Write the following code inside the file:

    student = {
      'name': 'Joe',
      'age': 21,
      'college': 'Yale'

    Now, import the module inside the file
    from mod import student

    What do you think the output will be?

    Naming a Module in Python

    You can name the module file whatever you like, but it must have the file extension .py.

    Import with renaming in Python

    We can also give the alias to any Python module while importing that module. See the following example.
    from mod import student as s

    It will give you the same output as above. We have renamed the module to s and print the variable from the file.

    That is how you can rename your module however you want.

    Built-in modules in Python

    There are lots of built-in modules available like math, random, datetime, etc.

    Let’s import the math module and print the value of pi.
    from math import pi as p

    We can also import the built-in platform module.
    import platform

    The output for me is Darwin as I am using Macbook.

    Using the dir() Function

    There is a built-in function in Python to list all the function names or variable names in the module. That function name is dir(). See the following example of the dir() function in Python.
    import math
    m = dir(math)

    Above code gives all the functions of Math module.

    This will help you find certain functions within a module, that you may be looking for or intend to use.

    That’s it for now, stay tuned for more.