Adam Starrh
Adam Starrh

Reputation: 6958

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']

Upvotes: 3

Views: 6235

Answers (6)

Whizzkid
Whizzkid

Reputation: 19

You can handle it this way

from geoip2 import geolite2

Upvotes: 0

Yash Verma
Yash Verma

Reputation: 21

just run the command pip install geoip2 then it will work fine...

Upvotes: 2

Mir Suhail
Mir Suhail

Reputation: 66

I was also facing the same error and couldn't resolve it after multiple attempts. Since it was working on one of my system, I found one difference, When you install geoip2 via pip it also installs maxminddb. The system on which it was working, maxminddb version was 1.5.4 and the one on which it was not working it was maxminddb==2.0.0 so finally I did pip install maxminddb==1.5.4 and it worked

Upvotes: 1

anjaneyulubatta505
anjaneyulubatta505

Reputation: 11665

I fixed by installing it through pip package geoip2==2.9.0

pip install geoip2==2.9.0

Upvotes: 7

Adam Starrh
Adam Starrh

Reputation: 6958

Double check your GEOIP_PATH. And my I remind you that Windows requires back slashes not forward slashes.

Upvotes: 1

Lemayzeur
Lemayzeur

Reputation: 8525

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

Upvotes: 4

Related Questions