mojo-python-interop

Write Mojo code that calls Python libraries and exposes Mojo types as Python extension modules.

Updated Sep 15, 2026
One-click install
npx skills add https://github.com/shakfu/mdsp --skill mojo-python-interop-shakfu
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: mojo-python-interop
Source: https://github.com/shakfu/mdsp/tree/main/.claude/skills/mojo-python-interop
Command: npx skills add https://github.com/shakfu/mdsp --skill mojo-python-interop-shakfu

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Pretrained models generate obsolete Mojo syntax for Python interoperability, producing code that fails to compile. This Skill provides current, correct patterns for bidirectional Mojo-Python interop so generated code works with the latest Mojo toolchain. ## Core Features & Use Cases - Python from Mojo: Import Python modules, convert between PythonObject and Mojo types with the py= keyword, build Python collections, evaluate Python code, and handle exceptions as Mojo Error. - Mojo from Python: Build Python extension modules with PythonModuleBuilder, export functions and types with methods, support kwargs, and auto-compile via mojo.importer. - Use Case: You want to expose a high-performance Mojo Counter struct to Python. The Skill shows the PyInit_<module> export pattern, def_method registration with manual or auto downcast, and compilation with mojo build --emit shared-lib. ## Quick Start Write a Mojo Python extension module that exports an add function and show me how to import it from Python using mojo.importer.

Frequently Asked Questions about mojo-python-interop

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I call Python libraries like numpy from Mojo?▼

Import Python modules in Mojo using `from std.python import Python` and `Python.import_module("numpy")`. The returned PythonObject supports attribute access, method calls, indexing, and iteration without converting to Mojo types.

How do I build a Python extension module in Mojo?▼

Define an `@export def PyInit_<module_name>() abi("C") -> PythonObject` function that uses PythonModuleBuilder to register functions and types, then compile with `mojo build --emit shared-lib`. Alternatively, use `import mojo.importer` in Python to auto-compile the .mojo file.

Why does Int(py_obj) fail to compile in Mojo?▼

Converting a PythonObject to a Mojo type requires the `py=` keyword argument, not a positional argument. Use `Int(py=py_obj)`, `Float64(py=py_obj)`, or `String(py=py_obj)`; Bool is the exception where positional also works.

Can a Mojo shared library contain a main function?▼

No. When building a shared library with `mojo build --emit shared-lib` or via mojo.importer, the compiler rejects files containing a `main()` function. Keep test or CLI code in a separate Mojo file.

How do I export a Mojo struct with methods to Python?▼

Register the struct with `m.add_type[MyType]("Name")`, add a constructor via `.def_py_init[...]()`, and register methods with `.def_method[Type.method]("name")`. Methods are @staticmethod taking either `py_self: PythonObject` or `UnsafePointer[Self, MutAnyOrigin]` for auto-downcast.