【发布时间】:2018-08-13 07:36:12
【问题描述】:
我已经在我的 Windows 10(64 位)机器上的 Postgresql 10(64 位)中成功设置了 plpyton3u 扩展。但是,当我尝试通过调用 requests 模块发出 http 请求时,我收到属性错误 AttributeError: 'module' object has no attribute 'get' 。这是我正在使用的代码
CREATE OR REPLACE FUNCTION from_url(
-- The URL to download.
IN url text,
-- Should any errors (like HTTP transport errors)
-- throw an exception or simply return default_response
IN should_throw boolean DEFAULT true,
-- The default response if any errors are found.
-- Only used when should_throw is set to true
IN default_response text DEFAULT E''
)
RETURNS text
AS $$
# We will use traceback so we get decent error reporting.
import requests
import traceback
# Either throws an error or returns the defeault response
# depending on the should_throw parameter of the from_url() function
def on_error():
if should_throw:
# plpy.error() throws an exception which stops the current transaction
plpy.error("Error downloading '{0}'\n {1}".format(url, traceback.format_exc()))
else:
return default_response
try:
response = requests.get(url)
return response.data
except:
# Log and re-throw the error or return the default response
return on_error()
$$ LANGUAGE plpython3u VOLATILE;
select from_url(<SOME_DATA_FETCHING_URL>);
其中 SOME_DATA_FETCHING_URL 是提供数据的服务器的 url。当我运行此代码时,它会引发以下错误
错误:plpy.Error:下载 SOME_DATA_FETCHING_URL 时出错 回溯(最近一次通话最后): 文件“”,第 16 行,在 __plpython_procedure_from_url_24640 AttributeError: 'module' 对象没有属性 'get'
CONTEXT: Traceback(最近一次通话最后一次): PL/Python 函数“from_url”,第 19 行,在 返回 on_error() PL/Python 函数“from_url”,第 11 行,on_error plpy.error("下载'{0}'出错\n {1}".format(url, traceback.format_exc())) PL/Python 函数“from_url” SQL状态:XX000
我的 PYTHONPATH 设置为 C:\Python34\;C:\Python34\Scripts;C:\Python34\Lib;
我检查了python命令提示符,我能够导入请求并成功运行get命令。
我是否缺少 PostGRESQL 中的任何设置?这将允许我成功运行此代码?
【问题讨论】:
标签: python postgresql-10 plpython