如何在构建时强制使用python wheel特定于平台?

问题描述 投票:7回答:2

我正在开发一个python2包,其中setup.py包含一些自定义安装命令。这些命令实际上构建了一些Rust代码并输出一些移动到python包中的.dylib文件。

重要的一点是Rust代码在python包之外。

如果python包是纯python或特定于平台(如果它包含一些C扩展),setuptools应该自动检测。在我的例子中,当我运行python setup.py bdist_wheel时,生成的轮子被标记为纯粹的python轮:<package_name>-<version>-py2-none-any.whl。这是有问题的,因为我需要在不同的平台上运行此代码,因此我需要为每个平台生成一个轮子。

在构建车轮时,是否有办法强制构建特定于平台?

python setuptools setup.py python-wheel
2个回答
11
投票

这是我通常从uwsgi看到的代码

基本方法是:

setup.py

# ...

try:
    from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
    class bdist_wheel(_bdist_wheel):
        def finalize_options(self):
            _bdist_wheel.finalize_options(self)
            self.root_is_pure = False
except ImportError:
    bdist_wheel = None

setup(
    # ...
    cmdclass={'bdist_wheel': bdist_wheel},
)

root_is_pure位告诉车轮机械制造非purelib(pyX-none-any)车轮。您还可以通过说有二进制平台特定的组件但没有cpython abi特定组件来获取fancier


1
投票

模块setuptoolsdistutilswheel通过检查它是否具有ext_modules来决定python分布是否是纯粹的。

如果您自己构建外部模块,您仍然可以在ext_modules中列出它,以便构建工具知道它存在。诀窍是提供一个空的源列表,以便setuptoolsdistutils不会尝试构建它。例如,

setup(
    ...,
    ext_modules=[
        setuptools.Extension(
            name='your.external.module',
            sources=[]
        )
    ]
)

这个解决方案对我来说比修补bdist_wheel命令更好。原因是bdist_wheel在内部调用install命令,该命令再次检查ext_modules的存在,以决定purelibplatlib安装。如果没有列出外部模块,最终会在车轮内的purelib子文件夹中安装lib。这在使用auditwheel repair时会引起问题,purelib抱怨扩展安装在qazxswpoi文件夹中。

© www.soinside.com 2019 - 2024. All rights reserved.