GeoDjango Installation on Windows - Geospatial libraries

646 views
Skip to first unread message

Martin Schultz

unread,
Jan 3, 2018, 12:08:40 PM1/3/18
to geodjango
Hi,

   apparently, this issue came up before and has been solved before, but there appears to be conflicting information on different "help" pages. As a consequence I couldn't figure out how to get Django (django 1.11.7, python 3.4.5) to work on Windows 10 (64 bit) with the required geospatial libraries, in particular with GDAL.

1) in my first attempt, I tried to use the latest Python (3.6.3 from conda) with the latest django. This failed and after a lot of browsing and reading I discovered somewhere that the latest supported version is said to be python 3.4, so I re-installed my complete environment with the versions above.

2) the documentation at https://docs.djangoproject.com/en/1.11/ref/contrib/gis/install/#windows recommends to install OSGEO4W in order to avoid dealing with manual building of the various geospatial libraries. So I did. I downloaded the installer from http://download.osgeo.org/osgeo4w/osgeo4w-setup-x86_64.exe (last change August 2017) and selected the packages that are needed, including gdal.

3) the error I got from django is rather unspecific: OS error - cannot locate file. After inserting a print() statement in ctypes/__init__.py I could see that django waqs looking for gdal201.dll in <my_virtual_env>/Library/bin . After some digging, I saw that this path is defined in django/contrib/gis/gdal/libgdal.py (line 16ff: lib_path = os.path.join(sys.prefix, 'Library', 'bin', 'gdal201.dll') ). 

4) The documentation at https://docs.djangoproject.com/en/2.0/ref/contrib/gis/gdal/ refers to an environment variable GDAL_LIBRARY_PATH - but apparently, this variable is not implemented in the django version I use: Neither adding it to settings.py, nor adding it to the Windows environment variables had any effect. Thus, I manually loaded the environment variable in libgdal.py as follows:
lib_path = os.environ.get('GDAL_LIBRARY_PATH')
if lib_path is None:
   
if 'linux' in sys.platform:
        lib_path
= os.path.join(sys.prefix, 'lib', 'libgdal.so')
   
elif 'darwin' in sys.platform:
        lib_path
= os.path.join(sys.prefix, 'lib', 'libgdal.dylib')
   
elif os.name == 'nt':
        lib_path
= os.path.join(sys.prefix, 'Library', 'bin', 'gdal201.dll')

      This served the purpose (apparently, ctypes now tried to open gdal202.dll), but it failed, because the required function or procedure wasn't found: 
self._handle = _dlopen(self._name, mode)  (line 352, in ctypes/__init__.py); does this mean that there is an incompatible change between gdal201.dll and gdal202.dll? Or does it mean that the gdal202.dll that is included in OSGEO4W is not compatible with the python version (specifically that's ctypes, I guess) I am using?

5) elsewhere (for example https://sandbox.idre.ucla.edu/sandbox/tutorials/installing-gdal-for-windowsalso referred to in another post on this mailing list), I read that gdal should be installed from http://www.gisinternals.com/release.php . However, that information appears outdated as it recommends to use python2.7 and gdal version.2.2 -- as I wrote above, this will not be recognized by libgdal.py, or will it? Some people say that it is important to use a gdal version that is compiled with the same compiler as python itself so that the bindings (ctypes?) work. If this is the case, then why does the GeoDjango documentation recommend to downloas OSGEO4W (unless it implicitly suggests to also use the python interpreter which comes with OSGEO4W - but this is not mentioned anywhere, nor will it easily allow the set-up of a virtual environment for the Django application development). Also, what does this mean for the other requried geospatial libraries? Will I run into the same problems with GEOS and PROJ4?

Any help - ideally to get this to work with Python 3+ - will be much appreciated. Also, if this reaches the right people, it would be great if a) the GeoDjango documentation could provide more explicit hints how to solve such geospatial library problems, and specifically a table that summarizes versions which work together, and b) if the respective django libraries (such as libgdal.py) could become more verbose with respect to the possible "binding" errors so that it becomes easier to search for the right solution.

Many thanks,

Martin


