使用pyo3、rust-cpython和cffi绑定以及作为python包的rust二进制文件构建和发布板条箱

pyo3-pack的Python项目详细描述


PYO3包

Linux和Mac构建状态windows构建状态crates.iopypi在gitter上聊天

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

这个项目是用来替代setuptools rust的零配置。它支持在windows、linux和mac上为python 3.5+构建控制盘,可以将它们上传到pypi,并具有基本的pypypy支持。

用法

您可以从最新版本下载二进制文件,也可以使用pip:

pip install pyo3-pack

您也可以从源代码安装pyo3包,尽管它是旧版本:

cargo install pyo3-pack

有三个子命令:

  • 发布将板条箱构建到python包中,并将其发布到pypi。
  • build生成控制盘并将其存储在文件夹中(默认情况下为target/wheels),但不上载它们。
  • 开发构建板条箱并将其作为python模块直接安装到当前的virtualenv中

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

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

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

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

您还可以在cargo.toml的package.metadata.pyo3 pack.classifier下指定trove分类器,例如:

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

PYO3和Rust Cpython

对于pyo3和rust-cpython,pyo3包只能为已安装的python版本构建包。在linux和mac上,使用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包将运行cbindgen并生成cffi绑定。您可以使用一个生成脚本覆盖它,该脚本将头写入target/header.h

<细节>自定义生成脚本的示例
usecbindgen;// Use `extern crate cbindgen` in rust 2015usestd::env;usestd::path::Path;fnmain(){letcrate_dir=env::var("CARGO_MANIFEST_DIR").unwrap();letbindings=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"的工具,以确保符合"manylinux1-policy" rel="nofollow">manylinux规则。

PYO3包包含自动检查生成库的AuditWheel的主要部分的重新实现。如果要禁用这些检查或为本机Linux目标生成,请使用--manylinux标志。

为了完全符合manylinux,您需要在cent os 5 docker容器中编译。konstin2/pyo3 pack图像基于正式的manylinux图像。您可以这样使用:

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

在为musl目标编译时,pyo3包本身是manylinux兼容的。发布页面上的二进制文件具有额外的keyring集成(通过密码存储功能),这不符合manylinux。

皮比

PYO3包可以用PYO3为PYPY制造轮子。请注意,pyo3中的pypy支持在撰写本文时尚未发布。还请注意,pypy与manylinux1不兼容,并且您不能将pypy-wheel发布到pypi pypypypypypy只有在手动和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包库,它有完整的文档记录,应该是可集成的。附带的main.rs负责pypi上载的用户名和密码,否则会调用库。

有三个不同的示例,它们也用于集成测试:使用pyo3绑定获取42个使用cffi绑定的点板条箱和作为二进制文件的hello worldsysconfig文件夹包含针对不同python版本和平台的python-msysconfig输出,这在开发过程中非常有用。

您需要安装virtualenv和cffi来运行测试。

你可能想查一下我的ref="https://blog.schuetze.link/2018/07/21/a-dive-into-packaging-native-python-extensions.html" rel="nofollow">博客文章解释了构建本机python包的复杂性。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java是用于日志记录的最佳NoSQL解决方案   是否使用iText(而不是acrobat reader)以java显示pdf文档?   java我如何初始化firebase在google appengine上托管的springboot应用程序   java Javafx菜单项白色边框   java奇怪的字符,而不是常规的空白   java Return语句在循环中无法正常工作   java Liskov的替换原理如何适用于对象之类的类?   java如何从JSON中的子对象访问内容?   REST方法中的java类层次结构   java在长字符串中查找和替换特定字符串集的优化方法是什么?   java netbeans gradle,部署到Wildfly   java如何对选定字段上的范围运行查找查询   java在Android应用程序中从服务器读取文本文件   java红黑树中红色节点的百分比   java正在尝试访问启动器提供程序