Django GeoIP import
Question: > **Possible Duplicate:**  
>  [Error setting up geoip on
> Django](http://stackoverflow.com/questions/4896996/error-setting-up-geoip-
> on-django)

I get the "cannot import name GeoIP" error from the browser but not on python
terminal. for example for geodata in /tmp/geo. the following works in the
python terminal.

    
    
        from django.contrib.gis.geoip import GeoIP
        GeoIP(path='/tmp/geo/')
    

However the following in a django view gives the error

    
    
        from django.contrib.gis.geoip import GeoIP
        return HttpResponse (GeoIP(path='/tmp/geo/'))
    

Any pointer will be helpfull. I'm using django 1.4 , python 2.6. here is the
trace. Thanks.

    
    
        Traceback:
        File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
        101.                             request.path_info)
        File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
        300.                     sub_match = pattern.resolve(new_path)
        File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
        209.             return ResolverMatch(self.callback, args, kwargs, self.name)
        File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py" in callback
        216.         self._callback = get_callable(self._callback_str)
        File "/usr/lib/python2.6/site-packages/django/utils/functional.py" in wrapper
        27.         result = func(*args)
        File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py" in get_callable
        92.                 lookup_view = getattr(import_module(mod_name), func_name)
        File "/usr/lib/python2.6/site-packages/django/utils/importlib.py" in import_module
        35.     __import__(name)
        File "/x/y/z/views.py" in <module>
        12. from django.contrib.gis.utils import GeoIP
    
        Exception Type: ImportError at /
        Exception Value: cannot import name GeoIP
    


Answer: The two statements seem to differ (look at the stacktrace):

    
    
    from django.contrib.gis.utils import GeoIP
    

vs

    
    
    from django.contrib.gis.geoip import GeoIP
    

Looking at [the
source](https://github.com/django/django/blob/master/django/contrib/gis/geoip/__init__.py),
`GeoIP` is defined in
[`django.contrib.gis.geoip.base`](https://github.com/django/django/blob/master/django/contrib/gis/geoip/base.py)
and imported in
[`django.contrib.gis.geoip`](https://github.com/django/django/blob/master/django/contrib/gis/geoip/__init__.py),
which explain why it works in the console, and not in the view, where you're
using `django.contrib.gis.utils.GeoIP`.

You should therefore use `from django.contrib.gis.geoip import GeoIP`
everywhere.

* * *

Your problem probably arises from the fact that the `django.contrib.gis.utils`
module was [removed in Django
1.4](https://docs.djangoproject.com/en/dev/ref/contrib/gis/geoip/)

