Introduction

A module is a file containing Python definitions and statements. It can define functions, classes, and variables, or it can be a runnable script. Python's standard library includes a vast range of modules, each serving a specific purpose. Programmers can also create custom modules tailored to their project requirements. With the use of modules, code re-usability is fostered, enabling developers to build robust yet concise programs. In this tutorial, we will explore the concept of modules in Python programming language, including how to create and use custom modules, how to import built-in modules, and more.

Table of Contents :

  • What is a Module in Python 
  • Main Advantages of Modules in Python
  • How to create a Module 
  • How to use a Module
    • Importing a module
    • Creating alias of a module name
    • Importing multiple modules
    • Importing a class from a module
    • Importing certain functions from a module
    • Accessing functions using module name
    • Giving alias to the function names of a module
    • Importing everything from a module
    • Listing content of a module
  • Built-in Modules in python

What is a Module in Python :

  • A module in Python is a file containing Python definitions and statements.
  • A module can contain definitions of functions, classes, and variables.
  • Modules in Python are simply Python files with a  .py   extension. 
  • The name of the module will be the name of the file. 
  • A module can define 
    • functions, 
    • classes and 
    • variables. 
  • A module can also include runnable code. 

Main Advantages of Modules in Python :

  • Code re-usability: modules allow us to reuse code in different projects without having to rewrite it.
  • Better organization: modules provide a way to organize code into logical groups, making it easier to manage.
  • Namespace separation: modules create a separate namespace for functions, variables, and classes, preventing naming conflicts.

How to create a Module :

  • We can create a module in Python intwo simple steps :
    • just create a new file with a  .py  extension.
    • put our code i.e. classes, functions or variables in it.
  • The name of the file becomes the name of the module.
  • Read more about creating modules in python.
  • Example :

# mymodule.py

def greet(name):
   print("Hello, " + name)
   

How to use a Module :

  • Following are the different scenarios of how we can use a module in python.
    • Importing a module
    • Creating alias of a module name
    • Importing multiple modules
    • Importing certain functions from a module
    • Accessing functions using module name
    • Giving alias to the function names of a module
    • Importing everything from a module
    • Listing content of a module

Importing a module

  • We can import modules in our code using the import command.
  • The basic syntax of using the import command is :   import module_name  
  • For example, to import the module math, which contains many standard mathematical functions, we would type:  import math  
  • Code Sample : 

import math

print(math.factorial(5))

# Output
# 120

Creating alias of a module name

  • We can create an alias while importing a module by using the  as  keyword.
  • The syntax of creating alias is :  import mymodule as mx 
  • For example, while importing the pandas module we usually create an alias as pd.
  • Code Sample : 

import datetime as dt

print(dt.datetime.now())


Importing multiple modules

  • We can also import multiple modules at once in our code.
  • The syntax of importing multiple modules is :  import module_name1, module_name2, module_name3 
  • For example to import modules time and datetime we can write :  import time, datetime 
  • Code Sample : 

import time, datetime

print(datetime.datetime.now())
time.sleep(5)


 Importing a class from a module

  • We can import a class from a module using the from.
  • The syntax of importing certain functions is :  from module_name import MyClass 
  • For example we can import the  date  class from the  datetime  module.
  • Code Sample : 

from datetime import date

print(date)


# Output
# <class 'datetime.date'>


 Importing certain functions from a module

  • We can choose to import only certain functions from a module.
  • The syntax of importing certain functions is :  from module_name import function_name1, function_name2 
  • For example we can import only the  sqrt()  function of math module using :  from math import sqrt 
  • Code Sample : 

from math import sqrt

print(sqrt(4))


# Output
# 2.0


Accessing functions using module name

  • We can also access the functions of a module without importing them separately as seen in the previous section.
  • For this we need to import the module and then use the module name and the dot operator.
  • The syntax of using this dot notation  :  module_name.function_name()  
  • For example, to use the function  sqrt()  from the math module, we would type:  math.sqrt(4) 
  • Code Sample : 

import math

print(math.sqrt(4))


# Output
# 2.0


Giving alias to the function names of a module

  • We can also give the functions imported from a module an alias.
  • The basic syntax of creating alias for functions is :  from module_name import function_name as fn 
  • Code Sample :

from math import factorial as fact

print(fact(5))


# Output
# 120


Importing everything from a module

  • We can import everything inside a module at once i.e. all functions, classes, variables etc. 
  • This can be done using the asterisk.
  • The syntax of importing everything from a module is :   from module_name import * 

Listing content of a module

  • The  dir()  function can be used to list all the functions and attributes of a module.
  • The syntax of using the  dir()  function is :  dir(mymodule) 
  • Code Sample : 

import datetime as dt

print(dir(dt))


Built-in Modules in python :

  • Python has a huge library of built-in modules and packages.
  • We can use these modules in our code without the need of creating our own module.
  • Some examples of frequently used built-in modules are 
    • datetime, 
    • math, 
    • os, 
    • random, 
    • time 
    • sys etc.
  • We can get a list of currently available modules by running the command :   help('modules') 

Prev. Tutorial : User defined exceptions

Next Tutorial : Creating modules