[英]How can I make a Python package that is platform-specific?
I have a Python package that can only meaningfully run on Linux. 我有一个只能在Linux上有意义地运行的Python包。 I've seen in the docs that there is a
platforms
key in the setup.py
metadata, but as far as I can tell from poking around in the distutils
source, this metadata isn't actually used for anything. 我在文档中看到
setup.py
元数据中有一个platforms
键,但据我所知,在distutils
源代码中,这个元数据实际上并没有用于任何事情。
Next, I went and looked at PyObjC, a prominent OS X-only Python package. 接下来,我去看了PyObjC,一个着名的OS X-only Python包。 I observe that it populates the aforementioned
platforms
key in its setup.py
. 我观察到它在
setup.py
填充了前面提到的platforms
键。 However, when I try to install PyObjC on Linux, the installation isn't prevented or blocked in any intentional way. 但是,当我尝试在Linux上安装PyObjC时,不会以任何有意的方式阻止或阻止安装。 The failure mode that ensues is pretty ungraceful: it errors out when
platform.mac_ver()
returns a value it doesn't expect. 接下来的失败模式非常糟糕:当
platform.mac_ver()
返回它不期望的值时,它会出错。 I tried manually fixing that problem, and distutils
appeared to be going about its merry way collecting dependencies, etc. until eventually it failed looking for a platform specific file... Bottom line, distutils
is not handling platform-specific packages in any reasonable way. 我尝试手动修复这个问题,并且
distutils
似乎正在以其快乐的方式收集依赖关系等等,直到最终它找不到特定于平台的文件...底线, distutils
没有以任何合理的方式处理特定于平台的包。
Ideally, I would expect the installation to fail with some message indicating that the package is not compatible with the current platform. 理想情况下,我希望安装失败,并显示一些消息,指出该软件包与当前平台不兼容。 I've noodled around a bit and the "best" thing I've been able to come up with involves subclassing the
install
and develop
commands and handling a platform check manually there. 我已经涂了一点,我能够提出的“最好”的东西包括子类化
install
和develop
命令并在那里手动处理平台检查。
Is there a better way? 有没有更好的办法?
I am probably oversimplifying, but at the top of your setup.py
couldn't you just do something like: 我可能过于简单了,但在你的
setup.py
的顶部,你不能只做以下事情:
import platform
distname,version,id = platform.linux_distribution()
if not distname:
raise Exception('Aborting installation: Requires Linux')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.