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?
Best Answer
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.