PS1: environment listing
# packages in environment at C:\Users\m.schultz\Python\Miniconda3\envs\locations:
#
certifi                   2017.1.23                py34_0    conda-forge
django                    1.11.7                     py_0    conda-forge
djangorestframework       3.7.7                      py_0    conda-forge
djangorestframework-gis   0.12                       py_0    conda-forge
mkl                       2017.0.3                      0
numpy                     1.13.0                   py34_0
pip                       9.0.1                    py34_0    conda-forge
python                    3.4.5                         2    conda-forge
pytz                      2017.3                     py_2    conda-forge
requests                  2.12.4                   py34_0    conda-forge
setuptools                32.3.1                   py34_0    conda-forge
vs2010_runtime            10.00.40219.1                 2
wheel                     0.30.0                     py_1    conda-forge
wincertstore              0.2                      py34_0    conda-forge

PS2: stack trace
C:\Users\m.schultz\Python\Miniconda3\envs\locations\python.exe C:/Users/m.schultz/Python/Projects/locations/manage.py runserver
*** ctypes.__init__ self._name = : kernel32
*** ctypes.__init__ self._name = : None
*** ctypes.__init__ self._name = : msvcr100.dll
*** ctypes.__init__ self._name = : rpcrt4
***config.py: mod_path =  django.contrib.admin.apps
***config.py: mod_path =  django.contrib.auth.apps
***config.py: mod_path =  django.contrib.contenttypes.apps
***config.py: mod_path =  django.contrib.sessions.apps
***config.py: mod_path =  django.contrib.messages.apps
***config.py: mod_path =  django.contrib.staticfiles.apps
***config.py: mod_path =  django.contrib.gis.apps
*** libgdal.py: os.environ.get('GDAL_LIBRARY_PATH') =  C:\OSGeo4W64\bin\gdal202.dll
*** ctypes.__init__ self._name = : C:\OSGeo4W64\bin\gdal202.dll
*** ctypes.__init__ self._name = : kernel32
*** ctypes.__init__ self._name = : None
*** ctypes.__init__ self._name = : msvcr100.dll
*** ctypes.__init__ self._name = : rpcrt4
***config.py: mod_path =  django.contrib.admin.apps
***config.py: mod_path =  django.contrib.auth.apps
***config.py: mod_path =  django.contrib.contenttypes.apps
***config.py: mod_path =  django.contrib.sessions.apps
***config.py: mod_path =  django.contrib.messages.apps
***config.py: mod_path =  django.contrib.staticfiles.apps
***config.py: mod_path =  django.contrib.gis.apps
*** libgdal.py: os.environ.get('GDAL_LIBRARY_PATH') =  C:\OSGeo4W64\bin\gdal202.dll
*** ctypes.__init__ self._name = : C:\OSGeo4W64\bin\gdal202.dll
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000000006B4A598>
Traceback (most recent call last):
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\utils\autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\utils\autoreload.py", line 251, in raise_last_exception
    six.reraise(*_exception)
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\utils\autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\apps\registry.py", line 116, in populate
    app_config.ready()
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\contrib\admin\apps.py", line 23, in ready
    self.module.autodiscover()
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\contrib\admin\__init__.py", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\utils\module_loading.py", line 50, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\contrib\gis\admin\__init__.py", line 5, in <module>
    from django.contrib.gis.admin.options import GeoModelAdmin, OSMGeoAdmin
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\contrib\gis\admin\options.py", line 2, in <module>
    from django.contrib.gis.admin.widgets import OpenLayersWidget
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\contrib\gis\admin\widgets.py", line 3, in <module>
    from django.contrib.gis.gdal import GDALException
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\contrib\gis\gdal\__init__.py", line 28, in <module>
    from django.contrib.gis.gdal.datasource import DataSource
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\contrib\gis\gdal\datasource.py", line 39, in <module>
    from django.contrib.gis.gdal.driver import Driver
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\contrib\gis\gdal\driver.py", line 5, in <module>
    from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\contrib\gis\gdal\prototypes\ds.py", line 9, in <module>
    from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\site-packages\django\contrib\gis\gdal\libgdal.py", line 53, in <module>
    lgdal = CDLL(lib_path)
  File "C:\Users\m.schultz\Python\Miniconda3\envs\locations\lib\ctypes\__init__.py", line 352, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 127] Die angegebene Prozedur wurde nicht gefunden



PS3: removing the contrib.gis entry from INSTALLED_APPS in settings.py yields a functional Django server (without any geospatial functionality, of course).

Oliver Larcher

unread,
Nov 28, 2018, 4:36:53 AM11/28/18
to geodjango
Hi, I have exactly the same problem. Did you ever manage to solve it?

Best regards,
Oliver
Reply all
Reply to author
Forward
0 new messages