Thanks to visit codestin.com
Credit goes to www.geeksforgeeks.org

Open In App

Python Modules

Last Updated : 20 Nov, 2025
Comments
Improve
Suggest changes
154 Likes
Like
Report

A Module in python is a file containing definitions and statements. A module can define functions, classes and variables. Modules help organize code into separate files so that programs become easier to maintain and reuse. Instead of writing everything in one place, related functionality can be grouped into its own module and imported whenever needed.

Create a Python Module

To create a Python module, write the desired code and save that in a file with .py extension. Let's understand it better with an example:

Example: Let's create a calc.py in which we define two functions, one add and another subtract.

Python
# calc.py
def add(x, y):
    return (x+y)

def subtract(x, y):
    return (x-y)

This is all that is required to create a module.

Import module

Modules can be used in another Python file using the import statement. When Python sees an import, it loads the module if it exists in the interpreter’s search path.

Syntax:

import module

Example: Now, we are importing the calc that we created earlier to perform add operation.

Python
import calc
print(calc.add(10, 2))

Output

12

Explanation: import calc loads the module and calc.add() accesses a function through dot notation.

Types of Import Statements

1. Import From Module: This allows importing specific functions, classes, or variables rather than the whole module.

Python
from math import sqrt, factorial
print(sqrt(16))
print(factorial(6))

Output
4.0
720

Explanation: Only sqrt and factorial are brought into the local namespace, so the prefix math. is not required.

2. Import All Names: * imports everything from a module into the current namespace.

Python
from math import *
print(sqrt(16))
print(factorial(6))

Output
4.0
720

Explanation: Every public name of math becomes directly accessible. (Not recommended in large projects due to namespace conflicts.)

3. Import With Alias: You can shorten a module’s name using as.

Python
import math as m
print(m.pi)

Output
3.141592653589793

Explanation: math is accessed through the shorter alias m.

Types of Modules in Python

Python provides several kinds of modules. Each type plays a different role in application development.

1. Built-in Modules: These come bundled with Python and require no installation - e.g., math, random, os.

Python
import random
print(random.randint(1, 5))

Output
4

Explanation: random.randint() returns a random number within the given range.

2. User-Defined Modules: These are modules you create yourself, such as calc.py.

Python
import calc
print(calc.sub(20, 5))

Output

15

Explanation: The module is created manually and then imported into another script.

3. External (Third-Party) Modules: These modules are installed using pip - e.g., NumPy, Pandas, Requests.

Python
import requests
r = requests.get("https://example.com")
print(r.status_code)

Output

200

Explanation: requests is installed separately (pip install requests) and provides HTTP utilities.

4. Package Modules: A package is a directory containing multiple modules, usually with an __init__.py file.

Example Directory

mypkg/
__init__.py
calc.py
utils.py

Using a module from a package

Python
from mypkg import utils
print(utils.some_func())

calls a function named some_func(), the output will be whatever that function returns.

If utils.py contains something like:

Python
def some_func():
    return "Hello"

Output

Hello

Locating a Module

Python searches for modules in a predefined list of directories known as the module search path. You can view this list using sys.path.

Python
import sys
for p in sys.path:
    print(p)

Output
/home/guest/sandbox
/usr/local/lib/python313.zip
/usr/local/lib/python3.13
/usr/local/lib/python3.13/lib-dynload
/usr/local/lib/python3.13/site-packages

Explanation: Python checks each path in order until it finds the module you’re trying to import.

Also Read:

Suggested Quiz
3 Questions
Which of the following is not an advantage of using modules?
  • A
    Provides a means of reuse of program code
  • B
    Provides a means of dividing up tasks
  • C
    Provides a means of reducing the size of the program
  • D
    Provides a means of testing individual parts of the program
Explanation:
The total size of the program remains the same regardless of whether modules are used or not. Modules simply divide the program."

Which of the following is used to import modules in Python?

  • A

    import

  • B

    include

  • C

    require

  • D

    using

Explanation:

The import keyword is used to bring modules or their contents into a Python program.

What is the main purpose of using modules in Python?

  • A

    To avoid using classes

  • B

    To write programs in Java

  • C

    To organize reusable code in separate files

  • D

    To increase the speed of the Python interpreter

Explanation:

Modules help structure and reuse code by keeping functions, classes, and variables in separate files.

Quiz Completed Successfully
Your Score :   2/3
Accuracy :  0%
Login to View Explanation
1/3 1/3 < Previous Next >

Article Tags :

Explore