2

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 ?

2

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.

  • Just to clarify, your ultimate suggestion is to use the context manager? Could you clarify a reference to "what Nathan W does" – raphael Dec 30 '16 at 19:31

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.