RustPython/RustPython
GitHub: RustPython/RustPython
RustPython 是一个完全用 Rust 编写的 Python 3 解释器,旨在提供内存安全的实现并支持 WebAssembly 和嵌入式应用。
Stars: 21991 | Forks: 1425

# [RustPython](https://rustpython.github.io/)
一个用 Rust 编写的 Python-3 (CPython >= 3.14.0) 解释器 :snake: :scream:
:metal:.
[](https://github.com/RustPython/RustPython/actions?query=workflow%3ACI)
[](https://codecov.io/gh/RustPython/RustPython)
[](https://opensource.org/licenses/MIT)
[](https://github.com/RustPython/RustPython/graphs/contributors)
[][discord]
[](https://docs.rs/rustpython/)
[](https://crates.io/crates/rustpython)
[](https://deps.rs/crate/rustpython/0.1.1)
[](https://gitpod.io#https://github.com/RustPython/RustPython)
## 使用说明
**请查看我们在 WebAssembly 上运行的[在线演示](
>>>> 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
$ . /bin/activate
$ python # now `python` is the alias of the RustPython for the new env
```
### PIP
如果您想发起 https 请求,可以启用 `ssl` 功能,这也
允许您安装 `pip` 包管理器。请注意,在 Windows 上,您
可能需要安装 OpenSSL,或者您可以改为启用 `ssl-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 提供程序
对于 HTTPS 请求,默认启用 `ssl-rustls` 功能。如果您的环境需要 OpenSSL,可以将其替换为 `ssl-openssl` 功能。
请注意,要在 Windows 上使用 OpenSSL,您可能需要安装 OpenSSL,或者您可以改为启用 `ssl-vendor` 功能,
该功能会为您编译 OpenSSL,但需要 C 编译器、perl 和 `make`。
CI 中预期并测试的是 OpenSSL 版本 3。旧版本可能无法工作。
### WASI
您可以将 RustPython 编译为独立的 WebAssembly WASI 模块,以便在任何地方运行。
构建
```
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"
```
### 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 尚未完全准备好投入生产。
非常欢迎贡献!有关更多信息,请参阅我们的贡献部分。
## 会议视频
查看这些会议上的演讲:
- [FOSDEM 2019](https://www.youtube.com/watch?v=nJDY9ASuiLc)
- [EuroPython 2018](https://www.youtube.com/watch?v=YMmio0JHy_Y)
## 用例
虽然 RustPython 是一个相当年轻的项目,但一些人已经用它来
制作很酷的项目:
- [GreptimeDB](https://github.com/GreptimeTeam/greptimedb):一个开源、云原生、分布式时序数据库。使用 RustPython 进行嵌入式脚本编写。
- [pyckitup](https://github.com/pickitup247/pyckitup):一个用
rust 编写的游戏引擎。
- [Robot Rumble](https://github.com/robot-rumble/logic/):一个基于竞技场的 AI 竞赛平台
- [Ruff](https://github.com/charliermarsh/ruff/):一个极快的 Python linter,用 Rust 编写
## 目标
- 完全用 Rust 实现完整的 Python-3 环境(不是 CPython 绑定)
- 一个没有兼容性 hack 的干净实现
## 文档
目前与项目的其他领域一样,文档仍处于
早期阶段。
您可以阅读最新版本的[在线文档](https://docs.rs/rustpython),或[用户指南](https://rustpython.github.io/docs/)。
您还可以通过运行以下命令在本地生成文档:
```
cargo doc # Including documentation for all dependencies
cargo doc --no-deps --all # Excluding all dependencies
```
然后可以在 `target/doc` 目录中找到文档 HTML 文件,或者您可以在之前的命令中附加 `--open` 以
在默认浏览器中自动打开文档。
有关组件的高级概述,请参阅[架构](architecture/architecture.md)文档。
## 贡献
非常欢迎贡献,在许多情况下,我们很乐意通过 PR 或在 Discord 上指导
贡献者。请参阅
[开发指南](DEVELOPMENT.md) 以获取开发技巧。
考虑到这一点,请注意该项目由志愿者维护,以下是一些
最好的入门方式:
大多数任务列在
[issue 追踪器](https://github.com/RustPython/RustPython/issues) 中。如果您希望开始编码,请查看标记为 [good first issue](https://github.com/RustPython/RustPython/issues?q=label%3A%22good+first+issue%22+is%3Aissue+is%3Aopen+) 的 issue。
为了增强 CPython 兼容性,请尝试通过查看这篇文章来增加 unittest 覆盖率:[How to contribute to RustPython by CPython unittest](https://rustpython.github.io/guideline/2020/04/04/how-to-contribute-by-cpython-unittest.html)
另一种方法是查看源代码:内置函数和对象
方法通常是最简单、最容易的贡献方式。
您也可以直接运行 `python -I scripts/whats_left.py` 来帮助查找任何未实现的
方法。
## 编译为 WebAssembly
[参阅此文档](wasm/README.md)
## 行为准则
我们的行为准则[可以在这里找到](code-of-conduct.md)。
## 链接
这些是一些指向相关项目的有用链接:
- https://github.com/ProgVal/pythonvm-rust
- https://github.com/shinglyu/RustPython
- https://github.com/windelbouwman/rspython
## 许可证
本项目根据 MIT 许可证获得许可。请参阅
[LICENSE](LICENSE) 文件了解更多详情。
[项目徽标](logo.png) 根据 CC-BY-4.0
许可证获得许可。请参阅 [LICENSE-logo](LICENSE-logo) 文件
了解更多详情。标签:AI工具, CPython, DNS解析, Python 3, Python解释器, Rust, WASM, WebAssembly, 内存安全, 可视化界面, 威胁情报, 字节码, 安全, 嵌入式Python, 开发者工具, 开源项目, 性能优化, 检测绕过, 生成式AI安全, 编程语言, 编译器, 网络流量审计, 虚拟机, 解释器实现, 超时处理, 逆向工具, 通知系统