Language: EN

que-son-modulos-python

What are and how to use modules in Python

Modules are files that contain code Python, which allow us to divide the code into multiple files in a way that makes it more organized and easier to reuse code.

A module in Python is simply a file with a .py extension that contains Python code (within this file, we can include definitions of functions, variables, classes, etc).

Creating a module

To create a module in Python, we simply create a file with a .py extension and write our code in it.

For example, we can create a module called operations.py with some mathematical operation functions:

# operations.py

def add(a, b):
    return a + b

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

def multiply(a, b):
    return a * b

def divide(a, b):
    if b != 0:
        return a / b
    else:
        return "Error: Division by zero"

Importing a Module

To use the functions and variables of a module in another Python file, we first need to import it.

We can import a complete module or import only the functions we need.

Importing a complete module

One way to import a module in another is to import it completely (which would import all the functions and definitions it contains)

import operations

result_add = operations.add(5, 3)
print("Result of the addition:", result_add)

result_subtract = operations.subtract(10, 4)
print("Result of the subtraction:", result_subtract)

In this example, we import the complete operations module and use the add and subtract functions defined in that module.

Importing specific functions from a module

We can also import specific functions from a module (which can be useful if we only need a few functions and do not want to load the entire module).

from operations import multiply, divide

result_multiply = multiply(6, 2)
print("Result of the multiplication:", result_multiply)

result_divide = divide(15, 3)
print("Result of the division:", result_divide)

In this case, we only import the multiply and divide functions from the operations module.

Aliases in Importing

We can also use aliases when importing modules or functions. This allows us to use a shorter or more convenient name in our code.

import operations as ops

result_add = ops.add(5, 3)
print("Result of the addition:", result_add)

from operations import subtract as subtract

result_subtract = subtract(10, 4)
print("Result of the subtraction:", result_subtract)

Standard Python Modules

In addition to creating our own modules, Python includes a standard library with a wide variety of useful modules for various tasks. Some examples are:

ModuleDescription
mathContains common mathematical functions
randomAllows generating random numbers
datetimeProvides classes for handling dates and times
osProvides functions for interacting with the operating system

To use these modules, we simply import them in the same way we did with our own operations module.

Basic Example

In many cases, it is common to have a main file that acts as an entry point to our program. This file can import the necessary modules and execute the main code.

For example, let’s suppose we have a file main.py that imports and uses the functions from the operations module.

# main.py

import operations as ops

result_add = ops.add(5, 3)
print("Result of the addition:", result_add)

result_subtract = ops.subtract(10, 4)
print("Result of the subtraction:", result_subtract)

When executing main.py, the functions from the operations module will be imported and the results of the addition and subtraction will be printed.