[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Modules in Python

Published on
8,320 Points
Last Modified:
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it.

Module is an object with arbitrarily named attributes which can be used in binding and referencing.

We can define a function, classes and variables inside a module; it can also have the runnable code.

Example: There is a function called "FLOOR" which is used to round the value for the given input to it. This function is inside a module that allows us to use it.

Let us check if we are able to use this function or not:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> floor(17.5)

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
NameError: name 'floor' is not defined

Open in new window

Here from the above we can see that the error as this function is not present on my Python because we have not imported the Module which is holding this function.

Let us import the module i.e. "MATH" which has "FLOOR" function.

>>> import math

Open in new window

Now let us use the "FLOOR" function again and check if we are able to use it:
>>> floor(17.5)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
NameError: name 'floor' is not defined

Open in new window

But still we see the same error because we cannot use the function like the above statement, in order to use we must provide the module name at the beginning and then the function name to it as like below:

>>> math.floor(17.5)

Open in new window

So now we are able to use the "FLOOR" function.

To check what are the other modules present in the math module we can hit ">>> math." and hold for a sec it will display all the functions associated with it. Or by using "dir()" which is a built-in function which will return the list of the names defined inside a module as like below:

>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

Open in new window

So from the above list we can see what are the functions that math module have, now we can use it inside our code whenever it is required.

Now let us see how we can assign it to an variable, so that we don’t have to use " module.function<<agruments>> " every time.

>>> var1 =math.floor
>>> var1(17.5)

Open in new window

So now we can use "var1" variable in our code instead of writing "math.floor" every time.

We can also created our own module where we can keep specific function and variables that we have declared or want to make use of the same on other scripts , and also we can keep that to use the same in our future reference.  Like when we close or exit Python IDLE the variables and functions which we have use will be lost unless we save them in some file or as a script.

So the best way is to create a module and keep them and use in future as per our need. It is also helpful when we create a function and have it on the module, so that we can make use of the same functions multiple times.

In order to create modules navigate to File on Python IDLE and then click on New Window or you can use the shortcut Ctrl+N.

The below code does nothing bust just to print a line which is very basic and a simple.

    def module1():
    print " It really worked "

Open in new window

Simple example of a module
Now click on “Save As” option under “File”. Save it on the same directory where Python is installed.

For me it’s on: C:\Python27

Save the module on directory where Python is installed
Now give any name to your script with python extension.

Let us give as “mod1.py”

Give a name and save the file
Now click ok “Save”.
Note: Always remember the name of the file which is your module name.
Now go to Python IDLE and import the module.

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import mod1

Open in new window

Make sure you do not enter the extension which we have used earlier while saving the module.

Now once imported we need to call the module for which we need to call the object or else we can directly call like as below:

>>> mod1.module1()
 It really worked 

Open in new window

So now if we can make many functions and use the variables on the one module which is little bit similar to packages in oracle.

For getting more information on modules in PYTHON we can go to Python official website i.e. http://docs.python.org/tutorial/modules.html#
Author:Swadhin Ray

Featured Post

Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Join & Write a Comment

Suggested Articles

Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Next Article:

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month