仅使用 AWS Lambda 加载本地模块时遇到问题 [英] Trouble loading local modules only with AWS Lambda

查看:22
本文介绍了仅使用 AWS Lambda 加载本地模块时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用结构:

<预><代码>.├── 生成文件├── pipfile├── pipfile.lock├── README.md├──模板.yaml├── 测试│ ├── __init__.py│ └── 单位│ └── lambda_application│ ├── test_handler.py│ └── test_parent_child_class.py└── lambda_application├── __init__.py├── first_child_class.py├── lambda_function.py├── second_child_class.py├──需求.txt└── parent_class.py4个目录,14个文件

来自 lambda_function.py 的代码示例:

导入操作系统导入json从 hashlib 导入 sha256导入 boto3从请求导入会话从 .first_child_class 导入 FirstChildClassdef lambda_handler(事件,上下文):# 做一些事情.

按原样,我收到错误消息无法导入模块 'lambda_function'",但是如果我注释掉最后一次导入,from .first_child_class import FirstChildClass",它能够通过该部分并得到错误我还没有加载那个类的模块.

我似乎只在 Lambci/lambda:python3.7 docker 映像中运行它以及在 AWS 上部署时才会收到此错误.我所有的测试都通过了,它可以毫无问题地导入模块.

是否应该在 __init__.py 文件中加载/设置某些内容?

编辑我更改了一些文件的名称以将其发布在这里.

解决方案

您在这里使用了 relative import,它可以在您执行的代码在模块中的情况下工作.但是,由于您的代码不是作为模块执行的,因此您的 AWS Lambda 会失败.

https://stackoverflow.com/a/73149/6391078

在本地快速运行出现以下错误:

Python 3.6

回溯(最近一次调用最后一次):文件lambda_function.py",第 4 行,在 <module> 中从 .first_child_class 导入 FirstChildClassModuleNotFoundError: 没有名为__main__.first_child_class"的模块;'__main__' 不是一个包

您的测试通过,因为您的测试套件从 lambda_application 文件夹中将文件作为 module 导入,该文件夹在测试模块中被视为包

<小时>

这让我朝着正确的方向前进,但并没有完全给我答案,但确实引导我找到了答案,所以我想我会更新我在此处找到的内容.

我没有尝试过,但根据我的发现,我相信:

from first_child_class import FirstChildClass

将是最简单的解决方案.

我最终做的是将类移动到一个子目录中,基本上和上面一样,只是在前面加上了一个包名.

于是,文件结构变为:

<预><代码>.├── 生成文件├── pipfile├── pipfile.lock├── README.md├──模板.yaml├── 测试│ ├── __init__.py│ └── 单位│ └── lambda_application│ ├── test_handler.py│ └── test_parent_child_class.py└── lambda_application├── __init__.py└── 库├── first_child_class.py├── second_child_class.py└── parent_class.py├── lambda_function.py└── 需求.txt

我的导入变成了 from lib.first_child_class import FirstChildClass

app structure:

.
├── Makefile
├── Pipfile
├── Pipfile.lock
├── README.md
├── template.yaml
├── tests
│   ├── __init__.py
│   └── unit
│       └── lambda_application
│           ├── test_handler.py
│           └── test_parent_child_class.py
└── lambda_application
    ├── __init__.py
    ├── first_child_class.py
    ├── lambda_function.py
    ├── second_child_class.py
    ├── requirements.txt
    └── parent_class.py

4 directories, 14 files

Code sample from lambda_function.py:

import os
import json
from hashlib import sha256
import boto3
from requests import Session
from .first_child_class import FirstChildClass


def lambda_handler(event, context):
    # Do some stuff.

As is, I get the error message "Unable to import module 'lambda_function'" but If I comment out the last import, "from .first_child_class import FirstChildClass", it is able to get past that part and get the error that I haven't loaded the module for that class.

I only seem to get this error when I run it in the lambci/lambda:python3.7 docker image and when I deploy on AWS. All my tests pass and it is able to import the module with no problems.

Is there something I should load/setup in the __init__.py file?

EDIT I changed the names of some of the files to post it here.

解决方案

You are using a relative import here which works in case the code you are executing is in a module. However, since your code is being executed not as a module, your AWS Lambda fails.

https://stackoverflow.com/a/73149/6391078

A quick run locally gave the following error:

PYTHON 3.6

Traceback (most recent call last):
  File "lambda_function.py", line 4, in <module>
    from .first_child_class import FirstChildClass
ModuleNotFoundError: No module named '__main__.first_child_class'; '__main__' is not a package

Your tests pass because your testing suite imports the file as a module from the lambda_application folder which gets treated as a package in the testing module


This got me going in the correct direction but didn't quite give me the answer but did lead me to the answer, so I thought I would update what I found here.

I didn't try it but from what I found, I believe that:

from first_child_class import FirstChildClass

would be the simplest resolution.

What I ended up doing was moving the classes into a sub-directory and essentially did the same as above but with a package name prepended.

So, the file structure changed to:

.
├── Makefile
├── Pipfile
├── Pipfile.lock
├── README.md
├── template.yaml
├── tests
│   ├── __init__.py
│   └── unit
│       └── lambda_application
│           ├── test_handler.py
│           └── test_parent_child_class.py
└── lambda_application
    ├── __init__.py
    └── lib
        ├── first_child_class.py
        ├── second_child_class.py
        └── parent_class.py
    ├── lambda_function.py
    └── requirements.txt

and my import became from lib.first_child_class import FirstChildClass

这篇关于仅使用 AWS Lambda 加载本地模块时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