我有一个基于python3.7的AWS函数,并试图通过AWS层使用模块口授。我的Python代码如下:
import json
import dicttoxml
def lambda_handler(event, context):
xml = dicttoxml.dicttoxml({"name": "Foo"})
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}在我的本地机器上,它运行得非常好,但是Lambda给出了如下错误:
{
"errorMessage": "module 'dicttoxml' has no attribute 'dicttoxml'",
"errorType": "AttributeError",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 4, in lambda_handler\n xml = dicttoxml.dicttoxml({\"name\": \"Ankur\"})\n"
]
}独裁者层的目录结构如下所示:
dicttoxml.zip > python >录音机> dicttoxml.py
我感到困惑,这里有什么问题吗?
发布于 2020-09-24 12:30:58
我用dicttoxml can 创建了自定义层,确认它可以工作。
所使用的技术包括最近在AWS博客中描述的停靠工具
因此,对于这个问题,我将其验证如下:
mylayer。requirements.txt文件,其内容为echo dicttoxml > ./requirements.txtdocker run -v "$PWD":/var/task "lambci/lambda:build-python3.7" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.7/site-packages/; exit"zip -9 -r mylayer.zip python mylayer.zip创建lambda层。不要忘记将Compatible runtimes指定为python3.7。import dicttoxml
def lambda_handler(event, context):
print(dir(dicttoxml))该函数正确执行:
['LOG', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', 'collections', 'convert', 'convert_bool', 'convert_dict', 'convert_kv', 'convert_list', 'convert_none', 'default_item_func', 'dicttoxml', 'escape_xml', 'get_unique_id', 'get_xml_type', 'ids', 'key_is_valid_xml', 'logging', 'long', 'make_attrstring', 'make_id', 'make_valid_xml_name', 'numbers', 'parseString', 'randint', 'set_debug', 'unicode', 'unicode_literals', 'unicode_me', 'version', 'wrap_cdata']https://stackoverflow.com/questions/63987517
复制相似问题