mojo-python-interop

Bridge Mojo and Python by importing modules, converting values, and invoking Python APIs.

6|Updated Sep 26, 2024
One-click install
npx skills add https://github.com/better-mojo/uuid --skill mojo-python-interop-better-mojo
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: mojo-python-interop
Source: https://github.com/better-mojo/uuid/tree/main/.agents/skills/mojo-python-interop
Command: npx skills add https://github.com/better-mojo/uuid --skill mojo-python-interop-better-mojo

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It solves the friction of integrating Mojo and Python by standardizing how you import Python code, convert values, handle Python exceptions, and expose Mojo functionality to Python.

Core Features & Use Cases

  • Python from Mojo: Import Python modules, create Python values, and run Python expressions or modules via evaluation.
  • Type conversions and interoperability: Convert Mojo primitives to Python objects using the correct py= keyword, and use ConvertibleToPython for automatic conversions or explicit to_python_object() when needed.
  • PythonObject workflow: Use PythonObject for attribute access, calls, operators, indexing/slicing, iteration, and len without unnecessary conversions.
  • Python extension modules: Build shared libraries using PythonModuleBuilder and export PyInit_<module_name> entry points with registered functions and types.

Quick Start

Use mojo-python-interop to call Python’s numpy from Mojo, pass arguments using py= where required, and catch Python exceptions as Mojo Error using raises.

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 functions from Mojo?

To call Python libraries from Mojo, import the target Python module, create Python values, and invoke APIs directly. Use the py= keyword to wrap Mojo primitives and handle Python exceptions as Mojo errors using the raises convention.

How does exception handling work when calling Python code from Mojo?

Exception handling for Python interop in Mojo uses a raises-based convention to catch Python exceptions as Mojo errors. This standardizes error propagation, ensuring failures in Python expressions or module invocations surface reliably in Mojo code.

Do I need manual type conversion for Mojo primitives when passing them to Python?

Yes, Mojo primitives require type conversion using the py= keyword when passed to Python APIs, except for Bool types which use positional arguments. You can also implement ConvertibleToPython for automatic conversions or call to_python_object() explicitly.

How do I build a Python extension module in Mojo?

To build a Python extension module in Mojo, use PythonModuleBuilder patterns to construct a shared library. Export a PyInit_<module_name> entry point and register your Mojo functions and types so Python can import and execute them natively.

Can I use PythonObject for slicing and iteration without converting back to Mojo types?

Yes, PythonObject supports direct attribute access, calls, operators, indexing, slicing, iteration, and len operations. This allows you to manipulate Python collections and values natively in Mojo without triggering unnecessary type conversions.