9

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
QgsApplication.initQgis()
from sextante.core.Sextante import Sextante
Sextante.alglist()
Sextante.alghelp("saga:slopeaspectcurvature")

That I'm calling from my batch file

@echo off
set OSGEO4W_ROOT=C:\OSGeo4W
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
 QgsApplication.initQgis()
 # how???
 # qgis.utils.iface = QgisInterface.instance()
 Sextante.initialize()
 run_script(qgis.utils.iface)
def run_script(iface):
 """ this shall be called from Script Runner"""
 Sextante.alglist()
 Sextante.alghelp("saga:slopeaspectcurvature")
if __name__=="__main__":
 main()

However I get something like

Traceback (most recent call last):
 File "test.py", line 29, in
 main()
 File "test.py", line 20, in main
 Sextante.initialize()
 File "C:\Documents and Settings\user\.qgis\python\plugins\sextante\core\Sextante.py", line 94, in initialize
 Sextante.addProvider(GrassAlgorithmProvider())
 File "C:\Documents and Settings\user\.qgis\python\plugins\sextante\grass\GrassAlgorithmProvider.py", lin
e 17, in __init__
 self.actions.append(DefineGrassRegionAction())
 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.

ahmadhanb
41.8k5 gold badges55 silver badges109 bronze badges
asked Jul 2, 2012 at 18:46
1
  • You can't access iface in a standalone QGIS script. iface is only of use when running in side QGIS. Commented May 16, 2013 at 0:31

5 Answers 5

5

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)
 ldr.load_shapefiles('/vmap0_shapefiles')
answered Jul 2, 2012 at 20:36
4
  • 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. Commented Jul 2, 2012 at 21:55
  • Did you add the def run_script(iface) function? Your script will not run in Script Runner without that. Commented Jul 2, 2012 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. Commented Jul 2, 2012 at 22:27
  • Script Runner is updated here Commented Jul 22, 2014 at 17:16
3

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

Call Sextante.initialize() before doing anything.

answered Jul 28, 2012 at 10:28
1
  • 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? Commented May 15, 2013 at 6:49
3

For new users reading this post, there is a way of running QGIS processing algorithms in standalone PyQGIS scripts. Check answers to Import error for qgis.core when running OSGeo4w shell script and How can I access `processing` with Python?, which provide you with tested examples.

answered Jan 15, 2015 at 12:34
1
  • 1
    And the reason for the downvote is...? Commented Jan 15, 2015 at 19:37
2

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

eg from a batchfile:

@ECHO OFF
REM SET SAGA_MLB = C:\SAGA\Modules
REM SET PATH = %PATH%;C:\SAGA
saga_cmd ta_morphometry "Slope, Aspect, Curvature" -ELEVATION=elevation.sgrd -SLOPE=slope.sgrd -ASPECT=aspect.sgrd -CURV=NULL -HCURV=NULL -VCURV=NULL -METHOD=5
PAUSE
answered Jul 2, 2012 at 18:58
1
  • I apologize, I should have made it clear. It was just an example. I am not going to use SAGA in particular. Commented Jul 2, 2012 at 19:05
2

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?

answered May 14, 2013 at 19:18
1
  • Yes, please check my answer. Commented Jan 16, 2015 at 18:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.