Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm kind of stuck trying to figure out the way to run sextante from a standalone python from OSGeo4W distribution. The reason I want to do this is that I got tired entering parameters in the dialog every time I want to test a model from Model Builder.

So here is the python script let's call it test.py

# as per http://qgis.org/pyqgis-cookbook/intro.html#using-pyqgis-in-custom-application
from qgis.core import *
# supply path to where is your qgis installed
QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis", True)
# load providers

from sextante.core.Sextante import Sextante

That I'm calling from my batch file

@echo off

set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%OSGEO4W_ROOT%\apps\qgis\python\plugins;%HOME%/.qgis/python/plugins
set PATH=%OSGEO4W_ROOT%\bin;%OSGEO4W_ROOT%\apps\qgis\bin;%OSGEO4W_ROOT%\apps\qgis\plugins

python test.py

The problem is that it says Algorithm not found whereas I get meaningful output from QGIS python console.

I feel like I'm missing to initialize something. But what?

Is there a better way to test a Model other than via entering tons of parameters using GUI?

UPDATE 7/2/2012

I'm looking for generic pythonic solution to test with "mine" algorithms. Aforementioned algorithm is just an example showing that something probably was not initialized.

UPDATE 7/27/2012

An alternative to Script Runner is to use IPython console to debug scripts. Other than that there does not seem to be a way to do simple unit testing with sextante with nothing else running:(

UPDATE 7/30/2012

As Victor Olaya suggests, I try to initialize Sextante like in the code below.

#!/usr/bin/env python

import sys
from PyQt4.QtGui import QApplication
from sextante.core.Sextante import Sextante

def main():
    """ main function or something """
    # as per http://qgis.org/pyqgis-cookbook/intro.html#using-pyqgis-in-custom-application
    from qgis.core import *
    import qgis.utils

    app = QApplication(sys.argv)
    # supply path to where is your qgis installed
    QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis", True)
    # load providers
    # how???
    # qgis.utils.iface = QgisInterface.instance()

def run_script(iface):
    """ this shall be called from Script Runner"""

if __name__=="__main__":

However I get something like

Traceback (most recent call last):
  File "test.py", line 29, in
  File "test.py", line 20, in main
  File "C:\Documents and Settings\user\.qgis\python\plugins\sextante\core\Sextante.py", line 94, in initialize
  File "C:\Documents and Settings\user\.qgis\python\plugins\sextante\grass\GrassAlgorithmProvider.py", lin
e 17, in __init__
  File "C:\Documents and Settings\user\.qgis\python\plugins\sextante\grass\DefineGrassRegionAction.py", li
ne 16, in __init__
    canvas = QGisLayers.iface.mapCanvas()
AttributeError: 'NoneType' object has no attribute 'mapCanvas'

Well... it all becomes a mailing list discussion alike. Perhaps it worth moving to qgis-user or qgis-developer instead of SE.

share|improve this question
You can't access iface in a standalone QGIS script. iface is only of use when running in side QGIS. –  Nathan W May 16 '13 at 0:31

4 Answers 4

You could craft your script to work with Gary Sherman's Script Runner plugin and run it from within QGIS. Re-running the script, after editing, should prompt Script Runner to reload the module and reflect your changes. See also: Script Runner's plugins.qgis.org listing.

The essentials are to make sure you have a run_script function, which gets called by Script Runner (example from his blog):

def run_script(iface):
    ldr = Loader(iface)
share|improve this answer
While in theory it should indeed help to debug (though inside QGIS), it looks like it is broken on Windows. It keeps saying AttributeError: 'module' object has no attribute 'run_script' and keep insisting that I have no docstrings that I can see in source viewer. –  mlt Jul 2 '12 at 21:55
Did you add the def run_script(iface) function? Your script will not run in Script Runner without that. –  dakcarto Jul 2 '12 at 22:07
Apparently one should not name script as test :-) mytest works okay so far. It would be nice if __import__ scope can be constrained instead of looking all over sys.path. It shows docstring and list of other functions. –  mlt Jul 2 '12 at 22:27
Script Runner is updated here –  Dave X Jul 22 at 17:16

Sextante has to be initialized, so it loads the algorithms and can later execute them.

Call Sextante.initialize() before doing anything.

share|improve this answer
Hi Victor, is this still the case? I'm struggling with how to call Sextante in a standalone script (outside of QGIS) and finding all examples I google to not work. This was posted a year ago so I wonder if the architecture of sextante has changed? –  Rich May 15 '13 at 6:49

Since the algorithm you want to use is part of saga, you could use saga directly.

eg from a batchfile:



saga_cmd ta_morphometry "Slope, Aspect, Curvature" -ELEVATION=elevation.sgrd -SLOPE=slope.sgrd -ASPECT=aspect.sgrd -CURV=NULL -HCURV=NULL -VCURV=NULL -METHOD=5

share|improve this answer
I apologize, I should have made it clear. It was just an example. I am not going to use SAGA in particular. –  mlt Jul 2 '12 at 19:05

According to How to run a simple python script for QGIS from outside (e.g. Sublime Text)? you can't get a reference to the iface object here because it doesn't exist in this context since this is being run outside of QGIS. Any progress?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.