GDAL Error – Fixing No Module Named ‘ogr’ in GDAL 3.3.2 Installing

gdalgdal2tilespython

I download GDAL 3.3.2(with compiler MSVC 2019) from here. I have downloaded the following components and install them in windows 10 x64:

  • gdal-303-1928-x64-core.msi
  • GDAL-3.3.2.win-amd64-py3.9.msi
  • gdal-303-1928-x64-mrsid.msi
  • gdal-303-1928-x64-ecw-55.msi
  • and python-3.9.7-amd64

The environment variables are as the follow:

  • GDAL_DATA = C:\Program Files\GDAL\gdal-data
  • GDAL_DRIVER_PATH = C:\Program Files\GDAL\gdalplugins
  • USE_PATH_FOR_GDAL_PYTHON = YES
  • path = ...;C:\Program Files\GDAL;

When I run gdal2tiles.py -z 12-13 sample.tif /tiles the output is as follow:

ERROR 1: Can't load requested DLL: C:\Program Files\GDAL\gdalplugins\gdal_GEOR.dll
126: The specified module could not be found.

ERROR 1: Can't load requested DLL: C:\Program Files\GDAL\gdalplugins\gdal_GEOR.dll
126: The specified module could not be found.

ERROR 1: Can't load requested DLL: C:\Program Files\GDAL\gdalplugins\gdal_MrSID.dll
126: The specified module could not be found.

ERROR 1: Can't load requested DLL: C:\Program Files\GDAL\gdalplugins\gdal_MrSID.dll
126: The specified module could not be found.

ERROR 1: Can't load requested DLL: C:\Program Files\GDAL\gdalplugins\ogr_MSSQLSpatial.dll
126: The specified module could not be found.

ERROR 1: Can't load requested DLL: C:\Program Files\GDAL\gdalplugins\ogr_MSSQLSpatial.dll
126: The specified module could not be found.

ERROR 1: Can't load requested DLL: C:\Program Files\GDAL\gdalplugins\ogr_OCI.dll
126: The specified module could not be found.

ERROR 1: Can't load requested DLL: C:\Program Files\GDAL\gdalplugins\ogr_OCI.dll
126: The specified module could not be found.

Traceback (most recent call last):
  File "C:\Program Files\GDAL\gdal2tiles.py", line 5, in <module>
    from osgeo_utils.gdal2tiles import *  # noqa
  File "C:\Users\Internet\AppData\Local\Programs\Python\Python39\lib\site-packages\osgeo_utils\gdal2tiles.py", line 58, in <module>
    from osgeo import gdal
  File "C:\Users\Internet\AppData\Local\Programs\Python\Python39\lib\site-packages\osgeo\gdal.py", line 1920, in <module>
    import ogr
ModuleNotFoundError: No module named 'ogr'

In C:\Program Files\GDAL\gdalplugins\ exist all gdal_GEOR.dll, gdal_MrSID.dll, ogr_MSSQLSpatial.dll, ogr_OCI.dll files, and In C:\Program Files\GDAL exist the gdal2tiles.py file. What's the problem?

Best Answer

EDIT

There is actually an open ticket from August, 2021 about this problem in the issue tracker https://github.com/gisinternals/buildsystem/issues/180

The Python modules that gisinternals.com deliver are not up to date. The error messages in your question are showing where the error happens. For example this one that refers to line 1920 in file gdal.py

 import ogr
ModuleNotFoundError: No module named 'ogr'

The GDAL Python API documentation in https://gdal.org/api/python.html gives information about imports.

There are five major modules that are included with the GDAL Python bindings.:

>>> from osgeo import gdal
>>> from osgeo import ogr
>>> from osgeo import osr
>>> from osgeo import gdal_array
>>> from osgeo import gdalconst

Additionally, there are five compatibility modules that are included
but provide notices to state that they are deprecated and will be going
away. If you are using GDAL 1.7 bindings, you should update your imports
to utilize the usage above, **but the following will work until GDAL 3.1.**

>>> import gdal
>>> import ogr
>>> import osr
>>> import gdalnumeric
>>> import gdalconst

The Python bindings of gisinternals.com are not up to date and they cannot work with GDAL version 3.2 and higher without a fix. It may be possible to get a bit further by editing the lines that give errors

import ogr -> import osgeo.ogr

There is one place to edit in ogr.py and two in gdal.py. After those edits the gdal2tiles.py script at least starts but I did not make any tests with data.

However, there are warnigns in the headers of the .py files

# This file was automatically generated by SWIG (http://www.swig.org).
# Version 4.0.2
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.

You could also have a try with GDAL 3.1 or older from the archive https://gisinternals.com/archive.php. They may not suffer from this import issue. On the other hand, by the comment in the issue tracker the Python bindings may have problems with all Python 3 builds.

Related Question