setuptools,提前知道本机库的轮文件名 [英] setuptools, know in advance the wheel filename of a native library

查看:54
本文介绍了setuptools,提前知道本机库的轮文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以在运行安装脚本之前知道 Python 轮子的文件名?

Is there an easy way to know the filename of a Python wheel before running the setup script?

我正在尝试生成一个 Bazel 规则,该规则为机器中安装的每个 Python 版本构建一个 .whl,该库包含本机代码,因此需要为每个版本单独编译.Bazel 的问题是它需要提前声明任何输出,我观察到的是每个 Python 版本生成不同的文件名,没有明显的一致性(malloc 和 unicode 的前缀不同)

I'm trying to generate a Bazel rule that builds a .whl for each Python version installed in the machine, the library contains native code so it needs to be compiled for each version separately. The thing with Bazel is that it requires to declare any outputs in advance, and what I'm observing is that each Python version generates a different filename without obvious consistency (different prefixes for malloc and unicode)

2.7  --> lib-0.0.0-cp27-cp27mu-linux_x86_64.whl
3.6m --> lib-0.0.0-cp36-cp36m-linux_x86_64.whl
3.8  --> lib-0.0.0-cp36-cp38-linux_x86_64.whl

我知道作为一种解决方法,我可以拉动轮子来传递它,但我想知道是否有更简洁的方法来做到这一点.

I know as a workaround I could zip the wheel to pass it around, but I was wondering if there is a cleaner way to do it.

推荐答案

更新

另见更详细的答案此处.

您可以通过查询 bdist_wheel 命令获取名称,为此您甚至不需要构建任何东西或编写 setup.py 脚本(但您需要您传递给 setup 函数的元数据).示例:

You can get the name by querying the bdist_wheel command, for that you don't even need to build anything or writing a setup.py script (but you need the metadata you pass to the setup function). Example:

from distutils.core import Extension
from setuptools.dist import Distribution


fuzzlib = Extension('fuzzlib', ['fuzz.pyx'])  # the files don't need to exist
dist = Distribution(attrs={'name': 'so', 'version': '0.1.2', 'ext_modules': [fuzzlib]})
bdist_wheel_cmd = dist.get_command_obj('bdist_wheel')
bdist_wheel_cmd.ensure_finalized()

distname = bdist_wheel_cmd.wheel_dist_name
tag = '-'.join(bdist_wheel_cmd.get_tag())
wheel_name = f'{distname}-{tag}.whl'
print(wheel_name)

会打印出你想要的名字.请注意,传递给 Distributionattrs 应该包含您传递给 setup 函数的相同元数据,否则您可能会得到错误的标签.要重用元数据,可以在 setup.py 脚本中将其组合起来,例如

will print you the desired name. Notice that attrs passed to Distribution should contain the same metadata you pass to the setup function, otherwise you will likely get a wrong tag. To reuse the metadata, in a setup.py script this could be combined like e.g.

setup_kwargs = {'name': 'so', 'version': '0.1.2', ...}

dist = Distribution(attrs=setup_kwargs)
...
setup(**setup_kwargs)

这篇关于setuptools,提前知道本机库的轮文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