跳至主要内容

使用pyo3、rust-cpython和cffi绑定以及rust二进制文件作为Python包来构建和发布crates

项目描述

Pyo3-pack

Linux and Mac Build Status Windows Build status Crates.io PyPI Chat on Gitter

使用pyo3、rust-cpython和cffi绑定以及rust二进制文件作为Python包来构建和发布crates。

该项目原本旨在作为setuptools-rust的无配置替代品。它支持在windows、linux和mac上构建python 3.5+的wheels,可以上传到pypi,并具有基本的pypy支持。

用法

您可以从最新版本下载二进制文件,或者使用pip安装。

pip install pyo3-pack

您还可以从源代码安装pyo3-pack,尽管它是一个较旧的版本。

cargo install pyo3-pack

有三个主要子命令

  • publish 将crate构建成Python包并将其发布到PyPI。
  • build 构建wheels并将它们存储在一个文件夹中(默认为target/wheels),但不会上传它们。
  • develop 构建crate并将其作为Python模块直接安装到当前虚拟环境中

pyo3rust-cpython 绑定将自动检测,对于cffi或二进制文件,您需要传递 -b cffi-b bin。pyo3-pack不需要额外的配置文件,也不会与现有的setuptools-rust或milksnake配置冲突。您甚至可以将其与测试工具(如tox)集成(请参阅get-fourtytwo示例)。

包的名称将是Cargo项目的名称,即Cargo.toml中[package]部分的名称字段。您导入时使用的模块名称将是[lib]部分的name值(默认为包的名称)。对于二进制文件,它只是cargo生成的二进制文件名称。

Pip 允许添加所谓的控制台脚本,这些脚本是在程序中执行某些功能的 shell 命令。您可以在 [package.metadata.pyo3-pack.scripts] 部分添加控制台脚本。键是脚本名称,而值是以 some.module.path:class.function 格式的函数路径,其中 class 部分是可选的。函数无参数调用。示例

[package.metadata.pyo3-pack.scripts]
get_42 = "get_fourtytwo:DummyClass.get_42"

您还可以在 package.metadata.pyo3-pack.classifier 下您的 Cargo.toml 中指定 trove 类别,例如

[package.metadata.pyo3-pack]
classifier = ["Programming Language :: Python"]

pyo3 和 rust-cpython

对于 pyo3 和 rust-cpython,pyo3-pack 只能为已安装的 Python 版本构建软件包。在 Linux 和 macOS 上,使用 PATH 中的所有 Python 版本。如果您没有使用 -i 设置自己的解释器,将使用启发式方法来搜索 Python 安装。在 Windows 上,使用 Python 捕获器(由 python.org 安装程序默认安装)的所有版本和除 base 之外的所有 conda 环境。您可以使用 list-python 子命令检查捕获的版本。

pyo3 将在环境变量 PYTHON_SYS_EXECUTABLE 中设置使用的 Python 解释器,这可以用于自定义构建脚本。

Cffi

Cffi 轮子与所有 Python 版本兼容,但构建时使用的 Python 需要安装 cffipip install cffi)。

pyo3-pack 将运行 cbindgen 并生成 cffi 绑定。您可以使用写入 target/header.h 的头文件的构建脚本覆盖此操作。

自定义构建脚本示例
use cbindgen; // Use `extern crate cbindgen` in rust 2015
use std::env;
use std::path::Path;

fn main() {
    let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

    let bindings = cbindgen::Builder::new()
        .with_no_includes()
        .with_language(cbindgen::Language::C)
        .with_crate(crate_dir)
        .generate()
        .unwrap();
    bindings.write_to_file(Path::new("target").join("header.h"));
}

Manylinux 和 auditwheel

出于可移植性原因,Linux 上的原生 Python 模块只能动态链接到基本到处都安装的一组非常少的库,因此得名 manylinux。pypa 提供了一个特殊的 docker 容器和名为 auditwheel 的工具,以确保符合 manylinux 规则

pyo3-pack 自动检查生成的库,实现了 auditwheel 的主要部分。如果您想禁用这些检查或为原生 Linux 目标构建,请使用 --manylinux 标志。

要完全符合 manylinux 规范,您需要在一个 CentOS 5 docker 容器中编译。基于官方 manylinux 图像的 konstin2/pyo3-pack 映像。您可以这样使用它

docker run --rm -v $(pwd):/io konstin2/pyo3-pack build

当为 musl 目标编译时,pyo3-pack 本身符合 manylinux 规范。发布页面上的二进制文件具有额外的密钥环集成(通过 password-storage 功能),这不符合 manylinux 规范。

PyPy

pyo3-pack 可以使用 pyo3 为 pypy 构建 wheels。请注意,截至本文撰写时,pyo3 中的 pypy 支持尚未发布。另外请注意,pypy 与 manylinux1 不兼容,并且您不能将 pypy 轮子发布到 pypi。pypy 只在 Linux 上手动测试过。有关更多详细信息,请参阅 #115

构建

FLAGS:
    -h, --help
            Prints help information

        --release
            Pass --release to cargo

        --skip-auditwheel
            [deprecated, use --manylinux instead] Don't check for manylinux compliance

        --strip
            Strip the library for minimum file size

    -V, --version
            Prints version information


