简体   繁体   English

找不到Django GeoIP模块

[英]Django GeoIP Module Not Found

I've spent several hours attempting to resolve my problems setting up GeoIP in Django to no avail and was hoping to get some guidance on what the problem(s) might be. 我花了几个小时试图解决我在Django中设置GeoIP的问题无济于事,并希望得到关于问题可能是什么的一些指导。

I'm working on an existing Django application that required some geolocation abilities, specifically getting a users IP and lat/long and then placing that info on a map marker. 我正在研究一个现有的Django应用程序,它需要一些地理定位能力,特别是获取用户IP和lat / long,然后将这些信息放在地图标记上。 GeoIP and the associated libraries appeared to be the best solution for the first step. GeoIP和相关的库似乎是第一步的最佳解决方案。

I installed GeoIP on a Mac using Homebrew. 我使用Homebrew在Mac上安装了GeoIP。 I then manually created a folder in the root directory of my project with the GeoIPv6.data and GeoLiteCity.dat files. 然后,我使用GeoIPv6.data和GeoLiteCity.dat文件在项目的根目录中手动创建了一个文件夹。 After this, I added the path in my settings file: 在此之后,我在我的设置文件中添加了路径:

import os

DEBUG = True
TEMPLATE_DEBUG = DEBUG

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

GEOIP_PATH = os.path.join(BASE_DIR, 'geoip'),

I then opened a command shell for the project and received the following error: 然后我打开了项目的命令shell并收到以下错误:

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

I can't seem to remedy this problem. 我似乎无法解决这个问题。 One issue that may be the cause is extracting the two dat.gz files was an issue. 可能原因之一是提取两个dat.gz文件是一个问题。 Neither could be unzipped from the command line – neither are .zip files – and had to use Stuffit Expander to open these. 两者都不能从命令行解压缩 - 既不是.zip文件 - 也不得不使用Stuffit Expander打开它们。 The resulting dat files in my project IDE (pyCharm) have a VLC (?) icon on each. 我的项目IDE(pyCharm)中生成的dat文件在每个文件上都有一个VLC(?)图标。 Perhaps this is part of the issue (finding a way to uncompress the file was a challenge in itself). 也许这是问题的一部分(找到解压缩文件的方法本身就是一个挑战)。 I'm not sure as the module was not even found. 我不确定该模块是否找不到。

Any help would be extremely appreciated in resolving this issue as I can't progress any further without figuring out what's wrong. 任何帮助都会在解决这个问题时非常感激,因为如果不弄清楚什么是错的,我就无法继续前进。

Many thanks. 非常感谢。

*Edit. *编辑。 Okay, it first appeared an imroper import statement might have been the problem. 好吧,它首先出现了一个非常重要的导入声明可能是问题所在。 Fixed it: from django.contrib.gis.utils.geoip import GeoIP. 修复它:来自django.contrib.gis.utils.geoip导入GeoIP。 Unfortunately, once fixed, the following error results: 不幸的是,一旦修复,会出现以下错误:

>>> from django.contrib.gis.utils.geoip import GeoIP
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/gis/utils/geoip.py", line 68, in <module>
    lgeoip = CDLL(lib_path)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: dlopen(/usr/local/lib/libGeoIP.dylib, 6): no suitable image found.  Did find:
        /usr/local/lib/libGeoIP.dylib: mach-o, but wrong architecture

This is perplexing. 这很令人困惑。 Not sure what the problem is exactly. 不确定问题究竟是什么。

The arch mismatch message means that the libgeoIP library you installed was built with a different CPU architecture than the architecture that the Python interpreter is running in. From the paths involved, you appear to be using a newer (non-Apple-supplied) Python 2.7. arch不匹配消息意味着您安装的libgeoIP库使用与运行Python解释器的体系结构不同的CPU体系结构构建。从所涉及的路径,您似乎使用较新的(非Apple提供的)Python 2.7 。 Chances are that the libgeoIP was built as 64-bit ( -arch x86_64 ) while the Python you installed is a 32-bit -only Python 2.7 ( -arch i386 ). 有可能libgeoIP构建为64位( -arch x86_64 ),而您安装的Python是32位只有Python 2.7( -arch i386 )。 Or possibly the reverse. 或者可能相反。 In either case, you could try reinstalling GeoIP with the universal option so that it contains both archs. 在任何一种情况下,您都可以尝试使用universal选项重新安装GeoIP,以便它包含两个拱门。 Or you could try reinstalling Python 2.7 using a 64-bit/32-bit version such as downloadable from python.org. 或者您可以尝试使用64位/ 32位版本重新安装Python 2.7,例如可从python.org下载。 You can see for sure which archs are involved by using the file command: 您可以使用file命令确定涉及哪些arch:

$ file /usr/local/lib/libGeoIP.dylib
$ file $(python2.7 -c 'import sys;print(sys.executable)')

There needs to be at least one common architecture between the two. 两者之间至少需要有一个共同的架构。

在Django 1.4及更高版本上导入GeoIP的正确方法(包括当前的1.9版本)是:

from django.contrib.gis.geoip import GeoIP

Its not an issue with your dat files. 它不是您的dat文件的问题。 Its a problem with the import statement, and it finding the GeoIP module. 它是import语句的一个问题,它找到了GeoIP模块。

From the docs: 来自文档:
https://docs.djangoproject.com/en/1.4/ref/contrib/gis/geoip/ https://docs.djangoproject.com/en/1.4/ref/contrib/gis/geoip/

In Django 1.4, the GeoIP object was moved out of django.contrib.gis.utils and into its own module, django.contrib.gis.geoip. 在Django 1.4中,GeoIP对象被移出django.contrib.gis.utils并进入它自己的模块django.contrib.gis.geoip。 A shortcut is still provided in utils, but will be removed in Django 1.6. utils中仍提供了一个快捷方式,但在Django 1.6中将被删除。

If you are using django 1.3.x, try: 如果您使用的是django 1.3.x,请尝试:

from django.contrib.gis.utils import GeoIP

Note the difference in the import paths between these two versions: 请注意这两个版本之间导入路径的差异:
https://docs.djangoproject.com/en/1.3/ref/contrib/gis/geoip/ https://docs.djangoproject.com/en/1.3/ref/contrib/gis/geoip/
https://docs.djangoproject.com/en/1.4/ref/contrib/gis/geoip/ https://docs.djangoproject.com/en/1.4/ref/contrib/gis/geoip/

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

方案:

yum install GeoIP-devel -y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM