Friday, January 16, 2026

Python Modules.

 

Python Modules

Last Updated : 10 Jan, 2026

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 Module

To create a Python module, write the desired code and save that in a file with .py extension.

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

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. Below is the syntax to import a module:

import module

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

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.


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.


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.


Output
3.141592653589793

Explanation: math is accessed through the shorter alias m.

Types of Modules

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.


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.

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.

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

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

If utils.py contains something like:

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.


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.


No comments:

Post a Comment

Python Dictionary

  Removing Dictionary Items Dictionary items can be removed using built-in deletion methods that work on keys: del :  removes an item using ...