RustPython
一个用 Rust 编写的 Python-3 (CPython >= 3.14.0) 解释器 :snake: :scream: :metal:.
用法
查看我们在 WebAssembly 上运行的在线演示。
RustPython 需要 Rust 最新稳定版本(例如 2023 年 2 月 7 日的 1.67.1)。如果您当前 系统中未安装 Rust,可以按照 rustup.rs 上的说明进行安装。
要检查当前运行的 Rust 版本,请使用 rustc --version。如果您希望更新,
rustup update stable 会将您的 Rust 安装更新到最新的稳定版本。
要在本地构建 RustPython,首先,克隆源代码:
git clone https://github.com/RustPython/RustPython
RustPython 使用符号链接在 Lib/ 中管理 python 库。如果在 Windows 上,运行以下内容会有帮助:
git config core.symlinks true
然后你可以进入 RustPython 目录并运行演示(注意:在 Windows 上需要 --release 以防止栈溢出):
$ cd RustPython
$ cargo run --release demo_closures.py
Hello, RustPython!
或者使用交互式 shell:
$ cargo run --release
Welcome to rustpython
>>>>> 2+2
4
注意:对于 Windows 用户,请在项目目录中将 RUSTPYTHONPATH 环境变量设置为 Lib 路径。
(例如,当 RustPython 目录为 C:\RustPython 时,将 RUSTPYTHONPATH 设置为 C:\RustPython\Lib)
你还可以通过以下方式安装并运行 RustPython:
$ cargo install --git https://github.com/RustPython/RustPython rustpython
$ rustpython
Welcome to the magnificent Rust Python interpreter
>>>>>
venv
由于 RustPython 目前未提供打包完善的安装方式,使用 venv 有助于更轻松地使用 pip。
$ rustpython -m venv <your_env_name>
$ . <your_env_name>/bin/activate
$ python # now `python` is the alias of the RustPython for the new env
PIP
如果你希望发起 https 请求,可以启用 ssl 功能,该功能
同时允许你安装 pip 包管理器。请注意,在 Windows 上,你可能
需要安装 OpenSSL,或者你可以改为启用 ssl-openssl-vendor 功能,
该功能会为你编译 OpenSSL,但需要 C 编译器、perl 和 make。
预期并已在 CI 中测试 OpenSSL 版本 3。旧版本可能无法正常工作。
安装带有 SSL 支持的 rustpython 后,你可以通过 运行以下命令来安装 pip:
cargo install --git https://github.com/RustPython/RustPython
rustpython --install-pip
你还可以通过 conda 包管理器安装 RustPython,尽管
这并不受官方支持,且可能已过时:
conda install rustpython -c conda-forge
rustpython
SSL provider
对于 HTTPS 请求,ssl-rustls-aws-lc 在 RustPython 二进制文件中默认启用。嵌入者可以使用 rustpython-stdlib 的与提供者无关的 ssl-rustls 功能并安装自己的 rustls 加密提供者,或者如果其环境需要 OpenSSL,则可以用 ssl-openssl 替换 rustls。
请注意,要在 Windows 上使用 OpenSSL,您可能需要安装 OpenSSL,或者您可以改为启用 ssl-openssl-vendor 功能,
该功能会为您编译 OpenSSL,但需要 C 编译器、perl 和 make。
预期并已在 CI 中测试 OpenSSL 版本 3。旧版本可能无法正常工作。
WASI
您可以将 RustPython 编译为独立的 WebAssembly WASI 模块,以便它可以在任何地方运行。
Build
cargo build --target wasm32-wasip1 --no-default-features --features freeze-stdlib,stdlib --release
由 wasmer 运行
wasmer run --dir `pwd` -- target/wasm32-wasip1/release/rustpython.wasm `pwd`/extra_tests/snippets/stdlib_random.py
由 wapm 运行
$ wapm install rustpython
$ wapm run rustpython
>>>>> 2+2
4
构建 WASI 文件
你可以使用以下命令构建 WebAssembly WASI 文件:
cargo build --release --target wasm32-wasip1 --features="freeze-stdlib"
注意:我们使用
freeze-stdlib将标准库包含在二进制文件中。您还必须运行一次rustup target add wasm32-wasip1。
JIT (Just in time) 编译器
RustPython 拥有一个非常实验性的 JIT 编译器,可将 Python 函数编译为原生代码。
构建
默认情况下,JIT 编译器未启用,通过 jit cargo 特性启用。
cargo run --features jit
这需要安装 autoconf、automake、libtool 和 clang。
使用
要编译一个函数,请对其调用 __jit__()。
def foo():
a = 5
return 10 + a
foo.__jit__() # this will compile foo to native code and subsequent calls will execute that native code
assert foo() == 15
将 RustPython 嵌入到你的 Rust 应用中
如果你有兴趣在 Rust 编写的应用中暴露 Python 脚本功能,
也许是为了在 Rust 编译时间会成为阻碍的地方快速调整逻辑?
那么 examples/hello_embed.rs 和 examples/mini_repl.rs 可能会提供一些帮助。
免责声明
RustPython 正在开发中,虽然该解释器确实可以用于 一些有趣的用例,例如在 WASM 中运行 Python 以及嵌入到 Rust 项目中,但请注意 RustPython 尚未完全达到生产就绪状态。
我们非常欢迎贡献!有关此方面的更多信息,请参阅我们的贡献部分。
会议视频
查看这些会议上的演讲:
使用场景
尽管 RustPython 是一个相当年轻的项目,但一些人已经用它来 制作了一些很酷的项目:
- GreptimeDB: 一个开源、云原生、分布式时间序列数据库。使用 RustPython 进行嵌入式脚本。
- pyckitup: 一个用 rust 编写的游戏引擎。
- Robot Rumble: 一个基于竞技场的 AI 竞赛平台
- Ruff: 一个极其快速的 Python linter,用 Rust 编写
目标
- 完全用 Rust 实现的完整 Python-3 环境(而非 CPython 绑定)
- 一个没有兼容性 hack 的干净实现
文档
目前,与项目的其他领域一样,文档仍处于 早期阶段。
你可以阅读 在线文档 以获取 最新版本的文档,或者阅读 用户指南。
你还可以通过运行以下命令在本地生成文档:
cargo doc # Including documentation for all dependencies
cargo doc --no-deps --all # Excluding all dependencies
文档 HTML 文件随后可以在 target/doc 目录中找到,或者你可以在之前的命令后添加 --open,
以便在你的默认浏览器中自动打开文档。
有关组件的高层概述,请参阅 架构 文档。
贡献
欢迎并高度赞赏贡献。要开始,请查看 贡献指南。
你也可以加入我们的 Discord。
编译为 WebAssembly
社区
在 Discord 上与我们聊天。
行为准则
我们的行为准则 可以在这里找到。
致谢
初始工作基于 windelbouwman/rspython 和 shinglyu/RustPython
链接
以下是一些相关项目的有用链接:
- https://github.com/ProgVal/pythonvm-rust
- https://github.com/shinglyu/RustPython
- https://github.com/windelbouwman/rspython
许可证
本项目采用 MIT 许可证。请参阅 LICENSE 文件以获取更多详细信息。
项目标志 采用 CC-BY-4.0 许可证。请参阅 LICENSE-logo 文件 以获取更多详细信息。

