ImportError: cannot import name 'GEOSException'

2,069 views
Skip to first unread message

jogaserbia

unread,
Nov 18, 2014, 9:13:01 AM11/18/14
to django...@googlegroups.com
Hi,  

I am trying to get GeoDjango up and running.  

I followed these instructions (on Windows 7, Postgres 9.3.5).  

I followed these instructions:


And get the error 

ImportError: cannot import name 'GEOSException'

I am not familiary enough with Python, Django or GeoDjango to be able to figure out the issue. 

Any help would be appreciated. 


Ivan 



(ATMapp) C:\Python34\allenvs\ATMappenv\ATMApp>python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
385, in execute_from_command_line
    utility.execute()
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
354, in execute
    django.setup()
  File "C:\Python34\lib\site-packages\django\__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Python34\lib\site-packages\django\apps\registry.py", line 108, in pop
ulate
    app_config.import_models(all_models)
  File "C:\Python34\lib\site-packages\django\apps\config.py", line 202, in impor
t_models
    self.models_module = import_module(models_module_name)
  File "C:\Python34\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:\Python34\allenvs\ATMappenv\ATMApp\ATMlocator\models.py", line 2, in <
module>
    from django.contrib.gis.db import models
  File "C:\Python34\lib\site-packages\django\contrib\gis\db\models\__init__.py",
 line 8, in <module>
    from django.contrib.gis.db.models.manager import GeoManager  # NOQA
  File "C:\Python34\lib\site-packages\django\contrib\gis\db\models\manager.py",
line 2, in <module>
    from django.contrib.gis.db.models.query import GeoQuerySet
  File "C:\Python34\lib\site-packages\django\contrib\gis\db\models\query.py", li
ne 6, in <module>
    from django.contrib.gis.db.models.fields import get_srid_info, PointField, L
ineStringField
  File "C:\Python34\lib\site-packages\django\contrib\gis\db\models\fields.py", l
ine 4, in <module>
    from django.contrib.gis import forms
  File "C:\Python34\lib\site-packages\django\contrib\gis\forms\__init__.py", lin
