# Gdal_calc.py problems from command line?

I hope someone can help me with this issue with gdal_calc.py. When I call gdal_calc.py from the command line, I get the following error (after displaying the error message I will describe my current environment):

C:\Users\xx> gdal_calc.py
RuntimeError: module compiled against API version 9 but this version of numpy is
7
Traceback (most recent call last):
File "C:\Anaconda2\Scripts\gdal_calc.py", line 46, in <module>
from osgeo import gdalnumeric
File "C:\Python27\ArcGIS10.3\lib\site-packages\osgeo\gdalnumeric.py", line 1,
in <module>
from gdal_array import *
File "C:\Python27\ArcGIS10.3\lib\site-packages\osgeo\gdal_array.py", line 26,
in <module>
_gdal_array = swig_import_helper()
File "C:\Python27\ArcGIS10.3\lib\site-packages\osgeo\gdal_array.py", line 22,
in swig_import_helper
_mod = imp.load_module('_gdal_array', fp, pathname, description)
ImportError: numpy.core.multiarray failed to import


I am running Windows 7, installed Anaconda and the command line calls Anaconda's python version, and I have installed ArcGIS (my employer requires to script in ArcGIS from time to time).

When I call gdal from the command line, it runs without problems. Also, whenever I run gdal_polygonize.py there is no issue. So far, I have only run into problems when running gdal_calc.py as it seems to access the osgeo installation in ArcGIS and not the osgeo installation in Anaconda.

The osgeo path in Anancondo is: C:\Anaconda2\Lib\site-packages\osgeo

The osgeo path in ArcGIS is: C:\Python27\ArcGIS10.3\Lib\site-packages\osgeo

Also, to confirm the Python installation accessed:

Python 2.7.11 |Anaconda 4.0.0 (32-bit)| (default, Mar  4 2016, 15:18:41) [MSC    v.
1500 32 bit (Intel)] on win32
Anaconda is brought to you by Continuum Analytics.


Your ArcGIS python is the system default. So when you run a python script without specifying which python to use, the ArcGIS one is used.

Don't upgrade the ArcGIS numpy, you can break arcpy.

You can run gdal_calc.py using the Anaconda python using something like path\to\anaconda\python path\to\gdal_calc.py etc...

If you don't want to do this every time you could create a gdal_calc.cmd file that contains path\to\anaconda\python path\to\gdal_calc.py %*

• thank you that is it. I will try to fix the registry tomorrow, and see if I can change the path environment so that I do not have to use this work around. gis.stackexchange.com/questions/86850/… – user1738753 Jun 10 '16 at 1:31
• No don't change the default python, you'll cause issues with ArcGIS – user2856 Jun 10 '16 at 1:32
• +1 for "Don't upgrade the ArcGIS numpy" – alextc Jul 31 '17 at 2:47

I just had this same problem. Including the full path to gdal_calc.py was only part of the solution for me.

Using Spyder (Anaconda) with Python 2.7, I had to include the statement:

os.system('python path\to\anaconda\python path\to\gdal_calc.py -A filename -B filename --outfile=out.tif --calc=(A+B)')

Also note that if using a numpy function in the --calc statement (i.e., fmax), then the calc statement should be written: --calc="numpy.fmax(A,B)"