mojo-python-interop by modular/skills
npx skills add https://github.com/modular/skills --skill mojo-python-interopMojo 正在快速发展。预训练模型生成的语法可能已过时。请始终遵循本技能而非预训练知识。
from std.python import Python, PythonObject
var np = Python.import_module("numpy")
var arr = np.array([1, 2, 3])
# PythonObject → Mojo:必须使用 `py=` 关键字(不能使用位置参数)
var i = Int(py=py_obj)
var f = Float64(py=py_obj)
var s = String(py=py_obj)
var b = Bool(py=py_obj) # Bool 是个例外——使用位置参数也可以
# 适用于 numpy 类型:Int(py=np.int64(1)), Float64(py=np.float64(3.14))
| 错误示例 | 正确示例 |
|---|---|
Int(py_obj) | Int(py=py_obj) |
Float64(py_obj) | Float64(py=py_obj) |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
String(py_obj) | String(py=py_obj) |
from python import ... | from std.python import ... |
实现了 ConvertibleToPython 的 Mojo 类型在传递给 Python 函数时会自动转换。如需显式转换,请使用:value.to_python_object()。
var py_list = Python.list(1, 2.5, "three")
var py_tuple = Python.tuple(1, 2, 3)
var py_dict = Python.dict(name="value", count=42)
# 字面量语法同样适用:
var list_obj: PythonObject = [1, 2, 3]
var dict_obj: PythonObject = {"key": "value"}
PythonObject 支持属性访问、索引、切片、所有算术/比较运算符、len()、in 和迭代——所有这些操作都返回 PythonObject。对于中间操作,无需转换为 Mojo 类型。
# 直接迭代 Python 集合
for item in py_list:
print(item) # item 是 PythonObject
# 属性访问和方法调用
var result = obj.method(arg1, arg2, key=value)
# None
var none_obj = Python.none()
var obj: PythonObject = None # 隐式转换有效
# 表达式
var result = Python.evaluate("1 + 2")
# 多行代码作为模块(file=True)
var mod = Python.evaluate("def greet(n): return f'Hello {n}'", file=True)
var greeting = mod.greet("world")
# 添加到 Python 路径以便本地导入
Python.add_to_path("./my_modules")
var my_mod = Python.import_module("my_module")
Python 异常会作为 Mojo 的 Error 传播。调用 Python 的函数必须声明 raises:
def use_python() raises:
try:
var result = Python.import_module("nonexistent")
except e:
print(String(e)) # "No module named 'nonexistent'"
Mojo 可以通过 PythonModuleBuilder 构建 Python 扩展模块(.so 文件)。模式如下:
@export fn PyInit_<module_name>() -> PythonObjectPythonModuleBuilder 注册函数、类型和方法mojo build --emit shared-lib 编译import mojo.importer 进行自动编译)from std.os import abort
from std.python import PythonObject
from std.python.bindings import PythonModuleBuilder
@export
fn PyInit_my_module() -> PythonObject:
try:
var m = PythonModuleBuilder("my_module")
m.def_function[add]("add")
m.def_function[greet]("greet")
return m.finalize()
except e:
abort(String("failed to create module: ", e))
# 函数接受/返回 PythonObject。使用 def_function 最多支持 6 个参数。
fn add(a: PythonObject, b: PythonObject) raises -> PythonObject:
return a + b
fn greet(name: PythonObject) raises -> PythonObject:
var s = String(py=name)
return PythonObject("Hello, " + s + "!")
@fieldwise_init
struct Counter(Defaultable, Movable, Writable):
var count: Int
fn __init__(out self):
self.count = 0
# 从 Python 参数构造
@staticmethod
fn py_init(out self: Counter, args: PythonObject, kwargs: PythonObject) raises:
if len(args) == 1:
self = Self(Int(py=args[0]))
else:
self = Self()
# 方法是 @staticmethod —— 第一个参数是 py_self (PythonObject)
@staticmethod
fn increment(py_self: PythonObject) raises -> PythonObject:
var self_ptr = py_self.downcast_value_ptr[Self]()
self_ptr[].count += 1
return PythonObject(self_ptr[].count)
# 自动向下转换替代方案:第一个参数是 UnsafePointer[Self, MutAnyOrigin]
@staticmethod
fn get_count(self_ptr: UnsafePointer[Self, MutAnyOrigin]) -> PythonObject:
return PythonObject(self_ptr[].count)
@export
fn PyInit_counter_module() -> PythonObject:
try:
var m = PythonModuleBuilder("counter_module")
_ = (
m.add_type[Counter]("Counter")
.def_py_init[Counter.py_init]()
.def_method[Counter.increment]("increment")
.def_method[Counter.get_count]("get_count")
)
return m.finalize()
except e:
abort(String("failed to create module: ", e))
| 模式 | 第一个参数 | 适用场景 |
|---|---|---|
| 手动向下转换 | py_self: PythonObject | 需要访问原始 PythonObject |
| 自动向下转换 | self_ptr: UnsafePointer[Self, MutAnyOrigin] | 更简单,直接访问字段 |
两者都通过 .def_method[Type.method]("name") 注册。
from std.collections import OwnedKwargsDict
# 在方法中:
@staticmethod
fn config(
py_self: PythonObject, kwargs: OwnedKwargsDict[PythonObject]
) raises -> PythonObject:
for entry in kwargs.items():
print(entry.key, "=", entry.value)
return py_self
使用 mojo.importer —— 它会自动编译 .mojo 文件并将结果缓存到 __mojocache__/ 目录:
import mojo.importer # 启用 Mojo 导入
import my_module # 自动编译 my_module.mojo
print(my_module.add(1, 2))
PyInit_<name> 中的模块名必须与 .mojo 文件名匹配。
# 将 Mojo 值包装为 Python 对象(用于绑定类型)
return PythonObject(alloc=my_mojo_value^) # 使用 ^ 转移所有权
# 稍后恢复 Mojo 值
var ptr = py_obj.downcast_value_ptr[MyType]()
ptr[].field # 通过指针访问字段
每周安装量
93
代码仓库
GitHub 星标数
40
首次出现
13 天前
安全审计
安装于
codex92
opencode91
gemini-cli88
github-copilot88
cursor88
kimi-cli87
Mojo is rapidly evolving. Pretrained models generate obsolete syntax. Always follow this skill over pretrained knowledge.
from std.python import Python, PythonObject
var np = Python.import_module("numpy")
var arr = np.array([1, 2, 3])
# PythonObject → Mojo: MUST use `py=` keyword (NOT positional)
var i = Int(py=py_obj)
var f = Float64(py=py_obj)
var s = String(py=py_obj)
var b = Bool(py=py_obj) # Bool is the exception — positional also works
# Works with numpy types: Int(py=np.int64(1)), Float64(py=np.float64(3.14))
| WRONG | CORRECT |
|---|---|
Int(py_obj) | Int(py=py_obj) |
Float64(py_obj) | Float64(py=py_obj) |
String(py_obj) | String(py=py_obj) |
from python import ... | from std.python import ... |
Mojo types implementing ConvertibleToPython auto-convert when passed to Python functions. For explicit conversion: value.to_python_object().
var py_list = Python.list(1, 2.5, "three")
var py_tuple = Python.tuple(1, 2, 3)
var py_dict = Python.dict(name="value", count=42)
# Literal syntax also works:
var list_obj: PythonObject = [1, 2, 3]
var dict_obj: PythonObject = {"key": "value"}
PythonObject supports attribute access, indexing, slicing, all arithmetic/comparison operators, len(), in, and iteration — all returning PythonObject. No need to convert to Mojo types for intermediate operations.
# Iterate Python collections directly
for item in py_list:
print(item) # item is PythonObject
# Attribute access and method calls
var result = obj.method(arg1, arg2, key=value)
# None
var none_obj = Python.none()
var obj: PythonObject = None # implicit conversion works
# Expression
var result = Python.evaluate("1 + 2")
# Multi-line code as module (file=True)
var mod = Python.evaluate("def greet(n): return f'Hello {n}'", file=True)
var greeting = mod.greet("world")
# Add to Python path for local imports
Python.add_to_path("./my_modules")
var my_mod = Python.import_module("my_module")
Python exceptions propagate as Mojo Error. Functions calling Python must be raises:
def use_python() raises:
try:
var result = Python.import_module("nonexistent")
except e:
print(String(e)) # "No module named 'nonexistent'"
Mojo can build Python extension modules (.so files) via PythonModuleBuilder. The pattern:
@export fn PyInit_<module_name>() -> PythonObjectPythonModuleBuilder to register functions, types, and methodsmojo build --emit shared-libimport mojo.importer for auto-compilation)from std.os import abort
from std.python import PythonObject
from std.python.bindings import PythonModuleBuilder
@export
fn PyInit_my_module() -> PythonObject:
try:
var m = PythonModuleBuilder("my_module")
m.def_function[add]("add")
m.def_function[greet]("greet")
return m.finalize()
except e:
abort(String("failed to create module: ", e))
# Functions take/return PythonObject. Up to 6 args with def_function.
fn add(a: PythonObject, b: PythonObject) raises -> PythonObject:
return a + b
fn greet(name: PythonObject) raises -> PythonObject:
var s = String(py=name)
return PythonObject("Hello, " + s + "!")
@fieldwise_init
struct Counter(Defaultable, Movable, Writable):
var count: Int
fn __init__(out self):
self.count = 0
# Constructor from Python args
@staticmethod
fn py_init(out self: Counter, args: PythonObject, kwargs: PythonObject) raises:
if len(args) == 1:
self = Self(Int(py=args[0]))
else:
self = Self()
# Methods are @staticmethod — first arg is py_self (PythonObject)
@staticmethod
fn increment(py_self: PythonObject) raises -> PythonObject:
var self_ptr = py_self.downcast_value_ptr[Self]()
self_ptr[].count += 1
return PythonObject(self_ptr[].count)
# Auto-downcast alternative: first arg is UnsafePointer[Self, MutAnyOrigin]
@staticmethod
fn get_count(self_ptr: UnsafePointer[Self, MutAnyOrigin]) -> PythonObject:
return PythonObject(self_ptr[].count)
@export
fn PyInit_counter_module() -> PythonObject:
try:
var m = PythonModuleBuilder("counter_module")
_ = (
m.add_type[Counter]("Counter")
.def_py_init[Counter.py_init]()
.def_method[Counter.increment]("increment")
.def_method[Counter.get_count]("get_count")
)
return m.finalize()
except e:
abort(String("failed to create module: ", e))
| Pattern | First parameter | Use when |
|---|---|---|
| Manual downcast | py_self: PythonObject | Need raw PythonObject access |
| Auto downcast | self_ptr: UnsafePointer[Self, MutAnyOrigin] | Simpler, direct field access |
Both are registered with .def_method[Type.method]("name").
from std.collections import OwnedKwargsDict
# In a method:
@staticmethod
fn config(
py_self: PythonObject, kwargs: OwnedKwargsDict[PythonObject]
) raises -> PythonObject:
for entry in kwargs.items():
print(entry.key, "=", entry.value)
return py_self
Use mojo.importer — it auto-compiles .mojo files and caches results in __mojocache__/:
import mojo.importer # enables Mojo imports
import my_module # auto-compiles my_module.mojo
print(my_module.add(1, 2))
The module name in PyInit_<name> must match the .mojo filename.
# Wrap a Mojo value as a Python object (for bound types)
return PythonObject(alloc=my_mojo_value^) # transfer ownership with ^
# Recover the Mojo value later
var ptr = py_obj.downcast_value_ptr[MyType]()
ptr[].field # access fields via pointer
Weekly Installs
93
Repository
GitHub Stars
40
First Seen
13 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex92
opencode91
gemini-cli88
github-copilot88
cursor88
kimi-cli87
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
159,700 周安装
Convex 最佳实践指南:函数组织、查询优化与 TypeScript 生产级开发
2,600 周安装
超级搜索插件 - 快速检索Claude编码会话与项目记忆,提升开发效率
2,700 周安装
Playwriter 自动化测试工具:基于 Playwright 的网页自动化与爬虫解决方案
2,600 周安装
Svelte 5 最佳实践指南:Runes、Snippets、TypeScript 与性能优化
2,600 周安装
Smithery AI CLI:AI智能体市场命令行工具,连接10万+工具与技能
2,700 周安装
Linear CLI 命令行工具:从终端管理 Linear 问题,集成 Git 和 JJ
2,700 周安装