Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to install the MaxMind GeoIP C library, so that I can use GeoIP as part of Django. I’m working on Mac OS X.

I’ve downloaded GeoIP-1.4.8.tar.gz from here and run:

./configure
make
make check
make install

Without any problems. I’ve also downloaded GeoIP.dat.gz and GeoLiteCity.dat.gz, unzipped them and put them in a local directory.

Then I’ve set the following paths in my settings.py:

GEOIP_PATH = 'path_to_dat_files'
GEOIP_LIBRARY_PATH = 'path_to_c_files'

Now I’m trying to run python manage.py migrate (which has a reference to GeoIP in a project I'm using), but I’m still getting this error:

from django.contrib.gis.utils import GeoIP
ImportError: cannot import name GeoIP

Any ideas?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

I just follow the same procedure and found nothing wrong

./configure
make
make check
make install

and put the geo unzip-ed database in django project's root dir, add the following to settings.py:

GEOIP_PATH = "./"

here is what I run and got:

% python manage.py shell
>>> from django.contrib.gis.utils import GeoIP
>>> g = GeoIP()
>>> g.country('google.com')
{'country_name': 'United States', 'country_code': 'US'}
>>>>
share|improve this answer

I realize at this point this question is about 4+ years old, but I wanted to add additional details on how users of more current versions of Mac OS X—like Mac OS X 10.7 (Lion), Mac OS X 10.8 (Mountain Lion) and Mac OS X 10.9 (Mavericks)—can install GeoIP on their machines. This has been tested by me up until Mac OS X 10.9.5, but I believe the exact same instructions should work well for Mac OS X 10.10 (Yosemite).

As far as Python/Django integration goes, I am fairly sure that if the GeoIP binary—and related databases—are properly installed systemwide, you don’t have to futz with GEOIP_PATH or related settings. But would be open to hearing from others in the comments if my assumption is wrong.

Prerequisites.

First, one needs to have Xcode installed. And since Xcode—since at least version 4.3 I believe—no longer includes the GNU versions of autoconf, automake and libtool those need to be installed as well. I provide details on how to install autoconf, automake and libtool in this answer. The rest of this answer assumes your system already has that setup.

Installing GeoIP from source.

With the prerequisites covered, you can install MaxMind’s GeoIP as follows.

Set the working directory to your home directory:

cd

Get the source code and decompress it:

curl -O http://www.maxmind.com/download/geoip/api/c/GeoIP-latest.tar.gz
tar -xvzf GeoIP-latest.tar.gz

Go into the uncompressed source code directory:

cd ./GeoIP*

Run libtoolize on the source code:

libtoolize -f

When that is done, run the configure script on the source code:

./configure

Now run make to compile it:

make

Then run make check to check all is good:

make check

Now install it:

sudo make install

Check it’s installed by running the geoiplookup command whose output should be something like this:

Usage: geoiplookup [-h] [-?] [-d custom_dir] [-f custom_file] [-v] [-i] [-l] <ipaddress|hostname>

Now you have the GeoIP source code compiled, but you don’t have any GeoIP databases installed for it to to use/reference. So let’s install them.

Installing GeoIP databases.

So we’re going to get the three (3) major GeoIP databases that are available out there with curl like this. Set the working directory to your home directory:

cd

And now use curl to get the GeoIP.dat.gz, GeoLiteCity.dat.gz and GeoIPASNum.dat.gz databases:

curl -O http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
curl -O http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
curl -O http://geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz

With that done, let’s move them into the proper directory for the GeoIP program to reference them which is /usr/local/share/GeoIP/. If that directory doesn’t exist on your system, you can make it like this:

sudo mkdir -p /usr/local/share/GeoIP/

Now with that done, move the downloaded GeoIP.dat.gz, GeoLiteCity.dat.gz and GeoIPASNum.dat.gz databases into that directory:

sudo mv ~/GeoIP.dat.gz /usr/local/share/GeoIP/
sudo mv ~/GeoLiteCity.dat.gz /usr/local/share/GeoIP/
sudo mv ~/GeoIPASNum.dat.gz /usr/local/share/GeoIP/

And decompress them in place:

sudo gzip -d -f /usr/local/share/GeoIP/GeoIP.dat.gz
sudo gzip -d -f /usr/local/share/GeoIP/GeoLiteCity.dat.gz
sudo gzip -d -f /usr/local/share/GeoIP/GeoIPASNum.dat.gz

We also need to rename GeoLiteCity.dat to GeoIPCity.dat like this:

sudo mv /usr/local/share/GeoIP/GeoLiteCity.dat /usr/local/share/GeoIP/GeoIPCity.dat

Final GeoIP binary testing.

With the GeoIP binary compiled from source and installed as well as having the databases in place, geoiplookup should work as expected. For example, let’s do a GeoIP lookup on Google:

geoiplookup google.com

The output of that command if all went well should be:

GeoIP Country Edition: US, United States
GeoIP City Edition, Rev 1: US, CA, California, Mountain View, 94043, 37.419201, -122.057404, 807, 650
GeoIP ASNum Edition: AS15169 Google Inc.
share|improve this answer

After compiling it should print the library path:

---------------------------------------------------------------------- 
Libraries have been installed in:    /usr/local/lib

Set the path to the dylib file:

GEOIP_LIBRARY_PATH = '/usr/local/lib/libGeoIP.dylib'
GEOIP_PATH = GEOIP_DATA_FILE
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.