1

I've been trying to use subprocess for a while now, but can't get it to work when calling QGIS modules. Here's my script:

import subprocess

call("processing.runalg('gdalogr:merge', 
 [rasterin1,rasterin2],'false','false',5,rasterout)")

No matter the input rasters I use (or how I write their paths: I assume they should contain / , not \ to separate folders), I always get this error :

Traceback (most recent call last):
   File "<input>", line 1, in <module>
   File "C:\OSGEO4~1\apps\Python27\lib\subprocess.py", line 711, in __init__
     errread, errwrite)
   File "C:\OSGEO4~1\apps\Python27\lib\subprocess.py", line 948, in _execute_child
     startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

I used gdalogr here (and it doesn't work), but could have used saga:module or grass:module.

The script works perfectly in the console (i.e. when not wrapped by subprocess.call).

I also note people use subprocess successfully to run gdal (Use subprocess.call with gdalwrap), so why subprocessing the processing module would not work?

Any clues?

1 Answer 1

3

Subprocess can be used to start new processes - i.e. you use it to execute command line tools from within python. Gdalwarp is a command line tool - processing.runalg is not - hence your Windows error that it can't find it.

What you are trying to launch is not a command line tool but a method of the QGIS python module.

If you want to use that inside a Python script independent of the QGIS command line have a look at pyQgis. You can import QGIS functions into any Python script and execute them from there. No need to use subprocess.

2
  • Thanks a lot Kersten. I should have guessed it was not possible to use <subprocess> with <processing> from the lack of posts on this matter here and elsewhere. The reason why I needed <subprocess> is that I am writing a script in python for QGIS console, which involves a lot of rasters and very large loops. However, at one point I get errors due to memory problems, and I would very much like to split those loops in smaller chunks to be able to delete intermnediate results and free memory. How do I do that (or simulate subprocess) in pyQgis or through other modules/methods? Thanks again
    – lobelius
    Commented Jun 1, 2015 at 13:14
  • This sounds like you are reaching the limits of what the QGIS console was developed for. Have you considered writing your code in Python directly and do the processing with help of GDAL, PyQGIS and possibly Numpy. Also I don't see why you shouldn't be able to divide large loops into smaller chunks in QGIS since Python takes care of clearing memory after a finished loop.
    – Kersten
    Commented Jun 1, 2015 at 14:00

Your Answer

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

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