3

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?

10

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
  • Thank you, just checked the Qgis3 API and they renamed it from QGis to Qgis. Could you add that to your answere? – Blinxen Jan 18 '18 at 15:55
5

You can call simply try this inside your plugin

from qgis.core import *
print QGis.QGIS_VERSION # will print 2.18.13

I dont know if the function is the same in QGIS 3.0 but for 2.18 this worked. The link to the reference can be found here in the pyqgis documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.