Python Modules (original) (raw)

Module 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 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.

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 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.

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))

`

**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))

`

**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)

`

**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.

Python `

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

`

**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.subtract(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)

`