e 2, in <module>
    from .fields import (GeometryField, GeometryCollectionField, PointField,  #
NOQA
  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'

jogaserbia

unread,
Nov 18, 2014, 9:15:29 AM11/18/14
to django...@googlegroups.com
Forgot to mention Django 1.7.

Thanks,

Ivan

jogaserbia

unread,
Nov 18, 2014, 9:16:48 AM11/18/14
to django...@googlegroups.com
Python 3.4

jogaserbia

unread,
Nov 18, 2014, 9:45:58 AM11/18/14
to django...@googlegroups.com
I got the same error when I run the following command in the Python Shell:

from django.contrib.gis.db import models 

jogaserbia

unread,
Nov 18, 2014, 11:53:19 AM11/18/14
to django...@googlegroups.com
I just realized something. 
 
This are all the files in django.contrib.gis.geos:
 
Notice there is no GEOSException, nor is there GEOSGeometry.   There is however geometry.py (which as the class GEOSGeometry in it) and error.py (which has the class GEOSException in it). 
 
I am not sure what to do with this information as I am new to Python/Django.
 
 Directory of C:\Python34\Lib\site-packages\django\contrib\gis\geos
11/06/2014  02:02 PM    <DIR>          .
11/06/2014  02:02 PM    <DIR>          ..
11/06/2014  02:02 PM             1,660 base.py
11/06/2014  02:02 PM             4,482 collections.py
11/06/2014  02:02 PM             5,495 coordseq.py
11/06/2014  02:02 PM               813 error.py
11/06/2014  02:02 PM             1,033 factory.py
11/06/2014  02:02 PM            25,297 geometry.py
11/06/2014  02:02 PM               810 io.py
11/06/2014  02:02 PM             5,287 libgeos.py
11/06/2014  02:02 PM             1,530 LICENSE
11/06/2014  02:02 PM             5,813 linestring.py
11/06/2014  02:02 PM            10,974 mutable_list.py
11/06/2014  02:02 PM             4,400 point.py
11/06/2014  02:02 PM             6,827 polygon.py
11/06/2014  02:02 PM             2,428 prepared.py
11/06/2014  02:02 PM    <DIR>          prototypes
11/06/2014  02:02 PM    <DIR>          tests
11/06/2014  02:02 PM             1,154 __init__.py
11/06/2014  02:02 PM    <DIR>          __pycache__ 

Carl Meyer

unread,
Nov 19, 2014, 4:40:22 PM11/19/14
to django...@googlegroups.com
Hello,

On 11/18/2014 09:53 AM, jogaserbia wrote:
> I just realized something.
>
>
> This are all the files in django.contrib.gis.geos:
>
> Notice there is no GEOSException, nor is there GEOSGeometry. There is
> however geometry.py (which as the class GEOSGeometry in it) and error.py
> (which has the class GEOSException in it).
>
> I am not sure what to do with this information as I am new to
> Python/Django.

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.

Carl

signature.asc

jogaserbia

unread,
Nov 19, 2014, 4:58:10 PM11/19/14
to django...@googlegroups.com
Hi Carl,

Thanks very much for the response.  

I am trying to get GeoDjango up and running (for a couple of days now) and am having a really hard time.  

I came across another question on the internet (that does not specifically have to do with my issue) that suggested putting this path variable into the project setting (today):

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

Sadly, I do not know why this would work to rectify the initial problem. 

I think it gets past the GEOSException error, but another error is coming up:

OSError: [WinError 126] The specified module could not be found

Since I am new to all of these particular technologies (Django, Python, GeoDjango) I do not know where to begin troubleshooting these.  I am not even clear on how to read the below traceback (i.e. where the block of code are delineated.  I am assuming each time I see File, that is a new code block that is being run).  

Ivan 


  File "C:\Python34\lib\site-packages\django\contrib\gis\forms\__init__.py", lin
e 2, in <module>
    from .fields import (GeometryField, GeometryCollectionField, PointField,  #
NOQA
  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

Carl Meyer

unread,
Nov 19, 2014, 5:10:53 PM11/19/14
to django...@googlegroups.com
Hi Ivan,

On 11/19/2014 02:58 PM, jogaserbia wrote:
> Hi Carl,
>
> Thanks very much for the response.
>
> I am trying to get GeoDjango up and running (for a couple of days now)
> and am having a really hard time.

Sorry about that :/ I know the dependencies for GeoDjango can be hard to
set up in general, and it may well be worse on Windows (at the very
least, I'm sure the process is less well-tested, since I think most
users and developers of GeoDjango use OS X or Linux). Unfortunately
since I'm not an expert on GeoDjango, and I know even less about
Windows, I'm not sure I'll be able to help.

> I came across another question on the internet (that does not
> specifically have to do with my issue) that suggested putting this path
> variable into the project setting (today):
>
> GEOS_LIBRARY_PATH = r'C:\Program Files\OSGeo4W64\bin\geos_c.dll'
>
> Sadly, I do not know why this would work to rectify the initial problem.
>
> I think it gets past the GEOSException error, but another error is
> coming up:
>
> *OSError: [WinError 126] The specified module could not be found*

This error is still related to being unable to load the GEOS libraries -
but that's about all I can tell you.

> Since I am new to all of these particular technologies (Django, Python,
> GeoDjango) I do not know where to begin troubleshooting these. I am not
> even clear on how to read the below traceback (i.e. where the block of
> code are delineated. I am assuming each time I see *File*, that is a
> new code block that is being run).

Yes, each "File ..." line represents a stack frame.

Carl

signature.asc

jogaserbia

unread,
Nov 19, 2014, 5:24:02 PM11/19/14
to django...@googlegroups.com
Thanks for the help Carl.  Also, thanks for filing the ticket. 

Every bit of info helps.  I really do like Python and Django so far.  

I am just wondering whether I should just quit trying to make GeoDjango work on my windows machine (at work).  I will be setting up a linux machine at home in the next couple of months, and can try it out again.  

As a workaround, what I could probably do (if I can figure it out) is call the postgres fuctions for distance between Lon Lat points that will serve as the basis for the application that I am building.  

What I am in effect trying to do in the Django app I am building is:

find the closets 3 locations (from a list of locations in a postgres database) to the address location that will be passed via input box (and might be run through a Google to get the Lon Lat info).  

Thanks again, 

Ivan

Tom Lockhart

unread,
Nov 20, 2014, 9:38:09 AM11/20/14
to django...@googlegroups.com

On Nov 19, 2014, at 2:24 PM, jogaserbia <ivanja...@live.ca> wrote:

> Thanks for the help Carl. Also, thanks for filing the ticket.
>
> Every bit of info helps. I really do like Python and Django so far.
>
> I am just wondering whether I should just quit trying to make GeoDjango work on my windows machine (at work). I will be setting up a linux machine at home in the next couple of months, and can try it out again.

Windows boxes can be cranky and annoying, but I’d stick with it and just Make It Work. Once you have a complete installation (and document how you got there!) you won’t have to think hard about it again. Or use virtualbox (and Docker on top of it) to get started with your Linux installation now.

You *are* using virtualenv, right? That is very high on my list of things that make my life easier for setting up development systems and deploying to production. You may find that it helps with some of the issues you are seeing on your system.

> As a workaround, what I could probably do (if I can figure it out) is call the postgres fuctions for distance between Lon Lat points that will serve as the basis for the application that I am building.

Yeah, I wrote that code back in the day and I would use GeoDjango. Not that it doesn’t do what it says it does, but GeoDjango gives you everything you will need all at once.

hth

- Tom

jogaserbia

unread,
Nov 20, 2014, 10:01:56 AM11/20/14
to django...@googlegroups.com
Hi Thomas,

Thanks for the reply.  Yes, I am using a virtualenv for this project.  I'll try to stick it out with GeoDjango, but I really do not know what to do now to try to continue on with the install.  

At this point in my Django career, I am pretty much a monkey banking on a keyboard when troubleshooting this type of issue.   On that note, can you possible refer me to any program/documentation on how I would troubleshoot such an issue (stepping through code, checking variable values, etc).

If and when I get this up and running, I'll have to put all of the info somewhere, for other peoples' reference.  

Ivan  

Tom Lockhart

unread,
Nov 20, 2014, 10:44:51 AM11/20/14
to django...@googlegroups.com
Thanks for the reply.  Yes, I am using a virtualenv for this project.  I’ll try to stick it out with GeoDjango, but I really do not know what to do now to try to continue on with the install.  

I would probably stop “continuing”, and go back to the start. afaict the installation instructions for Windows in the Django docs (mentioned by Carl) should address the path issues you are seeing. In particular, the lines including “set PATH=%PATH%;…” and the registry updates in the “Modify Windows environment” section of the GeoDjango installation instructions would seem to cover it.

If after starting over and slavishly following the instructions you still see the path issues, post the values of the variables from the installation instructions which are actually set on your system (e.g. PATH). We should be able to help a bit more. Not me since I don’t know much about Windows, but someone will…

hth

- Tom


-- 
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fa610d6b-352e-411b-b831-1540b2c67724%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Please consider the environment before printing this message.
This message may be privileged and/or confidential, and the sender does not waive any related rights and obligations.  Any distribution, use or copying of this message or the information it contains by other than an intended recipient is unauthorized.  If you received this message in error, please immediately advise me by return e-mail or phone.  All information, references, images, programs, source code, or other materials whatsoever contained in, or supplied with, this document are TRADE SECRETS and governed by the Uniform Trade Secrets Act.  User assumes all direct and consequential liabilities and costs that result from any unauthorized disclosure or use of this information.

jogaserbia

unread,
Nov 21, 2014, 3:21:23 PM11/21/14
to django...@googlegroups.com
Hi All,

Thanks very much for the help.  I managed to figure it out with tips for both of you. 

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. 
2) 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.  

Thanks again for the advice. 

Ivan
Reply all
Reply to author
Forward
0 new messages