OPTIONS:
    -m, --manifest-path <PATH>
            The path to the Cargo.toml [default: Cargo.toml]

        --target <TRIPLE>
            The --target option for cargo

    -b, --bindings <bindings>
            Which kind of bindings to use. Possible values are pyo3, rust-cpython, cffi and bin

        --cargo-extra-args <cargo_extra_args>...
            Extra arguments that will be passed to cargo as `cargo rustc [...] [arg1] [arg2] --`

            Use as `--cargo-extra-args="--my-arg"`
    -i, --interpreter <interpreter>...
            The python versions to build wheels for, given as the names of the interpreters. Uses autodiscovery if not
            explicitly set.
        --manylinux <manylinux>
            Control the platform tag on linux.

            - `1`: Use the manylinux1 tag and check for compliance

            - `1-unchecked`: Use the manylinux1 tag without checking for compliance

            - `2010`: Use the manylinux2010 tag and check for compliance

            - `2010-unchecked`: Use the manylinux1 tag without checking for compliance

            - `off`: Use the native linux tag (off)

            This option is ignored on all non-linux platforms [default: 1]  [possible values: 1, 1-unchecked, 2010,
            2010-unchecked, off]
    -o, --out <out>
            The directory to store the built wheels in. Defaults to a new "wheels" directory in the project's target
            directory
        --rustc-extra-args <rustc_extra_args>...
            Extra arguments that will be passed to rustc as `cargo rustc [...] -- [arg1] [arg2]`

            Use as `--rustc-extra-args="--my-arg"`

发布

FLAGS:
        --debug
            Do not pass --release to cargo

    -h, --help
            Prints help information

        --no-strip
            Strip the library for minimum file size

        --skip-auditwheel
            [deprecated, use --manylinux instead] Don't check for manylinux compliance

    -V, --version
            Prints version information


OPTIONS:
    -m, --manifest-path <PATH>
            The path to the Cargo.toml [default: Cargo.toml]

        --target <TRIPLE>
            The --target option for cargo

    -b, --bindings <bindings>
            Which kind of bindings to use. Possible values are pyo3, rust-cpython, cffi and bin

        --cargo-extra-args <cargo_extra_args>...
            Extra arguments that will be passed to cargo as `cargo rustc [...] [arg1] [arg2] --`

            Use as `--cargo-extra-args="--my-arg"`
    -i, --interpreter <interpreter>...
            The python versions to build wheels for, given as the names of the interpreters. Uses autodiscovery if not
            explicitly set.
        --manylinux <manylinux>
            Control the platform tag on linux.

            - `1`: Use the manylinux1 tag and check for compliance

            - `1-unchecked`: Use the manylinux1 tag without checking for compliance

            - `2010`: Use the manylinux2010 tag and check for compliance

            - `2010-unchecked`: Use the manylinux1 tag without checking for compliance

            - `off`: Use the native linux tag (off)

            This option is ignored on all non-linux platforms [default: 1]  [possible values: 1, 1-unchecked, 2010,
            2010-unchecked, off]
    -o, --out <out>
            The directory to store the built wheels in. Defaults to a new "wheels" directory in the project's target
            directory
    -p, --password <password>
            Password for pypi or your custom registry. Note that you can also pass the password through
            PYO3_PACK_PASSWORD
    -r, --repository-url <registry>
            The url of registry where the wheels are uploaded to [default: https://upload.pypi.org/legacy/]

        --rustc-extra-args <rustc_extra_args>...
            Extra arguments that will be passed to rustc as `cargo rustc [...] -- [arg1] [arg2]`

            Use as `--rustc-extra-args="--my-arg"`
    -u, --username <username>
            Username for pypi or your custom registry

开发

FLAGS:
    -h, --help
            Prints help information

        --release
            Pass --release to cargo

        --strip
            Strip the library for minimum file size

    -V, --version
            Prints version information


OPTIONS:
    -b, --bindings <binding_crate>
            Which kind of bindings to use. Possible values are pyo3, rust-cpython, cffi and bin

        --cargo-extra-args <cargo_extra_args>...
            Extra arguments that will be passed to cargo as `cargo rustc [...] [arg1] [arg2] --`

            Use as `--cargo-extra-args="--my-arg"`
    -m, --manifest-path <manifest_path>
            The path to the Cargo.toml [default: Cargo.toml]

        --rustc-extra-args <rustc_extra_args>...
            Extra arguments that will be passed to rustc as `cargo rustc [...] -- [arg1] [arg2]`

            Use as `--rustc-extra-args="--my-arg"`

代码

主要部分是 pyo3-pack 库,该库完全有文档说明,应易于集成。伴随的 main.rs 负责处理 PyPI 上传的用户名和密码,并调用库的其他部分。

有三个不同的示例,它们也用于集成测试:使用 pyo3 绑定的 get_fourtytwo,具有 cffi 绑定的 points crate,以及作为二进制的 hello-world。在 sysconfig 文件夹中包含不同 Python 版本和平台的 python -m sysconfig 的输出,这在开发期间很有帮助。

要运行测试,您需要安装 virtualenvcffipip install virtualenv cffi)。

您可能想看看我的 博客文章,它解释了构建原生 Python 软件包的复杂性。

项目详情


下载文件

下载适合您平台的文件。如果您不确定该选择哪个,请了解更多关于安装包的信息。

源代码分发

本发布版本没有可用的源代码分发文件。请参阅生成分发存档的教程

构建分发

pyo3_pack-0.6.1-py2.py3-none-win_amd64.whl (5.0 MB 查看哈希值)

上传时间 Python 2 Python 3 Windows x86-64

pyo3_pack-0.6.1-py2.py3-none-win32.whl (4.5 MB 查看哈希值)

上传时间 Python 2 Python 3 Windows x86

pyo3_pack-0.6.1-py2.py3-none-manylinux1_x86_64.whl (5.1 MB 查看哈希值)

上传时间 Python 2 Python 3

pyo3_pack-0.6.1-py2.py3-none-manylinux1_i686.whl (5.1 MB 查看哈希值)

上传时间 Python 2 Python 3

pyo3_pack-0.6.1-py2.py3-none-macosx_10_7_x86_64.whl (5.2 MB 查看哈希值)

上传时间 Python 2 Python 3 macOS 10.7+ x86-64

由...