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?


In QGis < 3, qgis.core has a QGis object:

>>> from qgis.core import QGis

with various formats of the version string:


and the release name, fwiw:

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.

    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

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.