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?
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!')
QgsExpressionContextUtils.projectScope(project).variable('myvar')
If you set a project variable using:
QgsExpressionContextUtils.setProjectVariable('myvar','hello world')
You can use the following to read the value of this variable:
QgsExpressionContextUtils.projectScope().variable('myvar')
>>> u'hello world'
QgsExpressionContextUtils.projectScopemethod through which you can access that information. – Christoph Dec 24 '16 at 08:23