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

Mark m.n.summerfield at googlemail.com
Fri Jul 6 02:53:13 EDT 2018


On Thursday, July 5, 2018 at 6:24:09 PM UTC+1, Tim Williams wrote:
> On Thu, Jul 5, 2018 at 9:02 AM Mark Summerfield via Python-list <
> python-list at python.org> 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.
> >
> > But I'd really rather just have one lot of imports per file.
> >
> > One obvious solution is to create a 'Qt.py' module that imports everything
> > depending on the PYSIDE switch and that I then use in all the other .py
> > files, something like this:
> >
> > from Qt.QtCore import Qt
> > from Qt.QtGui import QIcon
> > ... etc.
> >
> > But I'm just wondering if there's a nicer way to do all this?
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> 
> 
> Check out pyqtgraph <http://pyqtgraph.org/>
> 
> It tries to use  PyQt5/PyQt4/PySide depending on which if these packages
> were imported before importing pyqtgraph.

I looked at the source for this and it is v. similar to what I'm doing myself right down to having an isObjectAlive() function done exactly as I'm doing it.

The thing I'm not keen on is that the imports are like this:

from .Qt import QtCore

and then used as:

p = QtCore.QPoint(0, 0)

whereas I want to do something like this (incorrect & maybe not possible):

from .Qt.QtCore import QPoint, QRect

p = QPoint(0, 0)



More information about the Python-list mailing list