10

I am trying to run some Python code using the Python window in ArcGIS 10.1 that uses both the arcpy and gdal modules. However, when I try and import the gdal module I get an error:

ImportError: No module named osgeo

Obviously it can't find the module, so I have added my main python site-packages directory to the sys.path list:

sys.path.append(r"C:\Python27\lib\site-packages")

However, when I try and run import osgeo or from osgeo import gdal I get the following error:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python27\lib\site-packages\osgeo\__init__.py", line 21, in <module>
    _gdal = swig_import_helper()
  File "C:\Python27\lib\site-packages\osgeo\__init__.py", line 17, in swig_import_helper
    _mod = imp.load_module('_gdal', fp, pathname, description)
ImportError: DLL load failed: %1 is not a valid Win32 application.

I've found various resources on the internet that seem to talk about similar problems (for example this question and this forum post, but they seem to be out-of-date (ie. not using ArcGIS 10.1) or not asking quite the same question.

Of course, importing osgeo from a normal non-Arc Python window works fine, and I have compared sys.path and os.environ['PATH'] between the scripts and updated them so that they are the same, and it doesn't seem to fix the problem.

Does anyone have any idea how I can get this to work?

robintw
  • 4,006
  • 11
  • 40
  • 60
  • I have heard stories of people getting GDAL to work in the ArcGIS python interface before. A few questions: Which method did you use to install GDAL and are you sure you have the correct windows bindings installed and correctly setup? And have you installed the python bindings to the ArcGIS python install? – James Milner Aug 14 '12 at 12:03
  • It looks like you have another version (2.7) of Python installed in addition to the version (2.6) installed by ArcGIS 10. Did you install GDAL for 2.6 as well or are you trying to point it to the version installed for 2.7? Is your 2.7 install a 64-bit installation? – Jason Bellino Aug 14 '12 at 13:37
  • @Jason: I'm using ArcGIS 10.1, which according to http://gis.stackexchange.com/questions/22271/arcgis-and-python-issues-with-different-versions uses Python 2.7. Do you know if that is correct? – robintw Aug 14 '12 at 17:22
  • @ZeroQualms: Glad to hear it can be done! I installed GDAL through the standard executable installer downloaded from the website, and it works fine in a standard python script run outside of ArcGIS. What exactly do you mean by "And have you installed the python bindings to the ArcGIS python install?"? – robintw Aug 14 '12 at 17:23
  • 1
    @Robintw there are numerous ways to install GDAL for python. Perhaps you could link me to the executable you used and how you set it up? – James Milner Aug 14 '12 at 19:33
  • In the mean time see if you can find an appropriate executable here: http://www.gisinternals.com/sdk/ and download and see if you can find the ArcGIS 10 python install from the list when you're installing – James Milner Aug 14 '12 at 19:40
  • Oh you're right, I thought I had read that 10.1 was still using Python 2.6. – Jason Bellino Aug 14 '12 at 19:54
  • @ZeroQualms: I've downloaded those setup files and tried installing them, and it can only find the c:\Python27 install, and says that GDAL is already installed under the site-packages in there. Looking on my system there seems to be another python.exe under c:\Python27\ArcGIS10.1. Is that where ArcGIS puts its version of Python? I've tried installing GDAL to there but importing osgeo gives the same error as I had originally. Any other ideas? – robintw Aug 15 '12 at 08:27
  • 2
    I've just managed to solve the question - I managed to miss reading the bit above where @Jason asked if I was using a 64-bit installation of Python 2.7. The answer is that my main Python 2.7 install is 64-bit, but Arc's install is 32-bit, so I needed to install a 32-bit version of GDAL, and now it works fine. Thanks everyone - I'll add my final solution as an answer. – robintw Aug 15 '12 at 14:18

1 Answers1

5

@robintw has confirmed it in the comments.


ImportError: DLL load failed: %1 is not a valid Win32 application.

From the error, it would seem that your Python 2.7 installation is 64-bit. You'll have to install the 32-bit version of Python and GDAL.

R.K.
  • 17,405
  • 3
  • 59
  • 110