การนำเข้าโมดูล Python
ใช้ Python.import_module ใน Mojo
การนำเข้าโมดูล Python เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Mojo Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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. 🎯
คำถามที่พบบ่อย
บทเรียน “การนำเข้าโมดูล Python” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การนำเข้าโมดูล Python” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การนำเข้าโมดูล Python”
ใช้ Python.import_module ใน Mojo คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การนำเข้าโมดูล Python” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม
ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การนำเข้าโมดูล Python
- การเรียกฟังก์ชัน Python
- การย้ายข้อมูลข้ามสะพานเชื่อม
- ควรใช้การทำงานร่วมกับ Python เมื่อใด