Python 调用 Rust 的示例
Project description
python_with_rust
演示 Python 如何调用一个 Rust 项目
- Python 传给 Rust 这些数据结构:str, int, float, list, dict 等
- Rust 返回给 Python 这些数据结构:String, i32, f64, Vec, Vec
- Python 使用 Rust 类
- 把 Python 项目发布,并且兼容多种操作系统
说明
- 自从 PEP 518 以来,
pyproject.toml
是比setup.py
更为先进的构建文件。 - 使用
setup.py
管理方式见于分支 use_setup.py,main 分支使用pyproject.toml
一、环境要求
- 安装 rust、Python(略)
- 安装必要的包
pip install maturin
二、测试
安装
maturin develop
测试
python examples/example1.py # 测试可以调用 Python 函数
python examples/example2.py # 测试可以调用 Rust 函数
python examples/example3.py # 测试相互传递 list、string 等数据
python examples/example4.py # 测试可以调用 Rust 类
三、说明
./my_rust_project1
是一个纯 Rust 项目./my_rust_project
是一个中间层,它使用 pyo3,调用my_rust_project1
并被 Python 调用- 目的是让
./my_rust_project1
不用考虑被 python 调用时的各种问题,成为纯粹的 Rust 项目,不被pyo3
污染
- 目的是让
- 但是在中间层
./my_rust_project
执行cargo build
会失败(可能与什么配置有关) - 另一个方案是 ffi 方法,使用 C 标准编译
my_rust_project1
,编译后的代码可以被 Python(ctypes)/C/Rust/Java 调用。具体参见 郭飞的笔记- 缺点是需要自定义数据类型,并有内存泄露的风险。优点是某些情况下潜在性能更高
- 使用
pyo3
是以一个比较好的实践,这个项目展示使用pyo3
的方案
关于名称
- 使用 python 的
from python_with_rust import my_rust_project
时,这里的my_rust_project
对应的是./my_rust_project/src/lib.rs
中#[pymodule]
定义的那个函数 setup.py
中的RustExtension("python_with_rust.my_rust_project")
对应的是安装后的文件夹名、.so 文件名。为了防止混乱,最好取值为 python 项目名、Rust 项目名
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distributions
Close
Hashes for python_with_rust-0.1.3-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0804d3bdcd21daaffdf3d3f15b541c784c04c6848a55ff628400ea1bb033f4b8 |
|
MD5 | 50c7a07fa7f02ce472677c858a50463b |
|
BLAKE2b-256 | 32dbf851d8ef2e5749516726e9eeae77bd62a80e89f69ab3e370f3a723952981 |
Close
Hashes for python_with_rust-0.1.3-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25ccbc148a689f4456499066cf68bbfbc82936ca426db58ecfe04fe458f2e95d |
|
MD5 | ee0c597daed1c7128672cb2d392e2a0e |
|
BLAKE2b-256 | 22ce906a76abf138420d192532e37a02d743203e3fa8f1d057afae0274a06bc8 |
Close
Hashes for python_with_rust-0.1.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e95a6f87bcbad84ab5063d8c0f3b8d08e6bf35fb00445ac7f6c6d20ced61ebe1 |
|
MD5 | 7375abaf8a82adc2e2fe833294e338fc |
|
BLAKE2b-256 | 13477352dc5810181a0c1875065c1490afcc3abb6359aa2800e91a97961b36dc |
Close
Hashes for python_with_rust-0.1.3-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a656c2f1d77760426c83bafd1705a03236497f17637b6a27034032970c510af8 |
|
MD5 | 720a6fed5fc777d7e695036b441cd0e9 |
|
BLAKE2b-256 | cb7655519710f812ff990c6fa99428b2a1e0a8f994a42055e021863bd7e5e6cb |
Close
Hashes for python_with_rust-0.1.3-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6053e8f8b0e4c1d2e752648e210a4e830f1d77cda91228a85bd84c0f7a8bf0c0 |
|
MD5 | 6ca690e515f8ca5bcb6eeb828919c48c |
|
BLAKE2b-256 | 223566f41839b414b6e4364a85c6b6f7d96e00eb0f41ce4a4c766d99642e28f6 |
Close
Hashes for python_with_rust-0.1.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af97a24a216ecc93826295e20bac2f6a3d2ad1916807c2aa0e82731d138bbdf0 |
|
MD5 | 8e953feeb3ab96db64e2e5d741cade8e |
|
BLAKE2b-256 | b50b898f623f0ed5197f6f6e7f6913e5dadb2b8acb821283e14256b740944bf9 |
Close
Hashes for python_with_rust-0.1.3-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c28a72d5095f4e8d68f60b6adb2604cb3a84cf2f8f32c947082a7261266494b |
|
MD5 | 61189faf5d5403c450efbf6e35c29444 |
|
BLAKE2b-256 | ec99cb5b46d9e0aa2938deefa536d3fbc3b913cd46c4235835c1ceeb6d8a5028 |
Close
Hashes for python_with_rust-0.1.3-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | adb281224e1e77781406cfcf5989b7bcff3fb556a0e33507e68066203c75ee7d |
|
MD5 | f23705c683f48ed6c545290236947f3c |
|
BLAKE2b-256 | 84f2b42deaabdccd2149eabee22317831620d7361646609ce3df5c590757bf9b |
Close
Hashes for python_with_rust-0.1.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 429c48829af9d3a53d467a92a3118f8e26f63cddb989cbfee43a87d05e211814 |
|
MD5 | 320ae78688e84b2744fe411607198b85 |
|
BLAKE2b-256 | f20eef001c3fd540619d42b06c7f11ebe580d458814e551413506d710500adc1 |
Close
Hashes for python_with_rust-0.1.3-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d050d8eeaba927837dd3d6882b419538761130d10d76e94006c7427a7b0bbc40 |
|
MD5 | f3014fd09fb2939e58abc6b56301f645 |
|
BLAKE2b-256 | f937d9c8b7f4d50c1fc30d1f14b033e53835439703e2bfdd20d981be9d7854d8 |
Close
Hashes for python_with_rust-0.1.3-cp38-cp38-manylinux_2_34_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 436193092e1d4a8d57e7f2ef82902e90d3a8398870eafee1a711b40b213595a5 |
|
MD5 | 1f1953c931420768468531eca2b40d61 |
|
BLAKE2b-256 | eafd7f360a0892adf5e8c3e792bdfa796551246578a2d1652f6ba7d96ba4da0c |
Close
Hashes for python_with_rust-0.1.3-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87a29dc72165535862f7f86ed2319be2d06327ea51fc7a20439a4a6fbaf51c9a |
|
MD5 | 25d5c5183dd4f6a0f2d0355494acb0b6 |
|
BLAKE2b-256 | 9a968cec18ab37884b8fb8f8ac191fa2b485f85203bd500fbefaa893eaf17839 |