OSDir


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Is there a nice way to switch between 2 different packages providing the same APIs?


On 05-07-18 14:57, Mark Summerfield via Python-list wrote:
> For GUI programming I often use Python bindings for Qt.
>
> There are two competing bindings, PySide and PyQt.
>
> Ideally I like to have applications that can use either. This way, if I get a problem I can try with the other bindings: if I still get the problem, then it is probably me; but if I don't it may be an issue with the bindings.
>
> But working with both means that my imports are very messy. Here's a tiny example:
>
> if PYSIDE: # bool True -> Use PySide; False -> Use PyQt5
>     from PySide2.QtCore import Qt
>     from PySide2.QtGui import QIcon
>     from PySide2.QtWidgets import (
>         QDialog, QFrame, QGridLayout, QLabel, QLineEdit, QPushButton,
>         QVBoxLayout)
> else:
>     from PyQt5.QtCore import Qt
>     from PyQt5.QtGui import QIcon
>     from PyQt5.QtWidgets import (
>         QDialog, QFrame, QGridLayout, QLabel, QLineEdit, QPushButton,
>         QVBoxLayout)
>
> The PYSIDE constant is imported from another module and is used for all .py files in a given project so that just by changing PYSIDE's value I can run an entire application with PySide2 or with PyQt5.

The following is untested but what about

if PYSIDE:
    import PySide2 as PyQt
else:
    import PyQt5 as PyQt

Qt = PyQt.QtCore.Qt
QIcon = PyQt.QtGui.QIcon
...

-- 
Antoon Pardon