0Pricing
Mojo Academy · Lección

Importar un módulo de Python

Use Python.import_module en Mojo.

Importar un módulo de Python es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

Keep Your Favorites

Mojo lets you reach into Python's huge ecosystem, so you keep using the libraries you already love while gaining speed. 🐍

The Python Object

Interop starts with the built-in Python object, your gateway to importing and calling Python from inside Mojo code.

from python import Python

import_module

To load a Python module, call Python.import_module with the module name as a string. It hands you the module back.

var np = Python.import_module("numpy")

Name It Like Python

The string you pass is exactly the name you would write in a Python import, like "math" or "numpy". Same names, same modules.

var math = Python.import_module("math")

Store the Handle

Save the returned module in a var so you can use it many times. That variable is your handle to everything the module offers.

var os = Python.import_module("os")

It Can Fail

If a module is missing, the import can raise an error. So importing usually lives in a function marked with raises.

fn load() raises:
    var np = Python.import_module("numpy")

Reach the Contents

Once imported, use dot access to reach functions and values inside the module, just like in Python itself.

var pi = math.pi

Use Installed Packages

You can import any third-party package that is installed in your Python environment, not only the standard library.

var pd = Python.import_module("pandas")

One Bridge, Many Modules

Import as many modules as you need. Each call returns its own handle, letting you mix several Python libraries in one Mojo file.

Environment Matters

Mojo uses your active Python environment to find modules. If a package imports in Python, it will import in Mojo too.

Why It Matters

Importing Python means you never start from zero: decades of tested tooling are one line away inside your fast Mojo code. ⚡

Quick Check

Recall how you bring a Python module into Mojo.

Recap

You learned to import Python with Python.import_module, store the handle, and unlock any installed library from Mojo. 🎯

Preguntas frecuentes

¿La lección «Importar un módulo de Python» es gratis?

Sí — el texto completo de «Importar un módulo de Python» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Importar un módulo de Python»?

Use Python.import_module en Mojo. Practicas Mojo Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Mojo Academy?

No se requiere experiencia previa. Mojo Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.

¿Cuánto tiempo toma la lección «Importar un módulo de Python»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Mojo Academy?

Sí. Cada lección de Mojo Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Importar un módulo de Python
  2. Llamar a funciones de Python
  3. Mover datos entre ambos entornos
  4. Cuándo usar la interoperabilidad con Python
← Volver a Mojo Academy