向PyPI发布WASM wheels以用于Pyodide

Simon Willison's Blog 新闻

摘要

PyPI现在支持为Pyodide发布WebAssembly wheels,允许包维护者直接分发他们自己的WASM包。本文以luau-wasm包为例进行演示。

暂无内容
查看原文
查看缓存全文

缓存时间: 2026/06/14 07:34

# 将WASM wheels发布到PyPI以供Pyodide使用 来源:https://simonwillison.net/2026/Jun/13/publishing-wasm-wheels/ 2026年6月13日 ThePyodide 314.0 版本发布公告 (https://blog.pyodide.org/posts/314-release/)(viaHacker News (https://news.ycombinator.com/item?id=48462759)) 中包含了一个我期待已久的消息: > 现在,你可以将为 Pyodide(或任何兼容 PEP 783 (https://pyodide.org/en/stable/development/abi.html) 定义的 PyEmscripten 平台的 Python 运行时)构建的 Python 包直接发布到 PyPI,并在运行时安装。在此之前,Pyodide 维护者需要自行维护、构建和托管超过 300 个包。这给维护者带来了巨大负担,并成为社区的主要瓶颈,因为每个新包都需要人工审核。展望未来,包维护者可以像为 Linux、macOS 或 Windows 构建原生 wheels 一样,只需为 Pyodide 构建 wheel 并发布到 PyPI。 这是支持这一功能的PyPI 自身 PR (https://github.com/pypi/warehouse/pull/19804),已于 4 月 21 日合并。 我非常喜欢Pyodide (https://pyodide.org/),过去也曾因这个限制而感到沮丧。将 C 或 Rust 扩展编译为 WASM 并打包到 wheel 文件中是可能的,但在此之前并没有简单的分发方式。 感谢许多人的努力,现在这个问题终于解决了! #### 用 luau\-wasm 试试看 我决定找一个可以打包的东西来庆祝一下。我手头有不少实验性的 Pyodide 项目,但最合适的似乎是 3 月 9 日的那个Luau WebAssembly 研究草稿 (https://github.com/simonw/research/tree/main/pluau-wasm-pyodide#readme)。 Luau (https://luau.org/) 是一个“基于 Lua 的、小巧、快速且可嵌入的编程语言,具有渐进类型系统”,由Roblox (https://luau.org/news/2022-11-04-luau-origins-and-evolution/) 开发,并以 MIT 许可证发布。 它是用 C++ 编写的。我之前已经知道可以将其编译为 WebAssembly 并在 Pyodide 中运行,所以我让Codex + GPT-5.5 xhigh (https://gist.github.com/simonw/1761eab6ba11d4053f56f955a28ad76b) 负责将我的实验打包并发布到 PyPI,使用 GitHub Actions。 虽然经历了一些迭代,但最终成果如下:luau\-wasm (https://pypi.org/project/luau-wasm/) 是一个全新的 PyPI 包,发布了一个 276KB 的 `luau\_wasm\-0\.1a0\-cp314\-cp314\-pyemscripten\_2026\_0\_wasm32\.whl` 文件,可以在 Pyodide 中这样使用: ``` import micropip await micropip.install("luau-wasm") import luau_wasm print(luau_wasm.execute(r''' local animals = {"fox", "owl", "frog", "rabbit"} table.sort(animals, function(a, b) return #a < #b end) for i, name in animals do print(i .. ". " .. name .. " (" .. #name .. ")") end ''')) ``` 你可以在Pyodide REPL 演示 (https://pyodide.org/en/stable/console.html) 中运行这段代码,看看效果。 luau\-wasm 的 GitHub 仓库 (https://github.com/simonw/luau-wasm) 包含了所有构建和部署脚本(使用最新的cibuildwheel (https://github.com/pypa/cibuildwheel)),还部署了一个 HTML 演示页面,该页面加载 Pyodide,安装 `luau\-wasm` 并提供一个尝试它的界面:https://simonw.github.io/luau-wasm/ 截图描述:一个标题为“Luau WASM”的 Web 应用,副标题为“Run Luau in the browser through Pyodide after installing the luau-wasm WebAssembly wheel from PyPI.”右上角有一个绿色的“Ready”状态徽章。下方是示例按钮:“Hello World”、“Variables”、“Tables”、“Fibonacci”、“Runtime Error”。“LUAU SOURCE”代码编辑器包含:local function fib(n: number): number / if n < 2 then return n end / return fib(n - 1) + fib(n - 2) / end / local out = {} / for i = 0, 12 do / table.insert(out, tostring(fib(i))) / end / print(table.concat(out, ", "))。右侧是“OUTPUT”面板,包含一个“Copy”按钮,显示深色终端输出:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144。左下角是蓝色“Run”按钮、“Clear”按钮,以及文本“6.0 ms”。 #### 目前有多少包在使用这个功能? 我很好奇目前有多少包正在为此平台发布 wheels。 在与 ChatGPT 进行了一些调试后 (https://chatgpt.com/share/6a2dee92-b110-83e8-9f53-ba9259b751ed),我得出了这个BigQuery SQL (https://gist.github.com/simonw/4a34a49fca63bc09c50bc31e17b3d33b?permalink_comment_id=6198602#gistcomment-6198602),它针对 PyPI 在 BigQuery 上的公共数据集 (https://packaging.python.org/en/latest/guides/analyzing-pypi-package-downloads/#public-dataset) 运行。这是查询结果的原始 JSON (https://gist.github.com/simonw/4a34a49fca63bc09c50bc31e17b3d33b),这里是一个 Datasette Lite (https://lite.datasette.io/?json=https://gist.github.com/simonw/4a34a49fca63bc09c50bc31e17b3d33b#/data?sql=select%0A++name%2C%0A++platform_tag%2C%0A++matching_file_count%2C%0A++max%28latest_upload%29+as+latest_upload%2C%0A++example_files%0Afrom+%28%0A++--+your+existing+query+here%2C+without+order+by%0A++select%0A++++name%2C%0A++++platform_tag%2C%0A++++matching_file_count%2C%0A++++latest_upload%2C%0A++++example_files%0A++from+raw%0A%29%0Agroup+by+name%0Aorder+by+latest_upload+desc%3B) 中的 SQLite SQL 查询,它按最近上传日期对包进行去重。 如果查询正确,目前有 28 个 PyPI 包使用了新的 `pyemscripten\_202\*\_wasm32` 标签: luau\-wasm (https://pypi.org/project/luau-wasm/), uuid7\-rs (https://pypi.org/project/uuid7-rs/), cmm\-16bit (https://pypi.org/project/cmm-16bit/), pyOpenTTDAdmin (https://pypi.org/project/pyOpenTTDAdmin/), imgui\-bundle (https://pypi.org/project/imgui-bundle/), numbertoolkit (https://pypi.org/project/numbertoolkit/), bashkit (https://pypi.org/project/bashkit/), geoarrow\-rust\-core (https://pypi.org/project/geoarrow-rust-core/), arro3\-io (https://pypi.org/project/arro3-io/), arro3\-core (https://pypi.org/project/arro3-core/), arro3\-compute (https://pypi.org/project/arro3-compute/), onnx (https://pypi.org/project/onnx/), powerfit\-em (https://pypi.org/project/powerfit-em/), tcod (https://pypi.org/project/tcod/), chonkie\-core (https://pypi.org/project/chonkie-core/), tokie (https://pypi.org/project/tokie/), robotraconteur (https://pypi.org/project/robotraconteur/), pydantic\_core (https://pypi.org/project/pydantic_core/), yaml\-rs (https://pypi.org/project/yaml-rs/), cadquery\-ocp\-novtk\-OCP\.wasm (https://pypi.org/project/cadquery-ocp-novtk-OCP.wasm/), uuid\_utils (https://pypi.org/project/uuid_utils/), base64\_utils (https://pypi.org/project/base64_utils/), pycdfpp (https://pypi.org/project/pycdfpp/), lib3mf\-OCP\.wasm (https://pypi.org/project/lib3mf-OCP.wasm/), typst (https://pypi.org/project/typst/), toml\-rs (https://pypi.org/project/toml-rs/), onnx\-weekly (https://pypi.org/project/onnx-weekly/), dummy\-pyodide\-ext\-test (https://pypi.org/project/dummy-pyodide-ext-test/) 希望在接下来的几个月和几年里,能看到更多这样的包出现。

相似文章

使用MicroPython和WASM在沙盒中运行Python代码

Simon Willison's Blog

Simon Willison介绍了micropython-wasm,一个alpha版本包,它使用编译为WebAssembly的MicroPython在沙盒中运行Python代码,从而在Datasette等Python应用中实现安全的插件执行。

luau-wasm 0.1a0

Simon Willison's Blog

luau-wasm 0.1a0,一个 Luau 脚本语言的 WebAssembly 构建版本,已发布。

micropython-wasm 0.1a0

Simon Willison's Blog

micropython-wasm 0.1a0 发布,使MicroPython能够在WebAssembly环境中运行,实现沙盒化和可移植性。

micropython-wasm 0.1a2

Simon Willison's Blog

MicroPython 移植到 WebAssembly,作为在浏览器中沙箱化执行 Python 的工具。