I am trying to write a Qgis plugin, which should work on Qgis2 and Qgis3. How can I find out which Qgis version the user is using, so the right imports are made?
Answer
In QGis < 3, qgis.core has a QGis object:
>>> from qgis.core import QGis
with various formats of the version string:
>>> QGis.QGIS_VERSION
u'2.18.5'
>>> QGis.QGIS_VERSION_INT
21805
and the release name, fwiw:
>>> QGis.QGIS_RELEASE_NAME
u'Las Palmas'
In versions 3 and above, this module is renamed Qgis for consistency.
There's a bit of a bootstrap problem here, since you can't get the version to figure out if you need to import Qgis or QGis without knowing. So you probably have to wrap it in a try and catch the exception.
try:
from qgis.core import Qgis
except ImportError:
from qgis.core import QGis as Qgis
No comments:
Post a Comment