I and my colleagues are running into a very frustrating Python import error while using the OSGeo4W Shell. Any time we attempt to use gdal_calc in a Python script, we get the following error message:

Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python27\Scripts\gdal_calc.py", line 51, in <module>
    import numpy
  File "C:\OSGEO4~1\apps\Python27\lib\site-packages\numpy\__init__.py", line 142, in <module>
    from . import add_newdocs
  File "C:\OSGEO4~1\apps\Python27\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "C:\OSGEO4~1\apps\Python27\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
    from .type_check import *
  File "C:\OSGEO4~1\apps\Python27\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "C:\OSGEO4~1\apps\Python27\lib\site-packages\numpy\core\__init__.py", line 26, in <module>
    raise ImportError(msg)
Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.

Original error was: DLL load failed: %1 is not a valid Win32 application.

The script we're using works fine in Cygwin on multiple machines, so we know that it's not the script itself, but rather something to do with our OSGEO installations. I have installed and uninstalled numpy in my OSGEO4W64 Python2.7 installation numerous times, both using

pip install --update

and using Gohlke's pre-compiled Numpy+MKL binary wheel. My numpy and scipy package installations have all been the correct bit type for my Python installation (64 bit). Oddly enough, I can import numpy without issue when I call it directly in the OSGeo4W shell.

While I've seen numerous potential solutions for this error online, they almost universally apply to Anaconda installations, and I have no idea how to apply them to OSGEO4W.

A few things about my system: I'm running Win10 64bit, with the Python2.7 installation that came with OSGEO. My OSGEO and QGIS installs are up to date. For reasons beyond my control, I cannot switch to using Python 3.x, and I do not have Administrative access to my computer without a call to the company Helpdesk, so I generally cannot set my own Python environment paths. There is also another installation of Python2.7 on my computer courtesy of ArcGIS.

  • can you please describe the steps for invoking your script? – nickves Nov 29 '18 at 0:00
  • My script is contained within a python file, so I just use >>python MyScript.py – KMatGIS Nov 29 '18 at 14:10
  • Within the script itself, after import numpy from OSGEO import gdal The code is cmd2 = 'gdal_calc.py -A {0} --calc="-9999*(logical_or(A>10000,A<-1000)) + A*(logical_and(A<=10000,A>=-1000))" --NoDataValue=-9999 --outfile={1}'.format(out,out2) os.system(cmd2) – KMatGIS Nov 29 '18 at 14:30
  • when you do python -vv script_name.py to verify which python you are actually using. you will see references in the output. It feels like you environments are mixed up for some reason or another . – nickves Nov 29 '18 at 14:40
  • @Nickves - I've run it with python -vv myscript.py and I'm not entirely sure what to make of the output. I think everything is coming from the correct directory, but I'm not sure. It "tried" a lot of different places when it was searching for numpy. – KMatGIS Nov 29 '18 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.