Python Modules

In this article, we are going to learn about python modules. How to write a python module. Different ways of importing a python module into a program. The most commonly used built-in python modules and how search path works.

What is python module?

The python module is a python file containing python definitions and executable statements. The biggest advantage of the python module is we can use any python program readily into our program without actually copying the code definitions.

When a program gets bigger then it becomes very difficult to organize and maintain it. So we may want to split it into different manageable and organized files. This breaking down of python code into different files is made possible by python modules.

Splitting of a big single python program into separate python modules helps us in many ways like;

  • It becomes easy to read the code and understand it.
  • Maintenance of neatly grouped code is easy.
  • Python module makes easy re-usability of frequently used code. We can group frequently used code into a module. So next time instead of copying the actual code definition into a program, we can just import the module.
  • Also, there are many python built-in modules available that can be used into our code.
How to a write a python module. Different ways of importing a python module into a program. Most commonly used built-in python modules and how search path works.

Write a python module:

The python module is nothing but a python file with definitions and executable statements in it.

Suppose we are having a python file named “” with some python definitions in it, Then the file ( will be called as a module and the name of module will be the name of a file (custom_module).

So for learning purpose let’s create a sample module file which performs basic mathematical operations. Save it with the name “

# Function "addition" performs addition of two numbers a and b and returns the sum it to its caller.
def addition(a, b):
add = a + b
return add

def subtraction(a, b):
sub = a - b
return sub

def multiplication(a, b):
multiply = a * b
return multiply

def division(a, b):
divide = a / b
return divide

We have written our first module “” so the name of the module becomes “basic_math_operations“.

How to import a python module?

For importing any module in a file just write keyword “import “followed by the name of the module.

Now let’s try to import module “” in a new python file and try to use the function “addition” from the imported module.

import basic_math_operations
Result = basic_math_operations.addition(5, 8)

#Output: 13

How to access the functions from a python module?

In the above example, we have imported “basic_math_operations” by using import keyword. Now by using the module name along with the dot operator, we can access the functions from the imported module.

So the construct for accessing a function from an imported module is “module name.function name”. In the above example to access addition function, we have written “basic_math_operations.addition(5, 8)“.

Rename a module while importing:

While importing a module, instead of importing it with its actual name we can create an alias (rename) for it. To create an alias (rename) for a module while importing we have to use “as” keyword.

There is no difference in behavior if we rename a module while importing. The only benefit of renaming is to save the typing time.

import basic_math_operations as basicMath
print(basicMath.addition(5, 8))


Here we have imported module named “basic_math_operations” with a new name as “basicMath“.

After creating an alias it is mandatory to use the alias only. We are not supposed to use the original module name. Due to this fact “basic_math_operations.addition()” becomes an invalid statement. Correct statement is “basicMath.addtion()“.

Import only required statements from module:

Sometimes instead of importing a complete module into a program, we may want to import only the required statements. We can do so by using the following syntax;

from <name of module> import <statement>

from basic_math_operations import multiplication, division

# Valid statements
print(multiplication(5, 4))
print(division(12, 4)

# Invalid statement since we haven't imported addition function from basic_math_operations module.
print(addition(9, 6))

Here from the module, we have imported only two functions i.e multiplication, division.

In the above example though “addition()” function is present in “basic_math_operations” module but since we haven’t imported it in our program so we’ll not be able to use it. It will not be able to identify the “addition()” function and will show us a ‘NameError’.

Import all names:

We can import all the definitions from a module using “*” (asterisk).

from basic_math_operations import *

print(multiplication(5, 4))
print(division(12, 4)
print(addition(9, 6))


Use of asterisk after import keyword imports all the definitions and statements from module except those beginning with underscore “_” character.

This is not a good programming practice to import all the module definitions using the asterisk (*). It makes code poorly readable.

Python built-in modules:

In python, there are many built-in python modules available which we can use readily. You can check the complete python built-in modules list here. These modules are present in your python installation directory\lib location.

These built-in modules can be imported in same way like we have imported our custom module.

Let’s see one example of how we can import and use python built-in module. Here we are going to import the python built-in “calendar” module and check if the year is a leap year or not.

import calendar


In the above sample program, the Statement “import calendar” imports the calendar module into our program. Within the calendar module “.isleap()” function available to check if the year is a leap year or not. this function returns the result into a Boolean format i.e if the year is leap it returns “True” otherwise it returns the value “False”.

Python module search path:

Until now we have seen how to import a module in python program. But we have nowhere mentioned the location of the module file. We have just mentioned the “import” keyword followed by the module name. So how python interpreter finds the module file to import all the definitions from it.

There are several locations in which python interpreter searches for the module. These locations are known as search paths.

Suppose we have imported a module “basic_math_operations“, So the interpreter first checks for this module in the built-in module directory. If not found then it starts to search for ”” file into the list of directories defined by environment variable sys.path

sys.path variable has following locations by default:

  • The directory containing the input script (or the current directory when no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.

In sys.path we can add our own path in as well.

Reloading a module during execution:

Python interpreter imports the module only once during the program execution. Even if we write the multiple import statements for a single module, the Interpreter imports the module only once.

Now we are facing a problem here, We have imported a module that gets changed during the course of program execution and we want to import these changes as well. Since re-importing of a module is not possible.

To solve this issue python has provided a built-in function “reload()”. This function available in “importlib” module (Earlier known as “imp” module).
As the name suggests function “reload()” performs reloading of the specified module during the course of program execution.

import importlib
import basic_math_operations

print(Test.multiplication(5, 4))
print(Test.division(12, 4))


print(Test.addition(9, 6))

Built-in dir() function

Python built-in “dir()” function is used to find out the names of definitions present inside the imported modules.

import basic_math_operations


We can see that, “dir()” function returns us all the names present in a module. All the names those start and end with double underscore (_) character are the python default attributes.

Continue Reading:

Leave a Comment