Skip to main content

python_with_rust

演示 Python 如何调用一个 Rust 项目

  1. Python 传给 Rust 这些数据结构:str, int, float, list, dict 等
  2. Rust 返回给 Python 这些数据结构:String, i32, f64, Vec, Vec
  3. Python 使用 Rust 类
  4. 把 Python 项目发布,并且兼容多种操作系统

说明

  • 自从 PEP 518 以来,pyproject.toml 是比 setup.py 更为先进的构建文件。
  • 使用 setup.py 管理方式见于分支 use_setup.py,main 分支使用 pyproject.toml

一、安装

两种方式:

  1. 使用 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
  2. 如果环境不在上述范围内,可以下载项目后在本地编译并安装。包括以下情况:
    • 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 版本

本地编译并安装的步骤:

  1. 安装 rust、Python(略),可以升级到较新版本
  2. 安装 maturin pip install maturin
  3. 在本地编译并安装此项目 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 的方案

Metadata

Release files for python-with-rust 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for python-with-rust 0.1.5
File
python_with_rust-0.1.5-cp312-none-win_amd64.whl CPython 3.12 none Windows x86-64 Details
python_with_rust-0.1.5-cp312-cp312-manylinux_2_34_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.34+ x86-64 Details
python_with_rust-0.1.5-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
python_with_rust-0.1.5-cp311-none-win_amd64.whl CPython 3.11 none Windows x86-64 Details
python_with_rust-0.1.5-cp311-cp311-manylinux_2_34_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.34+ x86-64 Details
python_with_rust-0.1.5-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
python_with_rust-0.1.5-cp310-none-win_amd64.whl CPython 3.10 none Windows x86-64 Details
python_with_rust-0.1.5-cp310-cp310-manylinux_2_34_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.34+ x86-64 Details
python_with_rust-0.1.5-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details
python_with_rust-0.1.5-cp39-none-win_amd64.whl CPython 3.9 none Windows x86-64 Details
python_with_rust-0.1.5-cp39-cp39-manylinux_2_34_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.34+ x86-64 Details
python_with_rust-0.1.5-cp39-cp39-macosx_11_0_arm64.whl CPython 3.9 CPython 3.9 macOS 11.0+ ARM64 Details
python_with_rust-0.1.5-cp38-none-win_amd64.whl CPython 3.8 none Windows x86-64 Details
python_with_rust-0.1.5-cp38-cp38-manylinux_2_34_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.34+ x86-64 Details
python_with_rust-0.1.5-cp38-cp38-macosx_11_0_arm64.whl CPython 3.8 CPython 3.8 macOS 11.0+ ARM64 Details

Total release size: 3.3 MB

Release files / python_with_rust-0.1.5-cp312-none-win_amd64.whl

Download URL python_with_rust-0.1.5-cp312-none-win_amd64.whl
Size 144.4 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
8003a19f07030104e1b6e5b3bc742ceecfb0e9ae7777f86e9c7936a6971d464d
BLAKE2b-256 checksum
How to use checksums
06fd8b984b8924e883227e558aec70ef70b61ef861b6d70ebc352acff0ccffbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp312-cp312-manylinux_2_34_x86_64.whl

Download URL python_with_rust-0.1.5-cp312-cp312-manylinux_2_34_x86_64.whl
Size 278.4 kB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
4d4422f40d0d360dcfe4d318d8e45d244e64a528555f478e0ce75f4ae664c49b
BLAKE2b-256 checksum
How to use checksums
5ae2aa6c2a19f1a603f748dbfd9a9c716428f1300a654edda3478b5cca12133d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp312-cp312-macosx_11_0_arm64.whl

Download URL python_with_rust-0.1.5-cp312-cp312-macosx_11_0_arm64.whl
Size 241.5 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
84fa79a95c488677393d29f8a8436fae6a5bc1b857410240143b7b85eb131d1a
BLAKE2b-256 checksum
How to use checksums
5e8bad65ca1c74bfa8733a7f6d5a67449e52244820766b34b87ff9371fc29c14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp311-none-win_amd64.whl

Download URL python_with_rust-0.1.5-cp311-none-win_amd64.whl
Size 145.3 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
38811657c9e9214030ec1bc2c29b7e5faa82bac547214acc257424eb94572c2b
BLAKE2b-256 checksum
How to use checksums
31dff5b531cd6b313df11bd1e8781f2dee21480e77453d7ac350978f0b3f23fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp311-cp311-manylinux_2_34_x86_64.whl

Download URL python_with_rust-0.1.5-cp311-cp311-manylinux_2_34_x86_64.whl
Size 278.6 kB
Tags CPython 3.11 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
14dae107501f5ccd33dc4d47998defb76cdee8303d61dcd5216a474e49c1f2f4
BLAKE2b-256 checksum
How to use checksums
6b2434759f0e1bbda315ac548b568e78a6cca58a82975cd8bd51de649f6db2b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp311-cp311-macosx_11_0_arm64.whl

