4

I have followed the guide here and many others but no luck so far. I am wondering if the two Operating systems are even supported by geodjango due to the dependencies.

I have set the environment variables GDAL_LIBRARY_PATH even renamed gdallib.py's gdal path strings to match the one actually installed.

But none of them helped.

Below is the error message I am getting.

   Unhandled exception in thread started by <function wrapper at 0x02EFF8B0>\par
Traceback (most recent call last):\par
  File "C:\\Python27\\lib\\site-packages\\django\\utils\\autoreload.py", line 227, in\par
wrapper\par
    fn(*args, **kwargs)\par
  File "C:\\Python27\\lib\\site-packages\\django\\core\\management\\commands\\runserver.\par
py", line 117, in inner_run\par
    autoreload.raise_last_exception()\par
  File "C:\\Python27\\lib\\site-packages\\django\\utils\\autoreload.py", line 250, in\par
raise_last_exception\par
    six.reraise(*_exception)\par
  File "C:\\Python27\\lib\\site-packages\\django\\utils\\autoreload.py", line 227, in\par
wrapper\par
    fn(*args, **kwargs)\par
  File "C:\\Python27\\lib\\site-packages\\django\\__init__.py", line 27, in setup\par
    apps.populate(settings.INSTALLED_APPS)\par
  File "C:\\Python27\\lib\\site-packages\\django\\apps\\registry.py", line 108, in pop\par
ulate\par
    app_config.import_models()\par
  File "C:\\Python27\\lib\\site-packages\\django\\apps\\config.py", line 202, in impor\par
t_models\par
    self.models_module = import_module(models_module_name)\par
  File "C:\\Python27\\lib\\importlib\\__init__.py", line 37, in import_module\par
    __import__(name)\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\auth\\models.py", line 4, in\par
 <module>\par
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\auth\\base_user.py", line 52\par
, in <module>\par
    class AbstractBaseUser(models.Model):\par
  File "C:\\Python27\\lib\\site-packages\\django\\db\\models\\base.py", line 124, in __\par
new__\par
    new_class.add_to_class('_meta', Options(meta, app_label))\par
  File "C:\\Python27\\lib\\site-packages\\django\\db\\models\\base.py", line 330, in ad\par
d_to_class\par
    value.contribute_to_class(cls, name)\par
  File "C:\\Python27\\lib\\site-packages\\django\\db\\models\\options.py", line 214, in\par
 contribute_to_class\par
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length(\par
))\par
  File "C:\\Python27\\lib\\site-packages\\django\\db\\__init__.py", line 33, in __geta\par
ttr__\par
    return getattr(connections[DEFAULT_DB_ALIAS], item)\par
  File "C:\\Python27\\lib\\site-packages\\django\\db\\utils.py", line 211, in __getite\par
m__\par
    backend = load_backend(db['ENGINE'])\par
  File "C:\\Python27\\lib\\site-packages\\django\\db\\utils.py", line 115, in load_bac\par
kend\par
    return import_module('%s.base' % backend_name)\par
  File "C:\\Python27\\lib\\importlib\\__init__.py", line 37, in import_module\par
    __import__(name)\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\db\\backends\\postgis\\bas\par
e.py", line 5, in <module>\par
    from .features import DatabaseFeatures\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\db\\backends\\postgis\\fea\par
tures.py", line 1, in <module>\par
    from django.contrib.gis.db.backends.base.features import BaseSpatialFeatures\par
\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\db\\backends\\base\\featur\par
es.py", line 4, in <module>\par
    from django.contrib.gis.db.models import aggregates\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\db\\models\\__init__.py",\par
 line 3, in <module>\par
    from django.contrib.gis.db.models.aggregates import *  # NOQA\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\db\\models\\aggregates.py\par
", line 1, in <module>\par
    from django.contrib.gis.db.models.fields import ExtentField\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\db\\models\\fields.py", l\par
ine 3, in <module>\par
    from django.contrib.gis import forms, gdal\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\forms\\__init__.py", lin\par
