Search code examples
pythonpackagingpython-wheel

How to support alternate dependencies in a Python package?


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.


Solution

  • 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]'