【问题标题】:Pl\Python: issues when importing modulePl\Python:导入模块时的问题
【发布时间】: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


    【解决方案1】:

    (以防其他人偶然发现这个问题)我遇到了类似的问题,在我的情况下,这是由于用户“postgres”对包含我的 Python 模块的目录的访问权限不足造成的。我学到的一件事是,要使用 Python 命令提示符检查 PL/Python 问题,始终需要以与 Postgres 服务器相同的用户身份运行它(在我的例子中,'postgres')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      相关资源
      最近更新 更多