Introduction to Python Modules

Modules are the pre-defined files that contain the python codes which depict the basic functionalities of class, methods, variables, etc. It consists of different functions, classes in a group of files inside a directory. Modules can also be termed as Libraries. These are basically the pre-defined methods that can be used to make the code more efficient and reduce redundancy.

Modules bind the code and reduce the repetitions of functions frequently used in the code. Thus, it makes the code much clear and easy to understand.

Examples:

  • OS
  • Time
  • Math
  • MatPlotlib

Mechanism of Python Modules

Python Module Mechansim
Python Module Mechanism

The moment a module is imported through a program, Python Interpreter fetches the module from either of the following locations:

  • Program Directory
  • The directory in the PYTHONPATH variable
  • Default directory

Listing of Modules

The list of available modules in Python can be found out by executing the following command in the command prompt (interpreter shell).

>>> help(“module”)

Python Module List
Python Module List

Importing modules from Python Standard path

Syntax:

import module_name

Example:

import math


Importing Modules from other Sources

To fetch and use modules from other and new sources, we need to install Python PIP.

Python pip is a software that installs python modules from index or using a manager like Anaconda.

Run the following command to install modules from new sources using python pip:

python -m pip3 install module_name

Run the following command to install modules from new sources using Ananconda:

conda install module_name

Example: Steps to install numpy

python -m pip3 install numpy
conda install numpy
sudo apt install python3-numpy


Example: Built-in Modules

import math 

print (math.sqrt(121)) 

print (math.pi) 

print (dir(math))

Output:

11.0
3.141592653589793
[‘doc’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘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’, ‘isf
inite’, ‘isinf’, ‘isnan’, ‘ldexp’, ‘lgamma’, ‘log’, ‘log10’, ‘log1p’, ‘log2’, ‘modf’, ‘pi’, ‘pow’, ‘radians’, ‘sin’, ‘sinh’, ‘sqrt’, ‘tan’, ‘t
anh’, ‘trunc’]

In the above example, dir() method gives the function name, variables, etc in the math module.


Variable in a Module

Apart from methods and classes, A module can also contain variables.

Example:

Fruit = {
  "name": "Apple",
  "color": "Green"
}

Save the above snippet of code in the file Module1.py

import Module1

x = Module1.Fruit["name"]
print(x)

Output:

Apple

In the above piece of code, Module1 is imported and functionality is performed on it.


Difference between a module and a package in Python

Python Module: These are set of pre-defined files that contain the python codes which depict the basic functionalities of class, methods, variables, etc.

Python Package: It is a directory that holds and contains modules and sub-packages.


References