Python Modules


In this section we will learn to import and create modules in Python and we will discuss how to use built in modules in Python programming language.

In Python a module can be defined as a file that contains Python statements and definitions. A file that has python modules has an extension .py for example abc.py is a Python module that has python statements and definitions.

Modules are used to break down large programs into smaller ones. Modules help to modify the program or codes easily. Modules increase the readability of programs. The mostly used functions can be imported to modules instead of copying the function into the program repeatedly.

Consider the following example, in which we created a module and saved it with the .py extension:

CODE

>>> def subtract(a, b):

          c = a-b

          return c

return-c

We saved this code with the .py extension. This file is the Python module that subtracts two numbers and returns the result after subtraction by storing it in the variable c. The function subtract () is defined inside the module Subtraction.py.

 

How to Import modules in Python

Modules can be imported inside another module in Python. The import keyword is used to import modules in Python. To import the module defined previously named subtraction, the following statement will be used:

import Subtraction

In Python the functions that are defined in the modules are not directly called. They are called by using the name of the module. The functions in the modules can be accessed by using (.) operation.

Consider the following example in which we have used the subtract function that was defined in the Subtraction module using (.) operation:

CODE

>>> Subtraction.subtract(6,2)

OUTPUT

4

The built in modules can also be imported into the program as the user defined modules are imported.

The following are some of the ways to import a module:

 

Python import statement

A built in module can be imported by using the import keyword. The functions inside the imported module can be accessed by using the (.) operation.

Consider the following example in which we have imported the math module:

CODE

>>> import math

>>> print(“The values of PI is:”, math.pi)

OUTPUT

The values of PI is: 3.141592653589793

the-values

 

Import with renaming

In Python a module can be imported by renaming. A module can be imported by renaming its name by using the as keyword. This is illustrated below:

CODE

CODE

>>> import math as x

>>> print(“The values of PI is:”, x.pi)

OUTPUT

The values of PI is: 3.141592653589793

pi

In the above example, math is renamed to be x by using the as keyword. This is advantageous when the user wants to save time. Now math is not in the scope instead x will be recognized as the module name. If we write math.pi now then an error message will appear.

 

Python from… import statement

In Python we can import the specific names from a module by using the ‘from’ keyword with the import keyword. Through this we do not need to import the whole module.

Consider the following example in which pi is imported from the math module:

CODE

>>> from math import pi

>>> print(“The value of PI is “,pi)

OUTPUT

The value of PI is 3.141592653589793

single-attribute

In the above example, we only imported a single attribute named pi from the module named math. In this way the use of dot (.) operation is not needed.

Consider the following example, in which we are importing multiple attributes using from keyword:

CODE

>>> from math import pi, e

>>> print(“The value of PI is”,pi)

>>> print(“The value of e is”,e)

OUTPUT

The value of PI is 3.141592653589793

The value of e is 2.718281828459045

the-value-of-e

In the above example, multiple attributes are imported from the module math. The names of these attributes are separated by comma. And then the values are printed using print statement.

 

Import all names

All names or definitions can be imported from a module by using the import keyword along with an asterisk (*).

Consider the following example in which we have imported all names (definitions) from the module:

CODE

>>> from math import *

>>> print(“The value of PI is”,pi)

>>> print(“The values of e is”,e)

OUTPUT

The value of PI is 3.141592653589793

The values of e is 2.718281828459045

above-example

It can be seen in the above example, that all the names or definitions are imported from the module math using *. In this way we can use all attributes in module math like we printed the value of pi and e.

 

Python Module search path

When interpreter is given instructions to import a module, it first looks for the built in modules, if the built in modules are not found then the interpreter finds it into the list of directories that are defined in sys.path. The search made by interpreter is in the following order:

  1. Current directory
  2. PYTHONPATH
  3. Installation dependent default directory.

Consider the following example, which is demonstrating the sys.path by importing sys using the import keyword:

CODE

>>> import sys

>>> sys.path

OUTPUT

[”, ‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ Lib \\ idlelib’,
‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ python35.zip’,
‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ DLLs’,
‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ lib’,
‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32’,
‘C: \\ Users \\ admin \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ lib \\ site-packages’]

import-sys

 

Reloading a Module

The interpreter of Python imports only one module during a session.

Let’s suppose that we have the following code in our module named myModule.

print (“MODULE”)

Consider the following example in which multiple import keywords are used to import multiple modules:

CODE

>>> import myModule

MODULE

>>> import myModule

>>> import myModule

From the above example we can conclude that the code is executed only once that is the module is executed only once. The program will be reloaded if you want to import another module. This can be done by restarting the interpreter. Restarting the interpreter is not a good idea. We there is a much better and neat way to do this. That is we can use the reload () function to reload a module.

Consider the following example, in which reload () function is used to reload the module:

CODE

>>> import imp

>>> import myModule

MODULE

import myModule

imp.reload (myModule)

MODULE

<module ‘myModule’ from ‘.\\myModule.py’>

 

The dir () built in function

The built in dir () function is used to find the names that are defined inside a module.

Consider the following example, in which the dir () function is used with the function subtract () that we added in the module subtraction.

CODE

>> dir(subtract)

OUTPUT

[‘__annotations__’, ‘__call__’, ‘__class__’, ‘__closure__’, ‘__code__’, ‘__defaults__’, ‘__delattr__’, ‘__dict__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__get__’, ‘__getattribute__’, ‘__globals__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__kwdefaults__’, ‘__le__’, ‘__lt__’, ‘__module__’, ‘__name__’, ‘__ne__’, ‘__new__’, ‘__qualname__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’]

dir

In the above example, the dir () function is used with the function subtract () that we added in the module subtraction. This will result in the sorted list of names along with subtract, all of these names begin with an underscore and these names are the default Python attributes that are associated with the module. Note that these attributes are not defined by us, they are default attributes.