Accessing `processing` with Python?

UPDATE 24.04.2018: See how to do this in QGIS v3.x here.


For QGIS v2.x

Finally found the proper way of running processing algorithms via standalone PyQGIS scripts.

Using Processing plugin version 2.2.0-2, you can try the following script:

# Prepare the environment
import sys
from qgis.core import *
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

# Prepare processing framework 
sys.path.append('/home/user/.qgis2/python/plugins') # Folder where Processing is located
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

# Run the algorithm
layerInput = QgsVectorLayer('test.shp', 'test', 'ogr')
general.runalg('qgis:explodelines', layerInput, 'temp.shp')

# Exit applications
QgsApplication.exitQgis()
QApplication.exit()

Newer Processing versions could be located at /usr/share/qgis/python/plugins, so you might need to use sys.path.append('/usr/share/qgis/python/plugins') accordingly.

I found the working example in Error: Algorithm not found, which is in turn based on a Qgis-dev mailing-list discussion.


Until I get to work the generic way of doing it, I will tell you a workaround.

I use the Processing plugin version 2.2.0-2 (I suggest you to use this version), which is installed in /home/germap/.qgis2/python/plugins/ on my computer. You need to know this folder location, because you import the processing module from there.

Since you know the provider (qgis) and the algorithm (explodelines), you can look at /home/germap/.qgis2/python/plugins/processing/algs/qgis/ to take the explode lines script name: Explode.py This information allows you to import the algorithm directly to your Python standalone script.

So, open a Python console and copy the following script (I use GNU/Linux, so the environment variables are set by default, allowing me to import qgis and PyQt4 libraries in a breeze):

# Prepare the environment
import sys
from PyQt4.QtGui import *
from qgis.core import *
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

# Tell Python where you will get processing from
sys.path.append('/home/germap/.qgis2/python/plugins')

# Reference the algorithm you want to run
from processing.algs.qgis.Explode import *
alg = Explode() 

# Set input and output
inLayer = QgsVectorLayer('/home/user/data/in.shp', 'input', 'ogr')
outLayer = '/home/user/data/out.shp'
alg.setParameterValue('INPUT', inLayer)
alg.setOutputValue('OUTPUT', outLayer)

# Run the algorithm
from processing.core.SilentProgress import SilentProgress
progress = SilentProgress()
alg.processAlgorithm(progress)

If you don't get error messages, that's it. The output layer has been saved in the output path you specified (/home/user/data/out.shp)

Note: Regarding a generic way (i.e., calling algorithms by name), I've found some troubles that I need to solve before posting it. As soon as I get it to work, I'll post it.