Modules and Imports
Split code across files cleanly.
Modules and Imports 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.
One File Gets Crowded
As a program grows, packing everything into one file gets messy. Splitting code across files keeps each piece small and focused. 📂
A Module Is a File
In Mojo, a single source file is a module. Every .mojo file you write is a module others can import.
Put Code in a Module
Say you place a helper function in a file named mymath.mojo. That file becomes the mymath module.
fn add(a: Int, b: Int) -> Int:
return a + bImporting the Module
Use the import keyword to pull a whole module in. Now you reach its names through the module name.
import mymath
fn main():
print(mymath.add(2, 3))Import Just What You Need
With from you can import a single name directly, so you call it without the module prefix.
from mymath import add
fn main():
print(add(2, 3))Rename With as
The as keyword gives an import a shorter or clearer local name, which helps avoid clashes.
import mymath as mm
fn main():
print(mm.add(2, 3))Import Several Names
You can list multiple names in one from import, separated by commas, to grab exactly what you use.
from mymath import add, subtractThe Standard Library
Mojo ships modules too. You import from the standard library the same way you import your own code.
from collections import ListModules Aid Reuse
Once code lives in a module, any program can import it. You write a useful function once and reuse it everywhere.
Clear Namespaces
Module names act as a namespace, so two modules can each define add without confusion or collision.
Split With Intent
Group related code into the same module and keep unrelated code apart. Good splits make a project easy to read. 🧩
Quick Check
Let us confirm what counts as a module in Mojo.
Recap
A module is one .mojo file. Use import and from to reuse its code, with as for handy local names. 🎯
Frequently asked questions
Is the “Modules and Imports” lesson free?
Yes — the full text of “Modules and Imports” 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 “Modules and Imports”?
Split code across files cleanly. 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 “Modules and Imports” 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
- Modules and Imports
- Building a .mojopkg
- Public APIs and Structure
- Project Layout Best Practices