Importare un modulo Python
Utilizzi Python.import_module in Mojo.
Importare un modulo Python è una lezione Mojo Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Mojo Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Mojo Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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 Pythonimport_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.piUse 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. 🎯
Domande Frequenti
La lezione «Importare un modulo Python» è gratuita?
Sì — il testo completo di «Importare un modulo Python» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Mojo Academy, passa a CoddyKit PRO. Il corso Mojo Academy include 4 lezioni in totale.
Cosa imparerò in «Importare un modulo Python»?
Utilizzi Python.import_module in Mojo. Eserciti Mojo Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Mojo Academy?
Non è richiesta alcuna esperienza precedente. Mojo Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «Importare un modulo Python»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Mojo Academy?
Sì. Ogni lezione Mojo Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Importare un modulo Python
- Chiamare funzioni Python
- Trasferire dati attraverso il bridge
- Quando usare l'interoperabilità con Python