Python - Modules


python logo

In this tutorial we will learn about modules in Python.

What is a module?

A module is a file containing Python program and the program can consists of variables, classes and functions.

We use module to organise logically related code in one place so that it can be used easily.

For example we can create a module that contains code to print greeting messages or create a module that computes area.

Types of module

We can divide modules in two groups.

  • Built-in modules
  • User defined modules

Built-in modules

These are the modules that come with Python. Example of built-in Python module is the Math module.

User defined modules

These are the modules written by developers for custom use.

Any module that you will create or download from online Python repository created by other users are user defined modules.

Create a module

To create a module all we have to do is save our Python code in a file with .py extension.

In the following example we are creating a module that contains functions to compute areas. And we are saving this in a file named

File name:

# area of square
def square(x):
    return x * x

# area of rectangle
def rectangle(l, b):
    return l * b

# area of circle
def circle(r):
    return (22 / 7) * (r * r)

Import a module

To use a module we have to first import it in the file were we want to use it.

We use the import keyword to import a module.


import moduleName

Where, moduleName is the name of the module.

When we use the import keyword we are importing all the content of the module.

In the following example we are importing the complete Math module.

import math

# some code goes here...

Import specific items of a module

We use the following statement to import specific items from a given module.

from moduleName import item1, item2, ...

Where, moduleName is the name of the module and item1, item2 are the items that we want to import from the module.

In the following Python program we are only importing the sqrt method and constant e from the math module.

from math import sqrt, e

# some code goes here...

We can use from moduleName import * statement to import all items from the given module.

Importing everything using * is not a good programming practice. Always import what you need in your program.

Import a built-in module

We can use the import and from ... import statement to import a built in Python module.

Import a user-defined module

The following Python file is a module consisting of message function.

File name:

def message(str = "World"):
    return "Hello %s!" %str

In the following Python file we are importing the greetings module.

File name:

# import module
import greetings

# some code goes here...

Using items of a module

A module can contain variables, constants, functions and classes.

To use the item present inside a module we use the following syntax.


Where, moduleName is the name of the module and item can be a variable, function or class of the module.

The following Python file is a module and it consists of a constant and a function.

# constant

# func
def message():
    return "This is awesome!"

In the following Python file we are using the awesome module.

import awesome

print(awesome.AWESOME_STRING)   # Awesome
print(awesome.message())        # This is awesome!