Language: EN

que-son-modulos-python

What are and how to use modules in Python

Modules are files that contain Python code, which allow us to divide the code into fragments so that it is more organized and easier to reuse.

A module in Python is simply a file with the extension .py that contains Python code. This code can include definitions of functions, variables, and classes.

Creating a Module

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

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

# operaciones.py

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

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

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

def division(a, b):
    if b != 0:
        return a / b
    else:
        return "Error: División por cero"

Importing a Module

To use the functions and variables of a module in another Python file, we need to import it. We can import an entire module or import only the functions we need.

Importing an Entire Module

import operaciones

resultado_suma = operaciones.suma(5, 3)
print("Resultado de la suma:", resultado_suma)

resultado_resta = operaciones.resta(10, 4)
print("Resultado de la resta:", resultado_resta)

In this example, we import the entire operations module and use the suma and resta functions that are 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 some functions and do not want to load the entire module.

from operaciones import multiplicacion, division

resultado_multiplicacion = multiplicacion(6, 2)
print("Resultado de la multiplicación:", resultado_multiplicacion)

resultado_division = division(15, 3)
print("Resultado de la división:", resultado_division)

In this case, we import only the multiplicacion and division functions from the operations module.

Alias 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 operaciones as ops

resultado_suma = ops.suma(5, 3)
print("Resultado de la suma:", resultado_suma)

from operaciones import resta as restar

resultado_resta = restar(10, 4)
print("Resultado de la resta:", resultado_resta)

Python Standard 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:

  • math: Contains common mathematical functions.
  • random: Allows generating random numbers.
  • datetime: Provides classes for handling dates and times.
  • os: Provides 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.

Example

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

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

# main.py

import operaciones as ops

resultado_suma = ops.suma(5, 3)
print("Resultado de la suma:", resultado_suma)

resultado_resta = ops.resta(10, 4)
print("Resultado de la resta:", resultado_resta)

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