Importerror geopy for use in Postgresql

518 Views Asked by At

After installing geopy-0.96.2 under Python3.2.5 I get the following: "Importerror: module geopy cannot be found." when running the script below

CREATE OR REPLACE FUNCTION geopy_test() RETURNS text AS $$ 
from geopy.geocoders import GoogleV3
geolocator = GoogleV3()
address, (latitude, longitude) = geolocator.geocode("Kungsgatan 20 Stockholm")
return address
$$ LANGUAGE plpython3u;
SELECT geopy_test()
1

There are 1 best solutions below

0
On

I spent almost a day trying to get geopy geocoding module for Python to play nice with my Postgres 9.3 installation. Here is how I solved it...

Installing Python3.2.5 for Postgresql 9.3 (Windows 8.1)

1) Install Python3.2.5 by downloading python-3.2.5.msi. 2) Go to C:\Python32\DLLs and copy the python3.dll to ....\PostgreSQL\9.3\lib and rename it to python32.dll 3) Open control panel -> Advanced system settings -> Environmental variables Find the PATH variable and if add the paths: C:\Python32;C:\Python32\Scripts 4) Open pgAdminIII and run the SQL query: CREATE EXTENSION plpython3u; 5) The following SQL query can be used to check the install:

CREATE OR REPLACE FUNCTION pyversion() RETURNS text AS $$ 
import sys 
return sys.version + '\n' + '\n'.join(sys.path) 
$$ LANGUAGE plpython3u; 
SELECT pyversion()

This should produce something like:

"3.2.5 (default, May 15 2013, 23:07:10) [MSC v.1500 64 bit (AMD64)]
C:\Python32\lib\site-packages\setuptools-2.0.1-py3.2.egg
C:\WINDOWS\SYSTEM32\python32.zip
C:\Python32\Lib
C:\Python32\DLLs
C:\Program Files\PostgreSQL\9.3\data
C:\Program Files\PostgreSQL\9 (...)"

Installing geopy for Python3.2.5 in Postgresql 9.3 (Windows 8.1)

1) Download geopy-0.96.2.tar.gz from https://pypi.python.org/pypi/geopy
2) Unzip into any directory.
3) Open command prompt and go to this directory
4) run: python setup.py install
 5) Open control panel  -> Advanced system settings -> Environmental variables 
Create new variable
PYTHONPATH = C:\Python32\Lib\site-packages;C:\Python32\Lib\site-package\geopy-0.96.2-py3.2.egg 
6) Open pgAdminIII and run the SQL query:
DROP EXTENSION IF EXISTS plpython3u CASCADE;
CREATE EXTENSION plpython3u;
7) The following SQL query can be used to check the install:

CREATE OR REPLACE FUNCTION pyversion() RETURNS text AS $$ 
import sys 
return sys.version + '\n' + '\n'.join(sys.path) 
$$ LANGUAGE plpython3u; 
SELECT pyversion()

This should now produce something like:

3.2.5 (default, May 15 2013, 23:07:10) [MSC v.1500 64 bit (AMD64)]
C:\Python32\lib\site-packages\setuptools-2.0.1-py3.2.egg
C:\Python32\lib\site-packages\geopy-0.96.2-py3.2.egg
C:\WINDOWS\SYSTEM32\python32.zip
C:\Python32\Lib
C:\Python32\DLLs
C:\Program Files\PostgreSQL\9.3\data
C:\Program Files\PostgreSQL\9.3\bin
C:\Python32
C:\Python32\lib\site-packages

8) You should now be able to test geopy within postgressql

CREATE OR REPLACE FUNCTION geopy_test() RETURNS text AS $$ 
from geopy.geocoders import GoogleV3
geolocator = GoogleV3()
address, (latitude, longitude) = geolocator.geocode("Kungsgatan 20 Stockholm")
return address
$$ LANGUAGE plpython3u;
SELECT geopy_test()

which gave me the following answer:

"Kungsgatan 20, Norrmalm, Stockholm, Sweden"

If you get an Importerror no module found be sure to check that your installation contains the paths above by running: SELECT pyversion()