RustPython基于 Rust 的 Python 解释器

联合创作 · 2023-10-02 00:53

RustPython 是一个由 Rust 编写的 Python 3 解释器。RustPython 可以嵌入到 Rust 程序中,使用 Python 作为应用程序的脚本语言,也可以将其编译为 WebAssembly,在浏览器中运行 Python。

用法:

RustPython 需要 Rust 1.43.0 或以上版本,可以使用以下命令检查 Rust 的版本:

rustc --version

如果想更新至最新版可以使用以下命令:

rustup update stable

要在本地构建 RustPython,可执行以下操作:

$ git clone https://github.com/RustPython/RustPython
$ cd RustPython
  # if you're on windows:
$ powershell scripts\symlinks-to-hardlinks.ps1
  # --release is needed (at least on windows) to prevent stack overflow
$ cargo run --release demo.py
Hello, RustPython!

或使用交互式 shell:

$ cargo run --release
Welcome to rustpython
>>>>> 2+2
4

还可以使用以下命令安装和运行 RustPython:

$ cargo install rustpython
$ rustpython
Welcome to the magnificent Rust Python interpreter
>>>>>

或通过conda包管理器:

$ conda install rustpython -c conda-forge
$ rustpython

WASI:

可以使用以下命令将 RustPython 编译为独立的 WebAssembly WASI 模块,以便可以在任何地方运行它。

$ wapm install rustpython
$ wapm run rustpython
>>>>> 2 + 2
4

构建 WASI 文件

使用以下方法构建 WebAssembly WASI 文件:

cargo build --release --target wasm32-wasi --features="freeze-stdlib"
浏览 4
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报