Importando um módulo Python
Use Python.import_module em Mojo.
Importando um módulo Python é uma aula grátis de Mojo Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Mojo Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Mojo Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em 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 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. 🎯
Perguntas Frequentes
A aula “Importando um módulo Python” é grátis?
Sim — o texto completo de “Importando um módulo Python” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Mojo Academy, atualize para CoddyKit PRO. O curso de Mojo Academy inclui 4 aulas no total.
O que vou aprender em “Importando um módulo Python”?
Use Python.import_module em Mojo. Você pratica Mojo Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Mojo Academy?
Nenhuma experiência prévia é necessária. Mojo Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.
Quanto tempo leva a aula “Importando um módulo Python”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Mojo Academy?
Sim. Cada aula de Mojo Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Importando um módulo Python
- Chamando funções Python
- Movendo dados pela ponte
- Quando usar a interoperabilidade com Python