Python Modules

Python modules are files that store functions, classes, and variables. Python has numerous built-in modules, each specialized for various tasks. In this guide, we’ll cover how to create and use modules in Python, including importing techniques, specific imports, aliasing, and exploring built-in modules.

What is a Python Module?

A Python module is a file containing definitions and statements. Modules help organize code logically, making it more understandable and reusable. Each module can define functions, classes, and variables, and it may include executable code.

Creating a Python Module

To create a module in Python, write the necessary code and save it with a .py extension. Here’s a simple example:

Example: operations.py

# Module: operations.py
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

Importing a Module in Python

To use the functions and classes defined in a module, we can import it using the import statement. Python searches for modules in a specific order and imports them if found in the search path.

Syntax:

import module_name

Example:

import operations

print(operations.add(15, 5))

Output:

20

Importing Specific Attributes from a Module

Python’s from statement allows you to import specific parts of a module rather than the whole module.

Example:

# Importing specific functions from math
from math import sqrt, factorial

print(sqrt(25))
print(factorial(5))

Output:

5.0
120

Importing All Names with *

Using the * symbol imports all names from a module into the current namespace. Use this approach cautiously to avoid conflicts.

Syntax:

from module_name import *

Example:

from math import *

print(sqrt(16))
print(factorial(4))

Output:

4.0
24

Locating Python Modules

When importing a module, Python looks through the following paths:

1. Current directory.
2. Directories listed in PYTHONPATH (an environment variable).
3. Installation-specific directories.

Python stores these paths in sys.path for easy access.

import sys
print(sys.path)

Example Output:

['/user/files', '/usr/lib/python3.8', ...]

Renaming a Module with Aliases

You can rename a module upon import using the as keyword.

Syntax:

import module_name as alias_name

Example:

import math as m

print(m.sqrt(25))
print(m.factorial(4))

Output:

5.0
24

Python Built-In Modules

Python provides several built-in modules, including math, random, and datetime. Let’s look at a few examples:

Example with math

import math

print(math.sqrt(36))
print(math.pi)
print(math.degrees(1.57))
print(math.radians(90))
print(math.sin(1.57))
print(math.factorial(5))

Output:

6.0
3.141592653589793
90.0
1.5707963267948966
1.0
120

Example with random

import random

print(random.randint(1, 10))  # Random integer between 1 and 10
print(random.uniform(0, 1))   # Random float between 0 and 1
print(random.choice(['apple', 'banana', 'cherry']))

Output:

7
0.5682323
banana

Example with datetime:

import datetime
from datetime import date
import time

print(time.time())
print(date.fromtimestamp(123456789))

Output:

1698257896.47832
1973-11-29