如何支持 Python 包中的替代依赖项? [英] How to support alternate dependencies in a Python package?

查看:89
本文介绍了如何支持 Python 包中的替代依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Python 编写了一个可与 Qt 框架配合使用的实用程序库.我的代码是纯 Python 并且兼容 PyQt5 和 PySide2.我的主模块可以使用 python -m 从命令行单独运行,也可以导入到另一个项目中.有没有一种干净的方法来指定项目在其轮分布中需要 PyQt5 还是 PySide2?

I have written a utility library in Python that works with the Qt framework. My code is pure Python and is compatible with both PyQt5 and PySide2. My main module could either be run on its own from the command line with python -m or it could be imported into another project. Is there a clean way to specify that the project needs either PyQt5 or PySide2 in its a wheel distribution?

这是我在研究中发现的,但我想问问是否有比这些选项更好的打包项目的方法:

Here is what I have found in my research but I am asking in case there is a better way to package the project than these options:

我可以在项目的源代码分发中向 setup.py 添加逻辑以检查 PyQt5 和 PySide2.但是,推荐使用轮子来分发 Python 项目,据我所知,轮子不可能实现这种安装时逻辑.或者,我无法指定 PySide2 或 PyQt5 作为依赖项,并在安装说明中建议将其中之一与我的项目一起安装.

I could add logic to setup.py in a source distribution of the project to check for PyQt5 and PySide2. However, wheels are recommended way to distribute Python projects, and from what I can tell this kind of install-time logic is not possible with wheels. Alternatively, I could not specify either PySide2 or PyQt5 as dependencies and recommend in the install instructions that one of them be installed together with my project.

推荐答案

使用 extras_require:

setup(
    …
    extras_require={
        'pyqt5': ['PyQt5'],
        'pyside2': ['PySide2'],
    },
)

并教您的用户运行

pip install 'yourpackage[pyqt5]'

pip install 'yourpackage[pyside2]'

这篇关于如何支持 Python 包中的替代依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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