Python/django : Cannot import GeoIP

那年仲夏 提交于 2019-12-08 06:53:00

问题


I cannot import GeoIP in django. I searched and tested this error two days, but still could not know problem.

Surely, I installed GeoDjango. I'm on MacOS 10.8

following is log by tested by django shell

1>>from django.contrib.gis import geoip

2>>module 'django.contrib.gis.geoip' from '/Library/Python/2.7/site-packages/django/contrib/gis/geoip/init.pyc'>

it works. even I could find geoip class at Library/Python/2.7/site-packages/django/contrib/gis/geoip/base.py


3>>> from django.contrib.gis.geoip import geoip Traceback (most recent call last): File "", line 1, in ImportError: cannot import name geoip


I also add django.contrib.gis to setting.py. Even I could find geoip class at eclipse shortcut.

anyway, I tested one more thing in django shell.

from django.contrib.gis.geoip.base import GeoIP

Traceback (most recent call last): File "", line 1, in File "/Library/Python/2.7/site-packages/django/contrib/gis/geoip/base.py", line 6, in from django.contrib.gis.geoip.libgeoip import GEOIP_SETTINGS File "/Library/Python/2.7/site-packages/django/contrib/gis/geoip/libgeoip.py", line 22, in if lib_path is None: raise GeoIPException('Could not find the GeoIP library (tried "%s"). ' NameError: name 'GeoIPException' is not defined

What I missing?

Is there any way to test my error?


回答1:


It appears you need to install a C library in order to use GeoIP.

Here is a snippet from the file that is throwing that error.

# The shared library for the GeoIP C API.  May be downloaded
#  from http://www.maxmind.com/download/geoip/api/c/
if lib_path:
    lib_name = None
else:
    # TODO: Is this really the library name for Windows?
    lib_name = 'GeoIP'

Once you have it installed somewhere you need to reference it in your settings.py

GEOIP_LIBRARY_PATH = '/whatever'

The library was trying to tell you this, but it seems that there is a bug that prevents it from raising the correct error. https://github.com/django/django/pull/103




回答2:


Previously i was facing this issue : from django.contrib.gis.geoip import GeoIP ImportError: cannot import name GeoIP

which is solved when i install this package.

yum install GeoIP-devel -y


来源:https://stackoverflow.com/questions/12761932/python-django-cannot-import-geoip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!