[GIS] Can’t get gdal_merge Python script to run

gdalgdal-mergepython-2.7

I downloaded and installed GDAL. I am using Python 27. I had to use the following commands at the IDLE Python GUI:

from osgeo import gdal,ogr
sys.path.append('C:\Program Files\GDAL')
import gdal_merge

I actually have C:\Program Files\GDAL listed first in my Path environmental system variables, so I don't know why I need to do the above.
Anyway, when I try to use this utility, I don't get an error, but it only opens the Python script for editing, and doesn't run the script. I know that the documentation for gdal_merge states "NOTE: gdal_merge.py is a Python script, and will only work if GDAL was built with Python support." I did install GDAL with Python support, and can get some utilities to run correctly (like gdalwarp.exe) but have trouble running the python scripts.

How can I find out for sure if my installation of GDAL has Python support?

Best Answer

If you can do from osgeo import gdal,ogr your GDAL was built with the Python bindings otherwise you wouldn't be able to import it.

If you want to start a GDAL utility (gdalwarp, gdal_translate, gdal_merge) from within Python your best bet is to use Pythons subprocess module as these utilities are spawned from the command line.

An example:

import subprocess

merge_command = ["python", "gdal_merge.py", "-o", "output.tif", "input1.tif", "input2.tif", "input3.tif", "inputN.tif"]
subprocess.call(merge_command)