0Pricing
Mojo Academy · Lektion

Module und Imports

Teilen Sie Code sauber auf mehrere Dateien auf.

Module und Imports ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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 + b

Importing 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, subtract

The Standard Library

Mojo ships modules too. You import from the standard library the same way you import your own code.

from collections import List

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

Häufig gestellte Fragen

Ist die Lektion „Module und Imports“ kostenlos?

Ja — der vollständige Text von „Module und Imports“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Module und Imports“?

Teilen Sie Code sauber auf mehrere Dateien auf. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Mojo Academy zu starten?

Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Module und Imports“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?

Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Module und Imports
  2. Ein .mojopkg erstellen
  3. Öffentliche APIs und Struktur
  4. Bewährte Projektstruktur
← Zurück zu Mojo Academy