[GIS] different way to build qgis python standalone application

pyqgispythonqgis

There seems to be 2 different ways to start a Python QGIS standalone script.
The official tutorial is using :

from qgis.core import *
# supply path to where is your qgis installed
QgsApplication.setPrefixPath("/path/to/qgis/installation", True)
# load providers
QgsApplication.initQgis()

But I have also seen code looking like this :

from qgis.core.contextmanagers import qgisapp
with qgisapp() as app:
    #do stuff

For example here and here.

I didn't see any reference to the latter method on the official doc.

Does anybody know if there is a difference between the two ?

Best Answer

If you look at the source code for the context manager qgisapp:

https://github.com/qgis/QGIS/blob/master/python/core/contextmanagers.py

you'll see it is just a neat wrapper for running code inside a QgsApplication context.

Following what Nathan W does with his Qgis python is usually a good idea, so I'd go with this.