Search code examples
djangogeodjango

Django: ImportError: cannot import name 'GeoIP2'


I am trying to set up geoip2 for GeoDjango as per the instructions.

For some reason the wrapper isn't importing the function. It worked before I downloaded the databases and pointed to them in my settings, but for some reason now I can't load GeoIP2 (even when I comment out the line in settings.py). How should I troubleshoot this?

Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import geoip2
>>>
>>> from django.contrib.gis.geoip2 import GeoIP2
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: cannot import name 'GeoIP2'
>>>

EDIT: I can see the GeoIP2 function correctly listed in the source file (which I haven't modified). What could possibly be preventing it from loading?

I am running Django 1.11.4

If I import django.contrib.gis.geoip2 this is it's __path__ property:

>>> geoip2.__path__
['C:\\Users\\Adam\\Envs\\otherlane\\lib\\site-packages\\django\\contrib\\gis\\geoip2']

Solution

  • This module is Deprecated since version 1.9 in favor of django.contrib.gis.geoip2, which supports IPv6 and the GeoLite2 database format.

    If you have a django < 1.9, use instead

    from django.contrib.gis.geoip import GeoIP