### 功能描述 之前发过一遍这个issue,但是还没做好,现在做好了,再发一遍,并且我也编辑了前一次的issue,和现在这里一样 ### 功能描述 在Python中,可以利用Maturin实现与Rust的交互,定义了一个get_sum函数并交给Python <img width="544" height="410" alt="Image" src="https://github.com/user-attachments/assets/bbece445-81fc-4156-b8df-648376a2085b" /> 现在要把一个Rust程序包装成一个Python库,这样更改文件目录: <img width="1280" height="684" alt="Image" src="https://github.com/user-attachments/assets/4802fd63-cb9c-4565-a426-79c54fd9c83b" /> (忽略这张图消失的.venv文件夹) 打开pyproject.toml(不是Cargo.toml)文件,添加两行: ```toml [tool.maturin] python-source = "python" # 这样告诉Maturin Python源文件存在python文件夹里 ``` <img width="1280" height="684" alt="Image" src="https://github.com/user-attachments/assets/92bb979a-0fb3-4ce8-ae33-56242e733c57" /> 运行`maturin develop`(我这里用了release模式编译,所以加了参数`--release`),开始调试 <img width="1215" height="421" alt="Image" src="https://github.com/user-attachments/assets/dbe38073-f92f-4514-8588-bb506f4d2935" /> 编译完成后,就可以写__init__.py了 ```python from .matry1 import get_sum __all__ = ["get_sum"] ``` 写完后,`maturin build`构建 <img width="1280" height="684" alt="Image" src="https://github.com/user-attachments/assets/a35e7555-c2b7-4282-a12c-2ede2c61d436" /> 打开wheels里的whl文件 <img width="1280" height="684" alt="Image" src="https://github.com/user-attachments/assets/5db9fb67-9ca0-422f-8226-f7f3164faea1" /> 复制绝对路径,然后可以直接安装 ```bash PS C:\Users\HP> py -m pip install I:\BobbyCheng\Rust\Maturin\matryoshka_maturin\matry1\target\wheels\matry1-0.1.0-cp314-cp314-win_amd64.whl Looking in indexes: https://mirrors.aliyun.com/pypi/simple/ Processing I:\BobbyCheng\Rust\Maturin\matryoshka_maturin\matry1\target\wheels\matry1-0.1.0-cp314-cp314-win_amd64.whl Installing collected packages: matry1 Successfully installed matry1-0.1.0 PS C:\Users\HP> ``` 接下来可以直接用Python调用 <img width="865" height="522" alt="Image" src="https://github.com/user-attachments/assets/b4db4381-1695-445f-a9fd-26865e508bf6" /> 但是(开始迷惑),由于Rust是可以调用Python的,所以可以这样写(因为我用的是Python 3.14,旧版的pyo3不支持这个版本,所以用了0.29.0的版本,然而这个版本with_gil和import_bound都没了,如果用的是0.23.5的话,就和之前with_gil的方法一样了): <img width="685" height="473" alt="Image" src="https://github.com/user-attachments/assets/0867c495-a56a-4fdc-9e16-72dd12fd78c4" /> 这样就成功套了一层娃: <img width="1280" height="684" alt="Image" src="https://github.com/user-attachments/assets/93d881dc-5415-4d00-8531-c03233e6c3b0" /> ```rust use pyo3::prelude::*; fn main() -> PyResult<()> { Python::attach(|py| { let matry1_mod = py.import("matry1")?; let result = matry1_mod.call_method("get_sum", (1, 2), None)?; let num: i32 = result.extract()?; println!("get_sum 返回: {}", num); Ok(()) }) } ``` Cargo.toml下面加入: ```toml [dependencies] pyo3 = { version = "0.29.0", features = ["auto-initialize"] } ``` 这还没完,再新建一个maturin项目,你甚至还可以这样: <img width="1280" height="684" alt="Image" src="https://github.com/user-attachments/assets/0f819b15-c6f9-437f-b038-d4d91e41e604" /> <img width="1218" height="247" alt="Image" src="https://github.com/user-attachments/assets/079874c6-089f-48b5-a6bc-c6b520462567" /> <img width="1280" height="684" alt="Image" src="https://github.com/user-attachments/assets/de454d13-3c9e-4c36-9563-de5341c54b68" /> <img width="865" height="522" alt="Image" src="https://github.com/user-attachments/assets/d8bc9839-1336-496d-8a6f-082cbec0ad70" /> 这样就成功用Python调用了Rust调用了Python调用了Rust 还能继续套(用Rust调用Python调用Rust调用Python调用Rust): <img width="1280" height="684" alt="Image" src="https://github.com/user-attachments/assets/96919ca5-db35-44dc-8e05-ca1bd79e997e" /> ...... 再往后我就不叠了,但是理论上来说只要内存足够,可以无限套娃 ### 附件 _No response_ ### 附件 _No response_
Activity