Download URL python_with_rust-0.1.5-cp311-cp311-macosx_11_0_arm64.whl
Size 242.5 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
fbf4bfbdff488d3e79708c4391370bde9651bcd3d224725fec0fac9cb3d6a14a
BLAKE2b-256 checksum
How to use checksums
e4c91d7c45675c5b73556ad82b6ac4ee8c991394a12db779fafbffd8c773d8b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp310-none-win_amd64.whl

Download URL python_with_rust-0.1.5-cp310-none-win_amd64.whl
Size 145.4 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
9503e355c62e7cfba1e11b541443048cad80458f54c4d7c0a713087d6613c5c2
BLAKE2b-256 checksum
How to use checksums
34887f63766460f391eeaa7b0481ee93b6fba063013376c86a1a1ecda909fde6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp310-cp310-manylinux_2_34_x86_64.whl

Download URL python_with_rust-0.1.5-cp310-cp310-manylinux_2_34_x86_64.whl
Size 278.7 kB
Tags CPython 3.10 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
0df3042dbc305d455e21a83dc6b50390443b44f4e87b10b37f765814fefaed12
BLAKE2b-256 checksum
How to use checksums
3d8cc12b913510df109ce2e17d85e1959731454db68a04dc0373092460c3e79c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp310-cp310-macosx_11_0_arm64.whl

Download URL python_with_rust-0.1.5-cp310-cp310-macosx_11_0_arm64.whl
Size 242.7 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c64726e72f4358ccfdfaca71e1adfdc075b496f1b3e4e24110657c92d1ad6077
BLAKE2b-256 checksum
How to use checksums
48eaffc18ddf09f4451838d48c11368a393bbc6622ab593e4321f4b01cb176b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp39-none-win_amd64.whl

Download URL python_with_rust-0.1.5-cp39-none-win_amd64.whl
Size 145.9 kB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
be5654c58a2b0f493e0e7f032eb09ff5a84009593ea172bafa788a6df3b016ba
BLAKE2b-256 checksum
How to use checksums
f8fb3fb1c9211b865ebae498b829669c563e36e90050abc7ff7b6e0183b09f7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp39-cp39-manylinux_2_34_x86_64.whl

Download URL python_with_rust-0.1.5-cp39-cp39-manylinux_2_34_x86_64.whl
Size 279.7 kB
Tags CPython 3.9 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
74a0dbbce65c11871f26e6b7fee9548fa48747cdb72efc03106218338eee49f1
BLAKE2b-256 checksum
How to use checksums
13bd1b5a2df5f79127b3b58ecb27b2d0ab0c3cb8e712707311c3565eba55b6ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp39-cp39-macosx_11_0_arm64.whl

Download URL python_with_rust-0.1.5-cp39-cp39-macosx_11_0_arm64.whl
Size 245.0 kB
Tags CPython 3.9 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f85185e39938da357f81cfc42f16dae0674b26f14da58382cc717c7f3048d622
BLAKE2b-256 checksum
How to use checksums
482270137ddcd1eb9450dbeaf0893ddeb1d636e9bdb0f2705cc06f12b8d9e48b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp38-none-win_amd64.whl

Download URL python_with_rust-0.1.5-cp38-none-win_amd64.whl
Size 145.9 kB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
66bd021f71271e240d0d8bb66a900d10f6e4d169b10db812a2edea1fa21ec1a1
BLAKE2b-256 checksum
How to use checksums
10d102989fc5fbbcd0ede1cadbf38c6d15b8a9ab4a65b75ae80054fe073ee1e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp38-cp38-manylinux_2_34_x86_64.whl

Download URL python_with_rust-0.1.5-cp38-cp38-manylinux_2_34_x86_64.whl
Size 279.5 kB
Tags CPython 3.8 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
12461491d81df94137777f952acb5c0da7833562dbbb5defe6a40f35bd6cfc35
BLAKE2b-256 checksum
How to use checksums
02209eefd4605cc5e10905f051bbb888b21140f43962c147e80db47a823d8848
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / python_with_rust-0.1.5-cp38-cp38-macosx_11_0_arm64.whl

Download URL python_with_rust-0.1.5-cp38-cp38-macosx_11_0_arm64.whl
Size 243.3 kB
Tags CPython 3.8 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
9bee2c20db37f08b14bdbbb32990c03dd2f754a17a288748039ecb17b9f81214
BLAKE2b-256 checksum
How to use checksums
7b73eb81bbbb3760ea1df5a2b07fda5c0bd34b505e8ff764626502df076334fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.1.5 This release

15 release files

0.1.4

12 release files

0.1.3

12 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page