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.
The proper way to import GeoIP on Django 1.4 and up (including the current 1.9 release) is:
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
Solution :
yum install GeoIP-devel -y
Related
I would like to have an interface between Python and sqlite. Both are installed on the machine. I had an old version of Python (2.4.3). So, pysqlite was not included by default. First, I tried to solve this problem by installing pysqlite but I did not succeed in this direction. My second attempt to solve the problem was to install a new version of Python. I do not have the root permissions on the machine. So, I installed it locally. The new version of Python is (2.6.2). As far as I know this version should contain pysqlite by default (and now it is called "sqlite3", not "pysqlite2", as before).
However, if I type:
from sqlite3 import *
I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/verrtex/opt/lib/python2.6/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/home/verrtex/opt/lib/python2.6/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named _sqlite3
It has to be noted, that the above error message is different from those which I get if I type "from blablabla import *":
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named blablabla
So, python see something related with pysqlite but still has some problems. Can anybody help me, pleas, with that issue?
P.S.
I use CentOS release 5.3 (Final).
On Windows, _sqlite3.pyd resides in C:\Python26\DLLs. On *nix, it should be under a path similar to /usr/lib/python2.6/lib-dynload/_sqlite3.so. Chances are that either you are missing that shared library or your PYTHONPATH is set up incorrectly.
Since you said you did not install as a superuser, it's probably a malformed path; you can manually have Python search a path for _sqlite3.so by doing
import sys
sys.path.append("/path/to/my/libs")
but the preferred approach would probably be to change PYTHONPATH in your .bashrc or other login file.
You have a "slite3.py" (actually its equivalent for a package, sqlite3/__init__.py, so import sqlite3 per se is fine, BUT that module in turns tries to import _sqlite3 and fails, so it's not finding _sqlite3.so. It should be in python2.6/lib-dynload under your local Python root, AND ld should be instructed that it has permission to load dynamic libraries from that directory as well (typically by setting appropriate environment variables e.g. in your .bashrc). Do you have that lib-dynload directory? What's in it? What environment variables do you have which contain the string LD (uppercase), i.e. env|grep LD at your shell prompt?
I am trying to import the spherical harmonic toolbox (SHTOOLS) in python. I have the files downloaded and unzipped and am using RedHat.
I added the package's path to my python system path and when I go to import the package, I get this error:
>>import pyshtools
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pyshtools/__init__.py", line 49, in <module>
load_documentation()
File "pyshtools/__init__.py", line 27, in load_documentation
from . import _SHTOOLS
ImportError: cannot import name _SHTOOLS
I can not seem to figure out what the issue is. I checked that the path to this folder was actually added to the system path and it was.
Is this an issue on my end? Or is it possible that I have something downloaded incorrectly? If so, how would I go about fixing this issue?
The SHTOOLS package needs to be built with make first to compile the Fortran libraries. The wiki on Github gives directions on which libraries are required - libblas-dev, liblapack-dev, g++, gfortran, and libfftw3-dev (these are the Ubuntu packages, they may have slightly different names on Redhat). Once these are installed, you need to run make, then sudo make all to install the Fortran and Python components. The Makefile has a lot of good comments in it, I'd recommend reading through it before running make.
I need to install the Python module audiolab for a research project, and while I have managed to install it and get the module to import in the Python shell, it returns an error in calling one of the most basic functions in the module, wavread().
I am using Python2.7.1 mainly, though I did try backtracking and installing audiolab for Python2.6.6, only to find the same error message after importing and calling the wavread() function.
My operating system is Mac OS X 10.5.8 with an intel processor.
This is generally how it goes:
import numpy
import scipy
import scikits.audiolab as audio
x, fs, nbits = audio.wavread('test.wav')
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
x, fs, nbits = audio.wavread('test.wav')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scikits.audiolab-0.11.0-py2.7-macosx-10.3- fat.egg/scikits/audiolab/pysndfile/matapi.py", line 91, in basic_reader
hdl = Sndfile(filename, 'r')
File "_sndfile.pyx", line 488, in scikits.audiolab.pysndfile._sndfile.Sndfile.__init__ (scikits/audiolab/pysndfile/_sndfile.c:4251)
IOError: error while opening test.wav
->error while opening file test.wav
-> System error : No such file or directory.
I installed the libsndfile library (which I believe audiofile is simply a wrapper around) using pip and that seemed to install fine without error. I then tried to install audiolab from the tar.gz file, using the command python setup.py install in Terminal. This is as directed in the documentation, found here under the header 'build'
The documentation also talks about in some cases needing to make a 'site.cfg' file, which from what I saw after some google searching, only really applies to Windows users, though I'm not certain about this.
Any light you could shed on my problem would be much appreciated!!
The audiolab documentation is somewhat lacking, but wavread() requires an input file. In your code, that input is test.wav. If it doesn't exist in the working directory (usually the location of your python script), you need to provide the full path to the file.
I looked through the other posts and bug reports and couldn't figure out what's causing this. I'm using Jython 2.5.1, in a Java project in Eclipse (Ubuntu 8.10). It has been added to the project as a standalone .jar file (I just replaced the old Jython 2.1 jar with this one).
I'm running a script that uses the threading.py class. At some point the statement "import os" is evaluated from linecache.py and I get this error, which I can't seem to figure out how to fix:
'Execution failed. Traceback (most recent call last):
File "<string>", line 1, in <module>
File "../lib/python/threading.py", line 6, in <module>
import traceback
File "../lib/python/traceback.py", line 3, in <module>
import linecache
File "../lib/python/linecache.py", line 9, in <module>
import os
ImportError: No module named os'
What do you mean with "the jar that comes with the 2.5 download"? Did you extract the contents and use jython.jar or did you run the installer? If you just extracted and didn't run the installer your jython.jar will miss the whole LIB folder.
Can you check if jython.jar contains a LIB folder? (e.g. open jython.jar with 7z or WinZip).
Or try copying the LIB folder in the same folder where jython.jar resides.
Did you try setting these properties. Jython Registry. e.g. via -Dpython.home in the eclipse run configuration.
python.cachedir
python.path
python.home
How is the jar named? If similar to jython-complete.jar try renaming it to jython.jar
Something is wrong at a very deep level, but it's probably easy to fix. You are seeing an error that happens while trying to report some other error.
Probably you have your PYTHONPATH misconfigured. I don't know the details of Jython or Eclipse running Jython, but it looks like you have no standard library available to you.
If you are getting maven, using the dependency jython-standalone instead of jython may help (at least it did for me in a maven project with jython-standalone-2.5.3)
I would like to have an interface between Python and sqlite. Both are installed on the machine. I had an old version of Python (2.4.3). So, pysqlite was not included by default. First, I tried to solve this problem by installing pysqlite but I did not succeed in this direction. My second attempt to solve the problem was to install a new version of Python. I do not have the root permissions on the machine. So, I installed it locally. The new version of Python is (2.6.2). As far as I know this version should contain pysqlite by default (and now it is called "sqlite3", not "pysqlite2", as before).
However, if I type:
from sqlite3 import *
I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/verrtex/opt/lib/python2.6/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/home/verrtex/opt/lib/python2.6/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named _sqlite3
It has to be noted, that the above error message is different from those which I get if I type "from blablabla import *":
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named blablabla
So, python see something related with pysqlite but still has some problems. Can anybody help me, pleas, with that issue?
P.S.
I use CentOS release 5.3 (Final).
On Windows, _sqlite3.pyd resides in C:\Python26\DLLs. On *nix, it should be under a path similar to /usr/lib/python2.6/lib-dynload/_sqlite3.so. Chances are that either you are missing that shared library or your PYTHONPATH is set up incorrectly.
Since you said you did not install as a superuser, it's probably a malformed path; you can manually have Python search a path for _sqlite3.so by doing
import sys
sys.path.append("/path/to/my/libs")
but the preferred approach would probably be to change PYTHONPATH in your .bashrc or other login file.
You have a "slite3.py" (actually its equivalent for a package, sqlite3/__init__.py, so import sqlite3 per se is fine, BUT that module in turns tries to import _sqlite3 and fails, so it's not finding _sqlite3.so. It should be in python2.6/lib-dynload under your local Python root, AND ld should be instructed that it has permission to load dynamic libraries from that directory as well (typically by setting appropriate environment variables e.g. in your .bashrc). Do you have that lib-dynload directory? What's in it? What environment variables do you have which contain the string LD (uppercase), i.e. env|grep LD at your shell prompt?