setuptools锈扩展插件
setuptools-rust的Python项目详细描述
用于rust扩展的setuptools插件
您可能希望签出pyo3-pack,此项目的后续项目,它允许在不进行任何配置的情况下开发、生成和上载
使用PyO3和rust-cpython实现的rust python扩展的setuptools帮助程序。
编译和分发用rust编写的python扩展就像 它们是用c写成的。
设置
有关完整示例,请参见 html-py-ever。
首先,您需要创建一组文件:
设置.py
fromsetuptoolsimportsetupfromsetuptools_rustimportBinding,RustExtensionsetup(name="hello-rust",version="1.0",rust_extensions=[RustExtension("hello_rust.hello_rust",binding=Binding.PyO3)],packages=["hello_rust"],# rust extensions are not zip safe, just like C-extensions.zip_safe=False,)
在
中显示生成源发行版时需要此文件
include Cargo.toml recursive-include src *
pyproject.toml
[build-system]requires=["setuptools", "wheel", "setuptools-rust"]
制造轮子。sh
#!/bin/bashset -ex curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightly -y exportPATH="$HOME/.cargo/bin:$PATH"cd /io for PYBIN in /opt/python/{cp27-cp27m,cp27-cp27mu,cp35-cp35m,cp36-cp36m,cp37-cp37m}/bin;doexportPYTHON_SYS_EXECUTABLE="$PYBIN/python""${PYBIN}/pip" install -U setuptools wheel setuptools-rust "${PYBIN}/python" setup.py bdist_wheel donefor whl in dist/*.whl;do auditwheel repair "$whl" -w dist/ done
用法
您可以使用与C扩展相同的命令。例如:
>>> python ./setup.py develop
running develop
running egg_info
writing hello-rust.egg-info/PKG-INFO
writing top-level names to hello_rust.egg-info/top_level.txt
writing dependency_links to hello_rust.egg-info/dependency_links.txt
reading manifest file 'hello_rust.egg-info/SOURCES.txt'
writing manifest file 'hello_rust.egg-info/SOURCES.txt'
running build_ext
running build_rust
cargo build --manifest-path extensions/Cargo.toml --features python3
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
Creating /.../lib/python3.6/site-packages/hello_rust.egg-link (link to .)
Installed hello_rust
Processing dependencies for hello_rust==1.0
Finished processing dependencies for hello_rust==1.0
或者可以使用bdist_wheel之类的命令(在安装wheel之后)。
默认情况下,develop
将创建调试生成,而install
将创建发布生成。
Linux上的二进制轮子
要在Linux上构建二进制控制盘,需要使用manylinux docker container。您还需要一个类似于the one in the example的build-wheels.sh
,它将在该容器中运行。
首先,拉manylinux1
docker图像:
docker pull quay.io/pypa/manylinux1_x86_64
然后使用以下命令为受支持的python版本构建控制盘:
docker run --rm -v `pwd`:/io quay.io/pypa/manylinux1_x86_64 /io/build-wheels.sh
这将在dist
目录中创建控制盘:
$ ls dist hello_rust-1.0-cp27-cp27m-linux_x86_64.whl hello_rust-1.0-cp35-cp35m-linux_x86_64.whl hello_rust-1.0-cp27-cp27m-manylinux1_x86_64.whl hello_rust-1.0-cp35-cp35m-manylinux1_x86_64.whl hello_rust-1.0-cp27-cp27mu-linux_x86_64.whl hello_rust-1.0-cp36-cp36m-linux_x86_64.whl hello_rust-1.0-cp27-cp27mu-manylinux1_x86_64.whl hello_rust-1.0-cp36-cp36m-manylinux1_x86_64.whl
然后可以使用twine将manylinux1
轮上载到pypi。
生锈扩展
您可以使用rust extension类定义rust扩展:
RustExtension(名称、路径、参数=无、功能=无, rust_version=none,quiet=false,debug=false)
创建rust扩展的类。
参数str名称 扩展名的全名,包括任何包。 不是文件名或路径名,而是python点式名称。它是 如果扩展名使用 binsing.exec绑定模式。在这种情况下,第一个论点必须是 字典。字典的键对应于编译后的锈 二进制文件和值是python中可执行文件的全名 包裹。
参数str路径 cargo.toml清单文件的路径
参数[str]参数 要传递给货物的额外参数列表。
参数[str]特征 还要生成的功能列表
参数[str]rustc_标志 传递给rustc的参数列表,例如货物rustc——特性 <;features>;<;args>;--<;rustc_flags>;
参数str rust_版本 RUST编译器版本的语义版本——例如 >;1.14,<;1.16,默认值为none
波安安静 不回显货物的输出。默认值为false
参数布尔调试 控制是否将--debug或--release传递给cargo。如果设置 如果设置为“无”,则生成类型为“自动检测”。就地生成正在调试 否则建立释放。默认值:无
参数int绑定 控制正在使用哪个python绑定。结合。PYO3使用PYO3 绑定。rust cpython使用rust cpython绑定。no binding使用no 结合。binding.exec生成可执行文件。
参数int strip 从最终文件中删除符号。对调试生成不做任何操作。 strip.no-不删除符号(默认值)strip.debug-strip 调试符号删除所有-删除所有符号
参数布尔脚本 如果使用binding.exec,则为可执行文件生成控制台脚本。
原始参数 使用“-c target cpu=native”生成扩展或可执行文件
可选参数bool 如果为true,则扩展中的生成失败不会中止 构建进程,但不安装失败的 分机。
命令
- build-标准build命令生成所有rust扩展。
- build_rust-命令生成所有rust扩展。
- clean-标准clean命令执行货物清除所有锈迹 分机。
- 检查-标准检查命令执行货物检查,检查所有锈蚀 分机。
- tomlgen_rust-自动生成基于cargo.toml清单的 关于python包元数据。见example project 有关此命令的详细信息,请访问github。