2

I'm trying to write a standalone script in qgis 2.10 in ubuntu I have written a script which crashes when I run from bash but works fine from inside the qgis desktop environment. The script is:

#!/usr/bin/python2.7
from qgis.core import *
from platform import python_version
print python_version()
# supply path to where is your qgis installed
QgsApplication.setPrefixPath("/usr", True)
# load providers
QgsApplication.initQgis()

input_file = "/path/to/shape/file.shp"
input_layer = QgsVectorLayer(input_file, "input layer", "ogr")
QgsMapLayerRegistry.instance().addMapLayer(input_layer)
print str(input_layer.featureCount())
print input_layer.dataProvider().crs().authid()
for feature in input_layer.getFeatures():
    pass
print "hello"

QgsApplication.exitQgis()

When I run from within the qgis DE I get:

2.7.6
105777
EPSG:2193
hello

When I run from bash I get:

2.7.6
105777
EPSG:2193
Segmentation fault (core dumped)

The only thing I can guess is my interpreter choice in my shebang but none of the python executables I can find make any difference? and given the response to the version function that seems unlikely. For the shebang I also tried

#!/usr/bin/env python

with the same result.

I've seen the docs on standalone scripts here and I figured out the correct(?) path to the installation from here but I'm stumped as to the cause of this crash.

  • 1
    Not completely sure but perhaps try adding app = QApplication([]) after your QgsApplication.initQgis(); and app.exit() after your QgsApplication.exitQgis()? – Joseph Jul 3 '15 at 12:31
  • 2
    I think you mean: app = QgsApplication([],False, None) but yes that did it. If you make it an answer I'll accept it. Reading the docs on "QgsApplication" would probably be warranted for me at this point :) – Mr Purple Jul 3 '15 at 21:46
  • I wish I meant that! But I only compared your script with several others using a linux-based system. So it's only fair that you post it as an answer (I will upvote it). Reading the manual can be useful but who has time for it =) – Joseph Jul 6 '15 at 12:27
2

Thanks to promting by Joseph I had a look at the docs for the application class reference. In order to utilise functions from qgis in your script you need to add a qgis application class reference object.

 app = QgsApplication([],False, None)

after the

QgsApplication.initQgis()

line and

app.exit()

At the end.

The docs describe the parameters but I was able to fill them as shown in order to run the script as a python shell script.

Also note that arguments for the script you write can be passed to the script from the shell as described here. I used the answer by wewa.

Also, thanks to a suggestion by Ole in this SE question I can now run *any custom pyQGIS process/script/function in parallel (not just functions available within the shell as described in my answer to the above linked SE question) using the above script plus the answer here as a wrapper.

Perhaps anyone else doing the same or similar may find this useful too. My next step is to pass those parallel processes to a server(farm) and speed my custom scripts up even more.

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.