GeoDjango: cannot import name GEOSException fixed, now [WinError 126]

4.1k views Asked by At

I am using python 3.4, postgres 9.3.5, Windows 7.

I followed this to try to get GeoDjango working: https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/#windows

When I try to add the django.contrib.gis to installed apps (in pycharm), or when I try to run

the below is the traceback.

However, in this directory C:\Python34\Lib\site-packages\django\contrib\gis\geos there is geometry.py (which as the class GEOSGeometry in it) and error.py (which has the class GEOSException in it).

When I run this in the python shell, it does not throw any errors:

>>> from django.contrib.gis.geos.error import GEOSException 

Thanks for any thought on what the issue may be. A bunch of other traceback, then:

  File "C:\Python34\lib\site-packages\django\contrib\gis\forms\fields.py", line
8, in <module>
    from django.contrib.gis.geos import GEOSException, GEOSGeometry
ImportError: cannot import name 'GEOSException'

UPDATE I found some info that suggested that I should update the project setting:

GEOS_LIBRARY_PATH = r'C:\Program Files\OSGeo4W64\bin\geos_c.dll'

That worked, but now I get a WinError 126

  File "C:\Python34\lib\site-packages\django\contrib\gis\forms\fields.py", line
8, in <module>
    from django.contrib.gis.geos import GEOSException, GEOSGeometry
  File "C:\Python34\lib\site-packages\django\contrib\gis\geos\__init__.py", line
 9, in <module>
    from .libgeos import geos_version, geos_version_info  # NOQA: flake8 detects
 only the last __all__
  File "C:\Python34\lib\site-packages\django\contrib\gis\geos\libgeos.py", line
61, in <module>
    lgeos = CDLL(lib_path)
  File "C:\Python34\lib\ctypes\__init__.py", line 351, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

I am not clear on what module it is that cannot be found, nor how to fix it.

1

There are 1 answers

0
ivan7707 On BEST ANSWER

I was able to fix the problem. See what I did here:

Django forum with how I fixed it

Sorry, this error message is obscure. I have filed a ticket to improve it: https://code.djangoproject.com/ticket/23873

The cause of the error message is that you do not have an installation of GEOS that is on your sys.path and importable. (That is, something in https://docs.djangoproject.com/en/1.7/ref/contrib/gis/install/#osgeo4w or the following step didn't work correctly.)

If you look in django/contrib/gis/geos/init.py, you'll see that GEOSException is only imported there if HAS_GEOS is True. That is why importing GEOSException from django.contrib.gis.geos will succeed if you have GEOS installed, but fails otherwise.

As for the win 126 error:

I am not sure exactly what helped finally fix it but:

  1. I changed my python version from 64 bit to 32 bit
  2. The script in that updated the path variables were pretty messed up, so I manually changed them to what they should have been.
  3. I had incorrectly added the GEOS_LIBRARY_PATH of

C:\program files\OSGeo4W\bin\geos_c.dll

but I should have added:

C:\OSGeo4W\bin\geos_c.dll

Now, I can just delete the GEOS_LIBRARY_PATH from the project settings and things still work, so I guess it must have been the 64 bit vs 32 bit issue, or the path issue.