RustPython简单使用
RustPython介绍#
同CPython,Jpython,PyPy一样,RustPython,是使用Rust语言实现的Python解释器,支持Python3语法。
项目地址:https://github.com/RustPython/RustPython
RustPython真正方便的是可以编译成Wasm文件,可以直接在浏览器中使用,示例网站:https://rustpython.github.io/demo/
RustPython简单使用#
需要提前安装好:git、rust的cargo环境
- 克隆并进入项目
git clone https://github.com/RustPython/RustPython
cd RustPython
- 手动编译
cargo build --release --features ssl
cd target/release
启用--feaatures ssl特性可以安装pip包管理工具
编译后在target/release下生成rust_python可执行文件
- 使用RustPython交互解释器
$ ./rustpython
Welcome to the magnificent Rust Python 0.4.0 interpreter 😱 🖖
No previous history.
RustPython 3.12.0
Type "help", "copyright", "credits" or "license" for more information.
>>>>>
然后你就可以在其中执行Python语句,如
>>>>> a = 1
>>>>> if a > 0:
..... print('%d > 0' % a)
.....
1 > 0
>>>>>
按Ctrl + Z退出交互环境
4. 执行python脚本
新建demo.py
a = 1
if a > 0:
print('%d > 0' % a)
使用rust_python解释器执行python脚本
$ ./rustpython demo.py
1 > 0
- 安装pip及三方包
安装pip
$ sudo ./rust_python --install-pip
使用pip安装三方包requests
$ ./rust_python -m pip install requests
使用三方包
新建demo2.py
import requests
r = requests.get('https://httpbin.org/get')
print(r.text)
执行脚本
$ ./rustpython demo2.py
...省略requests包的警告信息
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.32.3",
"X-Amzn-Trace-Id": "Root=1-66e2c7ac-6c207dba4d8aecdd32adf668"
},
"origin": "117.186.213.150",
"url": "https://httpbin.org/get"
}
作者:韩志超
出处:https://www.cnblogs.com/superhin/p/18410808/rustpython
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
如有问题,请微信联系 superz-han,并发送原文链接。
标签:
RustPython
/* 请我喝杯咖啡吧 */
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律