2

I am using a headless QGIS Processing script and found out that import errors (e.g. due to a missing library) lead to a silent fail. Neither does the object returned by processing.runalg contain info about an error nor is anything printed to the console.

This is not desirable behaviour and cost me quite some time to even realize that something went wrong.

To reproduce this problem put the two example scripts in a folder named scripts and run run_script.py in its parent folder.

scripts/my_script.py

print "Hello world. This is the processing script speaking."

scripts/my_broken_script.py

import unknown_library

print "Hello world. This is the broken processing script speaking."
print "Unfortunately you won't see this because the import above will fail silently."

run_script.py

import sys

# Prepare the environment
from qgis.core import *
from PyQt4.QtGui import QApplication

# ironic: we enable the GUI for headless operation, otherwise we get many errors like this:
# QPixmap: Cannot create a QPixmap when no GUI is being used
guiEnabled = True
qgis = QgsApplication([], guiEnabled) 

# supply path to where is your qgis installed
qgis.setPrefixPath("/usr", True)

# load providers
qgis.initQgis()

# Prepare processing framework 
sys.path.append('/usr/share/qgis/python/plugins')
import processing
from processing.core.Processing import Processing
from processing.script.ScriptUtils import *
Processing.initialize()

scripts = ScriptUtils.loadFromFolder('scripts')
name2script = {script.name: script for script in scripts}
print 'available custom scripts: %s' % (name2script.keys())

processing.runalg(name2script["my script"])
processing.runalg(name2script["my broken script"])

I noticed that if I run the script in QGIS (with GUI) a window pops up and tells me "no module named unknown_library" as expected. Is there any way to find out if processing.runalg was successful programmatically for headless scripts?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.