e 3, in <module>\par
    from .fields import (  # NOQA\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\forms\\fields.py", line\par
4, in <module>\par
    from django.contrib.gis.geos import GEOSException, GEOSGeometry\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\geos\\__init__.py", line\par
 5, in <module>\par
    from .collections import (  # NOQA\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\geos\\collections.py", l\par
ine 11, in <module>\par
    from django.contrib.gis.geos.geometry import GEOSGeometry, LinearGeometryMix\par
in\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\geos\\geometry.py", line\par
 11, in <module>\par
    from django.contrib.gis import gdal\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\gdal\\__init__.py", line\par
 28, in <module>\par
    from django.contrib.gis.gdal.datasource import DataSource\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\gdal\\datasource.py", li\par
ne 39, in <module>\par
    from django.contrib.gis.gdal.driver import Driver\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\gdal\\driver.py", line 5\par
, in <module>\par
    from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\gdal\\prototypes\\ds.py",\par
 line 9, in <module>\par
    from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal\par
  File "C:\\Python27\\lib\\site-packages\\django\\contrib\\gis\\gdal\\libgdal.py", line\par
49, in <module>\par
    lgdal = CDLL(lib_path)\par
  File "C:\\Python27\\lib\\ctypes\\__init__.py", line 362, in __init__\par
    self._handle = _dlopen(self._name, mode)\par
WindowsError: [Error 126] The specified module could not be found

When I run pip install gdal, I get the following.

C:\\xampp\\htdocs\\portal_api>pip install gdal\par
Collecting gdal\par
  Using cached GDAL-2.2.1.tar.gz\par
Installing collected packages: gdal\par
  Running setup.py install for gdal ... error\par
    Complete output from command c:\\python27\\python.exe -u -c "import setuptools\par
, tokenize;__file__='c:\\\\users\\\\admini~1\\\\appdata\\\\local\\\\temp\\\\pip-build-kbusj0\par
{{\field{\*\fldinst{HYPERLINK "\\\\\\\\gdal\\\\\\\\setup.py';f=getattr(tokenize"}}{\fldrslt{\\\\gdal\\\\setup.py';f=getattr(tokenize\ul0\cf0}}}}\f0\fs22 , 'open', open)(__file__);code=f.read().repl\par
ace('\\r\\n', '\\n');f.close();exec(compile(code, __file__, 'exec'))" install --rec\par
ord c:\\users\\admini~1\\appdata\\local\\temp\\pip-_xoozu-record\\install-record.txt --\par
single-version-externally-managed --compile:\par
    running install\par
    running build\par
    running build_py\par
    creating build\par
    creating build\\lib.win32-2.7\par
    copying gdal.py -> build\\lib.win32-2.7\par
    copying ogr.py -> build\\lib.win32-2.7\par
    copying osr.py -> build\\lib.win32-2.7\par
    copying gdalconst.py -> build\\lib.win32-2.7\par
    copying gnm.py -> build\\lib.win32-2.7\par
    creating build\\lib.win32-2.7\\osgeo\par
    copying osgeo\\gdal.py -> build\\lib.win32-2.7\\osgeo\par
    copying osgeo\\gdalconst.py -> build\\lib.win32-2.7\\osgeo\par
    copying osgeo\\gdalnumeric.py -> build\\lib.win32-2.7\\osgeo\par
    copying osgeo\\gdal_array.py -> build\\lib.win32-2.7\\osgeo\par
    copying osgeo\\gnm.py -> build\\lib.win32-2.7\\osgeo\par
    copying osgeo\\ogr.py -> build\\lib.win32-2.7\\osgeo\par
    copying osgeo\\osr.py -> build\\lib.win32-2.7\\osgeo\par
    copying osgeo\\__init__.py -> build\\lib.win32-2.7\\osgeo\par
    running build_ext\par
    building 'osgeo._gdal' extension\par
    creating build\\temp.win32-2.7\par
    creating build\\temp.win32-2.7\\Release\par
    creating build\\temp.win32-2.7\\Release\\extensions\par
    C:\\Users\\Administrator\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ fo\par
r Python\\9.0\\VC\\Bin\\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -I../../port -I.\par
./../gcore -I../../alg -I../../ogr/ -I../../ogr/ogrsf_frmts -I../../gnm -I../../\par
apps -Ic:\\python27\\include -Ic:\\python27\\PC -I. /Tpextensions/gdal_wrap.cpp /Fob\par
uild\\temp.win32-2.7\\Release\\extensions/gdal_wrap.obj\par
    gdal_wrap.cpp\par
    C:\\Users\\Administrator\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ fo\par
r Python\\9.0\\VC\\Include\\xlocale(342) : warning C4530: C++ exception handler used\par
, but unwind semantics are not enabled. Specify /EHsc\par
    extensions/gdal_wrap.cpp(3168) : fatal error C1083: Cannot open include file\par
: 'cpl_port.h': No such file or directory\par
    error: command 'C:\\\\Users\\\\Administrator\\\\AppData\\\\Local\\\\Programs\\\\Common\\\\\par
Microsoft\\\\Visual C++ for Python\\\\9.0\\\\VC\\\\Bin\\\\cl.exe' failed with exit status\par
2\par
\par
    ----------------------------------------\par
Command "c:\\python27\\python.exe -u -c "import setuptools, tokenize;__file__='c:\\\par
\\users\\\\admini~1\\\\appdata\\\\local\\\\temp\\\\pip-build-kbusj0\\\\gdal\\\\setup.py';f=geta\par
ttr(tokenize, 'open', open)(__file__);code=f.read().replace('\\r\\n', '\\n');f.clos\par
e();exec(compile(code, __file__, 'exec'))" install --record c:\\users\\admini~1\\ap\par
pdata\\local\\temp\\pip-_xoozu-record\\install-record.txt --single-version-externall\par
y-managed --compile" failed with error code 1 in c:\\users\\admini~1\\appdata\\local\par
\\temp\\pip-build-kbusj0\\gdal\\\par
}
1
  • Is it possible to move this question to stackoverflow as I have realized the experts in this area are mostly there.
    – wondim
    Jul 28, 2017 at 5:58

4 Answers 4

7

I do not see a direct question here and I am little late in answering this. Yet, I have had a hard time setting up a similar environment recently and could not find a simple solution that did not involve hacking source files... So, here is what worked for me:

  1. Install Python 3.6 (32 bit)
  2. Create a virtual environment and activate it
  3. pip install django
  4. pip install psycopg2 (assuming you've setup PostgreSQL)
  5. pip install GDAL-2.2.3-cp36-cp36m-win32.whl You can get that .whl here. Take note of the warning there "Do not use together with OSGeo4W or gdalwin32".
  6. add 2 system environment variables "GDAL_DATA" and "GDAL_LIB".

    • GDAL_DATA = C:\<path_to_your>\venv\Lib\site-packages\osgeo\data\gdal
    • GDAL_LIB = C:\<path_to_your>\venv\Lib\site-packages\osgeo
  7. Then, add each variable to your system PATH like so: %GDAL_DATA% %GDAL_LIB%

  8. Restart Pycharm and Fire up the django server

This basically follows the Django docs but uses the .whl instead of OsGeo4W. Also, I did not mess with the Python path because I have ArcGIS running with Python 2.7

4
  • Thanks! What about other GIS libraries such as Geos? So they should be installed separately also?
    – wondim
    Mar 20, 2018 at 3:07
  • Yes you can pip install geos as well. Although, not sure why you would want to since GeoDjango already implements the GEOS library. Here is some documentation for that.
    – gisdude
    Mar 21, 2018 at 15:31
  • Yes, it should be installed. According to this guide. docs.djangoproject.com/en/1.11/ref/contrib/gis/install/geolibs
    – wondim
    Mar 21, 2018 at 16:39
  • Worked like a charm! thank you for this answer Jul 17, 2020 at 22:58
0

Thank you @gisdude for the instructions those are very helpful. I am using window10...In my case even after adding system environment variables not solve the issue. I think if we work in a virtual environment mode chances are there Django will not able to locate lib/dll, So what I did

  • GEOS_LIBRARY_PATH = 'your ver_env_path/Lib/site-packages/osgeo/geos_c.dll'
    • GDAL_LIBRARY_PATH = 'your ver_env_path/Lib/site-packages/osgeo/gdal202.dll'

and that solves my issue but may not be an ideal one.

0

You might also encounter this error because the version of gdal has been bumped and you need to use an older version of the script. To do that, you can re-run the OSGeo4W installer, select custom, and select gdal202 (which appears to be the last supported version)

0

First, download the GDAL wheel from Christoph Gohlke's Unofficial Windows Binaries for Python Extension Packages.

pip install "/path/to/GDAL‑3.0.4‑cp37‑cp37m‑win_amd64.whl"

Modify the libgdal.py file in the virtual envrironment site packages by adding 'gdal300' to line 23 of the Django GDAL package python file (/path/to/virtual_env/Lib/site-packages/django/contrib/gis/gdal/libgdal.py):

elif os.name == 'nt':
    # Windows NT shared libraries
    lib_names = ['gdal300', 'gdal204', 'gdal203', 'gdal202', 'gdal201', 'gdal20']

Finally, in your settings.py file in your Django project add

if os.name == 'nt':
    VENV_BASE = os.environ['VIRTUAL_ENV']
    os.environ['PATH'] = os.path.join(VENV_BASE, 'Lib\\site-packages\\osgeo') + ';' + os.environ['PATH']
    os.environ['PROJ_LIB'] = os.path.join(VENV_BASE, 'Lib\\site-packages\\osgeo\\data\\proj') + ';' + os.environ['PATH']

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.