Python Module

Introduction

In this article, we will discuss about the python modules such as

  • What is a python module.
  • Why Do we use python module.
  • How to create and use python module.

Prerequisites

This guide assumes that you are using python 3.x.

What is a python module

A python module is a .py file which contains code. Module name is same as file name. Python module can contain following

  • Functions
  • Variables
  • Class definitions
  • python statements

For example: In the math module below (math.py), we have create a variable called PI, defined a function add and added a statement PI = 3.14

PI = 3.14

def add(a, b):
    return a+b

Why to use a module

A module is a way of grouping logically related features. Module provide benefits such as-

  • Improves code re usability. You can import a module ‘A’ in another module ‘B’ to use module A’s functions, classes etc. You do not need to write those again.
  • Improves maintainability of the code since it reduce duplication of the code.

How to create module

Let’s create a module to calculate addition and subtraction.

Create a file calculator.py and add following add and subtract methods to it. This is your first module.

def add(num1, num2):
    return num1+num2

def subtract(num1,num2):
    return num1-num2

You can include any variable, class definition in module. A module is not limited to only function definitions.

How to use a module

There are two ways to use a module.

Import module in another module

  • To import calculator module, start python interpreter and type import calculator. This brings calculator module’s reference in to current namespace. Then you can use calculator prefix to access any definitions (function, variable) from calculator module.
import calculator
calculator.add(10,20)
  • You can also import all the definitions directly in the current context.
from calculator import *
add(10,20)
  • You can import only selected functions/variables instead everything
from calculator import add
add(10,

Run module’s code as a main program

You can also run a module as a standalone script. To do that, you need to define an entry point in that module. When a module is run as a script, it’s property ‘name’ is assigned as ‘main’. So we specify which function(s) to call when module is run as a script.

if __name__ == '__main__':
    add(10,20)

To run module as a script, on the command line , type python -m calculator OR python /calculator.py

When you import a module or execute a script, python interpreter needs to be able to locate the modules. It searches the module in a particular order -

  1. Search in the current directory
  2. Search in the directories listed in PYTHONPATH environment variable

Make sure that the module you are trying to run is discoverable as per the above otherwise python interpreter may throw exception that module not found

To see all the path listed under PYTHONPATH, use following

# importing sys module
import sys

# importing sys.path
print(sys.path)