The environment variable (Win10) PYQGIS_STARTUP points to a python script that is executed before QGIS starts (cp. Central deployment of startup.py in QGIS).

Now I need to determine the QGIS version in the python script PYQGIS_STARTUP points to. Of course, this does not work (Answer to Getting user's QGIS version using PyQGIS?):

    from qgis.core import Qgis
except ImportError:
    from qgis.core import QGis as Qgis

if Qgis.QGIS_VERSION_INT >=30000:
    #do something
    #do something else or nothing

resulting in

ModuleNotFoundError: No module named 'qgis'

Is there another way to determine QGIS version in such script or am I lost?

1 Answer 1


Workaround: Determine Python version. Since PyQGIS 3 is allways python 3.X we can use:

import sys
if sys.version[0] == '3':
    #do something
elif sys.version[0] == '2':
    #do something else or nothing

cp. https://stackoverflow.com/questions/9079036/how-do-i-detect-the-python-version-at-runtime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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