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
一、安装
两种方式:
- 使用
pip install python_with_rust==0.1.5
但只限于已经对对应的环境预编译的情况,包含:ubuntu-latest
,macos-latest
,windows-latest
; python 版本为:'3.8', '3.9', '3.10', '3.11' ,'3.12'- 使用 CI 方法,在 release 时从 pypi 针对一些环境预编译并发布,CI见于workflows/release.yml
- pypi 托管的预编译文件可以查看 https://pypi.org/project/numpy/#files
- 如果环境不在上述范围内,可以下载项目后在本地编译并安装。包括以下情况:
- macos x86。Intel 系列的 Macbook,或者虽然是 M 系列处理器,但是 python 环境是通过 Rosetta 2 以 Intel 模拟模式运行的。
- manylinux/musllinux 的 aarch64版本。一般出现在移动设备、嵌入式设备上,或者用 MacBook 打开的默认 docker
- Linux 的 glibc 版本较老的
- win32
- macOS 14.7 + arm64 不支持 python 3.7,因此没发布较老的 Python 版本
- 上述情况实际上也可以用 CI 方法批量向 pypi 发布。但此项目只是示例,没必要都涵盖。
如何查询自己的环境?
uname -m
查询架构python -c "import platform; print(platform.machine())"
查询 Python 环境ldd --version
查询 glibc 版本
本地编译并安装的步骤:
- 安装 rust、Python(略),可以升级到较新版本
- 安装 maturin
pip install maturin
- 在本地编译并安装此项目
matruin 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 项目,是 python 所要调用的 rust项目。它与pyo3
之类的无关。./Cargo.toml
是一个中间层,它使用 pyo3,调用my_rust_project1
并被 Python 调用- 另一个方案是 ffi 方法,使用 C 标准编译
my_rust_project1
,编译后的代码可以被 Python(ctypes)/C/Rust/Java 调用。具体参见 郭飞的笔记- 缺点是需要自定义数据类型,并有内存泄露的风险。优点是某些情况下潜在性能更高
- 使用
pyo3
是以一个比较好的实践,这个项目仅展示使用pyo3
的方案
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
Built Distributions
File details
Details for the file python_with_rust-0.1.5-cp312-none-win_amd64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp312-none-win_amd64.whl
- Upload date:
- Size: 144.4 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8003a19f07030104e1b6e5b3bc742ceecfb0e9ae7777f86e9c7936a6971d464d |
|
MD5 | fb520a0357b7efd90b5194255fafff71 |
|
BLAKE2b-256 | 06fd8b984b8924e883227e558aec70ef70b61ef861b6d70ebc352acff0ccffbc |
File details
Details for the file python_with_rust-0.1.5-cp312-cp312-manylinux_2_34_x86_64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp312-cp312-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 278.4 kB
- Tags: CPython 3.12, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d4422f40d0d360dcfe4d318d8e45d244e64a528555f478e0ce75f4ae664c49b |
|
MD5 | 65cb0a053a779c5d107befc46e51d219 |
|
BLAKE2b-256 | 5ae2aa6c2a19f1a603f748dbfd9a9c716428f1300a654edda3478b5cca12133d |
File details
Details for the file python_with_rust-0.1.5-cp312-cp312-macosx_11_0_arm64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 241.5 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84fa79a95c488677393d29f8a8436fae6a5bc1b857410240143b7b85eb131d1a |
|
MD5 | a6f0d95dce963e87c7efd2e211c1415d |
|
BLAKE2b-256 | 5e8bad65ca1c74bfa8733a7f6d5a67449e52244820766b34b87ff9371fc29c14 |
File details
Details for the file python_with_rust-0.1.5-cp311-none-win_amd64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp311-none-win_amd64.whl
- Upload date:
- Size: 145.3 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38811657c9e9214030ec1bc2c29b7e5faa82bac547214acc257424eb94572c2b |
|
MD5 | 562c83812fadc0b4ff30f3fd316046c7 |
|
BLAKE2b-256 | 31dff5b531cd6b313df11bd1e8781f2dee21480e77453d7ac350978f0b3f23fb |
File details
Details for the file python_with_rust-0.1.5-cp311-cp311-manylinux_2_34_x86_64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp311-cp311-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 278.6 kB
- Tags: CPython 3.11, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14dae107501f5ccd33dc4d47998defb76cdee8303d61dcd5216a474e49c1f2f4 |
|
MD5 | 5bb711765ebb76125efa2bf1dfc66161 |
|
BLAKE2b-256 | 6b2434759f0e1bbda315ac548b568e78a6cca58a82975cd8bd51de649f6db2b6 |
File details
Details for the file python_with_rust-0.1.5-cp311-cp311-macosx_11_0_arm64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 242.5 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbf4bfbdff488d3e79708c4391370bde9651bcd3d224725fec0fac9cb3d6a14a |
|
MD5 | 7890d82bdbe536672fc6ac081fc38519 |
|
BLAKE2b-256 | e4c91d7c45675c5b73556ad82b6ac4ee8c991394a12db779fafbffd8c773d8b6 |
File details
Details for the file python_with_rust-0.1.5-cp310-none-win_amd64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp310-none-win_amd64.whl
- Upload date:
- Size: 145.4 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9503e355c62e7cfba1e11b541443048cad80458f54c4d7c0a713087d6613c5c2 |
|
MD5 | 613e00ad2dede799f028e051e880ad96 |
|
BLAKE2b-256 | 34887f63766460f391eeaa7b0481ee93b6fba063013376c86a1a1ecda909fde6 |
File details
Details for the file python_with_rust-0.1.5-cp310-cp310-manylinux_2_34_x86_64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp310-cp310-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 278.7 kB
- Tags: CPython 3.10, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0df3042dbc305d455e21a83dc6b50390443b44f4e87b10b37f765814fefaed12 |
|
MD5 | 1eaec979d357ea77f4ea755938a81a42 |
|
BLAKE2b-256 | 3d8cc12b913510df109ce2e17d85e1959731454db68a04dc0373092460c3e79c |
File details
Details for the file python_with_rust-0.1.5-cp310-cp310-macosx_11_0_arm64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 242.7 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c64726e72f4358ccfdfaca71e1adfdc075b496f1b3e4e24110657c92d1ad6077 |
|
MD5 | 09dd3e3530663110d509169bb9fc342c |
|
BLAKE2b-256 | 48eaffc18ddf09f4451838d48c11368a393bbc6622ab593e4321f4b01cb176b8 |
File details
Details for the file python_with_rust-0.1.5-cp39-none-win_amd64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp39-none-win_amd64.whl
- Upload date:
- Size: 145.9 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | be5654c58a2b0f493e0e7f032eb09ff5a84009593ea172bafa788a6df3b016ba |
|
MD5 | d29dcb8208ed277e56d7146b42fd2de6 |
|
BLAKE2b-256 | f8fb3fb1c9211b865ebae498b829669c563e36e90050abc7ff7b6e0183b09f7e |
File details
Details for the file python_with_rust-0.1.5-cp39-cp39-manylinux_2_34_x86_64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp39-cp39-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 279.7 kB
- Tags: CPython 3.9, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74a0dbbce65c11871f26e6b7fee9548fa48747cdb72efc03106218338eee49f1 |
|
MD5 | 51e3d727c4ec4d0fa10d97f5c09527a4 |
|
BLAKE2b-256 | 13bd1b5a2df5f79127b3b58ecb27b2d0ab0c3cb8e712707311c3565eba55b6ca |
File details
Details for the file python_with_rust-0.1.5-cp39-cp39-macosx_11_0_arm64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 245.0 kB
- Tags: CPython 3.9, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f85185e39938da357f81cfc42f16dae0674b26f14da58382cc717c7f3048d622 |
|
MD5 | 8356dcc139ef93c198c462ed60fcb440 |
|
BLAKE2b-256 | 482270137ddcd1eb9450dbeaf0893ddeb1d636e9bdb0f2705cc06f12b8d9e48b |
File details
Details for the file python_with_rust-0.1.5-cp38-none-win_amd64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp38-none-win_amd64.whl
- Upload date:
- Size: 145.9 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66bd021f71271e240d0d8bb66a900d10f6e4d169b10db812a2edea1fa21ec1a1 |
|
MD5 | 993893da9df35a36a013356b8cf0852c |
|
BLAKE2b-256 | 10d102989fc5fbbcd0ede1cadbf38c6d15b8a9ab4a65b75ae80054fe073ee1e0 |
File details
Details for the file python_with_rust-0.1.5-cp38-cp38-manylinux_2_34_x86_64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp38-cp38-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 279.5 kB
- Tags: CPython 3.8, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12461491d81df94137777f952acb5c0da7833562dbbb5defe6a40f35bd6cfc35 |
|
MD5 | ac044d0850615db5b5617370abf61fb0 |
|
BLAKE2b-256 | 02209eefd4605cc5e10905f051bbb888b21140f43962c147e80db47a823d8848 |
File details
Details for the file python_with_rust-0.1.5-cp38-cp38-macosx_11_0_arm64.whl
.
File metadata
- Download URL: python_with_rust-0.1.5-cp38-cp38-macosx_11_0_arm64.whl
- Upload date:
- Size: 243.3 kB
- Tags: CPython 3.8, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9bee2c20db37f08b14bdbbb32990c03dd2f754a17a288748039ecb17b9f81214 |
|
MD5 | d09bb6756ff0c35305af4619b7c51321 |
|
BLAKE2b-256 | 7b73eb81bbbb3760ea1df5a2b07fda5c0bd34b505e8ff764626502df076334fb |