Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: Python + GDAL - OSGeo4W installation

I have installed the latest compilation of OSGeo4W for Windows and I want to use the various GDAL tools available. When I open the OSGeo4W command shell, enter Python and write

> from osgeo import gdal

I get the following error:

> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "C:\OSGeo4W\apps\Python27\lib\site-packages\osgeo\__init__.py", line 21,
> in <module>
>     _gdal = swig_import_helper()
>   File "C:\OSGeo4W\apps\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: The operating system cannot run %1.

My PATH contains the following OSGeo4W entries:

> C:\OSGeo4W\bin
> C:\OSGeo4W\\apps\msys\bin
> C:\OSGeo4W\\apps\Python27\Scripts

From what I read around the web, this error could be about incompatible versions. I assume that the OSGeo4W package is compiled to work as it is, thus it might be other versions previously installed on my computer or residues that interferes. But I don't know how to determine whats interfering and where that information is stored.

What can I do to solve this problem?

like image 879
Chau Avatar asked Dec 12 '25 15:12

Chau


2 Answers

This problem occurs when there's another version of python installed.

I found that when I added the directory path to C:\OSGeo4W\bin at the beginning of the PATH variable, and ran python through the OSGEO4W shell, that I was able to import the gdal library.

like image 104
Matt Avatar answered Dec 15 '25 04:12

Matt


Shared libraries are not commonly stored in the bin subdirectories of a distribution. Try and dig through the source of OSGeo4W to locate the gdal dll file (gdal.dll??). Probably it is in a lib subdirectory. Add that path also to your PATH environment variable. If you want to check for other GDAL installs, just start a system wide search for the dll you try load and see if there are more than one.


Another option I found online for this error is that you need to use a specific Python version that is shipped with OSGeo4W. Using a normal Python 2.7 install will not work. See:

  • https://gis.stackexchange.com/questions/10173/how-to-import-qgis-core-in-a-osgeo4w-installation
  • http://old.nabble.com/Python-bindings-to-OGR-td29827795.html
like image 28
Paul Hiemstra Avatar answered Dec 15 '25 04:12

Paul Hiemstra