我花了将近一天的时间试图让 Python 的 geopy 地理编码模块与我的 Postgres 9.3 安装配合得很好。这是我解决它的方法...
为 Postgresql 9.3 (Windows 8.1) 安装 Python3.2.5
1) 通过下载 python-3.2.5.msi 安装 Python3.2.5。2) 到C:\Python32\DLLs 将python3.dll 复制到....\PostgreSQL\9.3\lib 并重命名为python32.dll 3) 打开控制面板-> 高级系统设置-> 环境变量找到PATH 变量,如果添加路径:C:\Python32;C:\Python32\Scripts 4) 打开 pgAdminIII 并运行 SQL 查询:CREATE EXTENSION plpython3u; 5) 以下 SQL 查询可用于检查安装:
CREATE OR REPLACE FUNCTION pyversion() RETURNS text AS $$
import sys
return sys.version + '\n' + '\n'.join(sys.path)
$$ LANGUAGE plpython3u;
SELECT pyversion()
这应该产生类似的东西:
"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 (...)"
在 Postgresql 9.3 (Windows 8.1) 中为 Python3.2.5 安装 geopy
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()
这现在应该产生如下内容:
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) 你现在应该可以在 postgressql 中测试 geopy
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()
这给了我以下答案:
"Kungsgatan 20, Norrmalm, 斯德哥尔摩, 瑞典"
如果您收到 Importerror no module found ,请务必通过运行检查您的安装是否包含上述路径:SELECT pyversion()