On 06/03/2021 12:00, Manfred Lotz wrote:
Let us say I have a package which reads a TOML file.
I want to give the user of my package the choice to decide if he wants
to use the toml, tomlkit or rtoml package.
So, in case the user chose to use rtoml then there should be an import
only for rtoml, aso.
How could I achieve this?
I expect in this case the best thing to do is to simply try to import
your favourite TOML module (rtoml or what have you), and if that doesn't
work import a fallback – accepting that there might be two TOML parser
modules loaded in the same process in some cases.
But this dilemma is fairly common in the Qt GUI world, as there are two
competing Python bindings for the Qt framework (and multiple
nearly-compatible versions of both) and in that case all packages need
to use the same one. So you could have a look at what modules depending
on Qt do.
For example:
https://pyqtgraph.readthedocs.io/en/latest/how_to_use.html#pyqt-and-pyside
PyQtGraph checks if any supported Qt bindings are already imported and
uses those. If not, it imports its own favourite.
The rules are fairly complex in their case (they're supporting 6
almost-but-not-quite-compatible backends), so they have an internal
wrapper module
https://github.com/pyqtgraph/pyqtgraph/blob/master/pyqtgraph/Qt.py
and do something like
from . import Qt
in every file.
Matplotlib does it the other way around:
https://matplotlib.org/stable/api/matplotlib_configuration_api.html#matplotlib.use
there the user imports matplotlib and then optionally calls a function
to select a backend. Then every function within matplotlib that uses the
backend has to defer to some wrapper (not sure where or how)
Hope that helps
Thomas
--
Dr. Thomas Jollans
e ✉ t...@tjol.eu
--
https://mail.python.org/mailman/listinfo/python-list