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.


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


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."


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

# Prepare processing framework 
import processing
from processing.core.Processing import Processing
from processing.script.ScriptUtils import *

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.