简体   繁体   中英

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.

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. GeoIP and the associated libraries appeared to be the best solution for the first step.

I installed GeoIP on a Mac using Homebrew. I then manually created a folder in the root directory of my project with the GeoIPv6.data and GeoLiteCity.dat files. 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:

>>> 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. Neither could be unzipped from the command line – neither are .zip files – and had to use Stuffit Expander to open these. The resulting dat files in my project IDE (pyCharm) have a VLC (?) icon on each. 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. 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. 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 ). Or possibly the reverse. In either case, you could try reinstalling GeoIP with the universal option so that it contains both archs. Or you could try reinstalling Python 2.7 using a 64-bit/32-bit version such as downloadable from python.org. You can see for sure which archs are involved by using the file command:

$ 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. Its a problem with the import statement, and it finding the GeoIP module.

From the docs:
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. A shortcut is still provided in utils, but will be removed in Django 1.6.

If you are using django 1.3.x, try:

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.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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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