0Pricing
Mojo Academy · Lesson

Importing a Python Module

Use Python.import_module in Mojo.

Importing a Python Module is a free Mojo Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

Is the “Importing a Python Module” lesson free?

Yes — the full text of “Importing a Python Module” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Importing a Python Module”?

Use Python.import_module in Mojo. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Importing a Python Module” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Mojo Academy lesson?

Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Importing a Python Module
  2. Calling Python Functions
  3. Moving Data Across the Bridge
  4. When to Use Python Interop
← Back to Mojo Academy