Version: 1.1.0
Calling Mojo from Python
Mojo functions can be exported so Python can import them like a native extension.
Essentials
- Mark the function for export and build the package with the Mojo tool.
- Stick to types that have a Python equivalent at the boundary: numbers, strings, and exported structs.
- The Python process owns the call. Mojo code runs underneath it.
- Raise Mojo errors to fail the Python call.
Example
from python import PythonObject
def add(a: PythonObject, b: PythonObject) -> PythonObject:
return a + b
Keep in mind
Export a small surface. Large object graphs are easier to keep on one side of the boundary.