How to support alternate dependencies in a Python package?

梦想的初衷 提交于 2021-01-28 11:46:51

问题


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:

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.


回答1:


Use extras_require:

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

and teach your users to run either

pip install 'yourpackage[pyqt5]'

or

pip install 'yourpackage[pyside2]'



回答2:


If you don't want to make either a strict requirement (which makes sense), I'd just throw a runtime error if neither is available.

For example

try:
   import PyQt5 as some_common_name
except ImportError:
   try:
       import PySide2 as some_common_name
   except ImportError:
       raise ImportError('Please install either PyQt5 or PySide2') from None



回答3:


My particular case is somewhat niche (so I am not accepting this as the answer). I realized the package was really doing two things: acting as a library and as a command line tool. I decided to split it into two packages: package and package-cli. package does not explicitly depend on PyQt5 or PySide2 but specifies that one of them must be installed in the documentation. Since package is a library, it is intended to be integrated into another project where it is easy to list package and PyQt5 together in the requirements.txt. For package-cli, I just choose one of PyQt5 or PySide2 to be the explicit dependency. package-cli depends on package and PyQt5 and just adds a console_script to call the main module in package.



来源:https://stackoverflow.com/questions/52286940/how-to-support-alternate-dependencies-in-a-python-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!