无法在Djang中导入GeoIP模块

2024-04-26 17:27:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我用的是Django 1.5.5。

设置.py:

GEOIP_PATH = os.path.join(PROJECT_DIR,  'geoIP')
INSTALLED_APPS = (..,'django.contrib.gis',..)

视图.py:

from django.contrib.gis import geoip
print geoip.HAS_GEOIP

打印出false

如果我尝试下列操作之一,就会得到一个ImportError: cannot import name GeoIP

from django.contrib.gis.utils import GeoIP #this one is deprecated whatsoever
from django.contrib.gis.utils.geoip import GeoIP #this one is deprecated whatsoever
from django.contrib.gis.geoip import GeoIP

通常看起来geoip不包含GeoIP模块。

如果我在终端中打开python

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

更多信息,如果我打印:

from django.contrib.gis import geoip
print geoip

我得到:

<module 'django.contrib.gis.geoip' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/gis/geoip/__init__.pyc'>

不知道这能不能暗示有人来帮我?


Tags: djangonamefrompyimportutilsthiscontrib
1条回答
网友
1楼 · 发布于 2024-04-26 17:27:35

看起来你没有在系统范围内安装GeoIP。django.contrib.gis.geoip只是GeoIP库的包装,无论如何都必须安装它。

在OS X上,如果使用自制程序,只需运行brew install geoip。如果没有,则需要确保GeoIP库已安装,并且在系统保存库的任何位置都有libGeoIP.dylib

相关问题 更多 >