Browsing the API at http://qgis.org/api and importing qgis and qgis.core I could not find any version information.

Is there a way to test in PyQGIS the version, like

if ( qgis.foo.VERSION == '2.99' ):
  print('on nightly')

So far I found PyQt:

In [11]: qgis.QtCore.PYQT_VERSION_STR
Out[11]: '5.5.1'

And I guess I could try&catch a new method.

But maybe there is a "cleaner" way to do this?


It is often more practical to use a numeric version, since it's easier to use in combination with comparison operators (>,<, ...).

For QGIS 3 (2.99)

qgis_version = qgis.core.Qgis.QGIS_VERSION_INT

For QGIS 2 and 3

    qgis_version = qgis.core.QGis.QGIS_VERSION_INT
except AttributeError:
    qgis_version = qgis.core.Qgis.QGIS_VERSION_INT

> 29900 # Meaning: 2.99.00; For 2.18.14 it is 21814

if qgis_version >= 29900:
    print('Congratulations, you are using a next generation GIS!')

To get a string

> '2.99.0-Master'
  • Thank you! With tab-completion I did not come to this result myself. – cprior Nov 29 '17 at 15:56
  • OP here, now with a username ;) I used the explanation to write three first unittests, available at github.com/cprior/sunlightmap/tree/master/application/physical/… I just applied for an OSGeo account because I get a message on stdout when verifying a QgsCoordinateRefernceSystem(): .../../src/core/qgscoordinatereferencesystem.cpp: 883: (getRecord) [0ms] failed : /resources/srs.db does not exist! Indeed 2.18.11 has a file in /usr/share/qgis/resources/srs.db (on Ubuntu 16.04) – cprior Nov 30 '17 at 19:47

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.