我有一个问题,httplib2无法在Python Lambda函数中使用。
我有以下测试用例:
import httplib2
h = httplib2.Http()
def handler(event=None, context=None):
return 'ok'
if __name__ == "__main__":
print handler()
httplib2 0.9.2部署在与测试用例代码相同的目录中。
我在Lambda上遇到以下错误:模块初始化错误:'module'对象没有属性'Http'
测试用例在本地计算机上按预期工作。
如果我注释掉第二行代码,那么它适用于Lambda。
如果我从部署包中省略了lib,导入失败,因此没有使用其他httplib2。
这里有什么想法吗?此错误完全阻止了我的项目。
答案 0 :(得分:-1)
将代码上传到AWS lambda时,请确保在创建压缩文件并上传之前已将依赖项安装到本地目录。通常,'module'对象没有属性'Http',当lambda找不到你的导入时。我建议你在目录中执行以下操作:
pip install httplib2==0.9.2 -t .
如果运行'tree / f'
,您的目录应如下所示Your directory
│ simple_http_get.py
│
└───httplib2
│ cacerts.txt
│ iri2uri.py
│ __init__.py
│
└───__pycache__
iri2uri.cpython-35.pyc
__init__.cpython-35.pyc
对于你的python代码库(使用simple-http-get.py),请尝试以下操作。注意我已经将'handler'更改为lambda_handler。这是代码的默认入口点 - 除非您更改了它。
import httplib2
def lambda_handler(event, content):
""" entity point into lambda function """
# GET request to google
h = httplib2.Http()
(response_headers, content) = h.request("http://www.google.com", "GET")
# response_headers will contain the response code from the GET request
response_code = None
if response_headers.status == 200:
return 'ok'
else:
return 'error'
return response_code
if __name__ == "__main__":
""" entry point for testing function locally """
response_code = lambda_handler(event=None, content=None)
print(response_code)