Version: 1.1.0
Python interoperability
Mojo can import Python modules and can export selected Mojo functions back to Python. The two languages stay distinct.
Essentials
Python.import_moduleloads a module from the active Python environment.- Values that cross the boundary become
PythonObjectunless you convert them. - A missing Python package fails when the call runs, not when Mojo compiles.
- Hot loops should stay in Mojo. Cross the boundary at coarse granularity.
- The Python environment is the one on
PATHwhen you launch Mojo.
Example
from python import Python
def main() raises:
var np = Python.import_module("numpy")
var arr = np.array([1, 2, 3])
print(arr)
Keep in mind
Install the Python package into the same environment as the mojo package before importing it.