I can change user defined project variables by QgsExpressionContextUtils.setProjectVariable('myvar','hello world')

But how can I access/read this variable in a python script?

  • 1
    I cannot try this but documentation suggests that there should be a QgsExpressionContextUtils.projectScope method through which you can access that information.
    – Christoph
    Dec 24, 2016 at 8:23

2 Answers 2


Since QGIS 3.0 you need also to specify the project when setting and reading a variable:

project = QgsProject.instance()
QgsExpressionContextUtils.setProjectVariable(project,'myvar','Hello World!')

If you set a project variable using:

QgsExpressionContextUtils.setProjectVariable('myvar','hello world')

You can use the following to read the value of this variable:

>>> u'hello world'

Your Answer

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

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