Amazon web services AWS lambda:“;无法导入模块';应用程序';:没有名为';型号'&引用;
我想使用aws sam部署pytorch模型。我的文件夹结构如下所示:Amazon web services AWS lambda:“;无法导入模块';应用程序';:没有名为';型号'&引用;,amazon-web-services,docker,aws-lambda,dockerfile,aws-sam,Amazon Web Services,Docker,Aws Lambda,Dockerfile,Aws Sam,我想使用aws sam部署pytorch模型。我的文件夹结构如下所示: ├── Dockerfile ├── __init__.py ├── app.py ├── models │ ├── common.py │ ├── experimental.py │ └── yolo.py ├── requirements.txt └── utils ├── autoanchor.py ├── datasets.py ├── general.py ├── goo
├── Dockerfile
├── __init__.py
├── app.py
├── models
│ ├── common.py
│ ├── experimental.py
│ └── yolo.py
├── requirements.txt
└── utils
├── autoanchor.py
├── datasets.py
├── general.py
├── google_utils.py
├── metrics.py
├── plots.py
└── torch_utils.py
FROM public.ecr.aws/lambda/python:3.8
COPY app.py requirements.txt ./
ADD models utils ./
RUN python3.8 -m pip install -r requirements.txt -t .
# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]
如您所见,我有几个本地依赖项。我的Dockerfile如下所示:
├── Dockerfile
├── __init__.py
├── app.py
├── models
│ ├── common.py
│ ├── experimental.py
│ └── yolo.py
├── requirements.txt
└── utils
├── autoanchor.py
├── datasets.py
├── general.py
├── google_utils.py
├── metrics.py
├── plots.py
└── torch_utils.py
FROM public.ecr.aws/lambda/python:3.8
COPY app.py requirements.txt ./
ADD models utils ./
RUN python3.8 -m pip install -r requirements.txt -t .
# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]
app.py
中的代码从模型导入模块。不幸的是,这不起作用,并产生以下错误:
"errorMessage": "Unable to import module 'app': No module named 'models'"
我还尝试对目录models
和utils
使用COPY
而不是ADD
,但它会导致相同的错误。如何修复此问题?您缺少的是,如果尝试将文件夹作为包加载,则必须在Python中创建正确的包。否则,您可以在app.py
创建包的步骤:
创建文件夹名称<代码>模型
模型
文件夹中创建\uuuuu init\uuuuuuuuuuuuuuuuuuuupy
文件yolo.py
,experimental.py
,common.py
等\uuuu init\uuuu.py
文件中,如下所示app.py
中使用它,如import models
或from models import*
此代码是否在本地工作?文件夹的Python打包似乎不起作用,因此lambda无法定位
模型
packageNo它在本地也不起作用。但是模型不是python包,而是一个目录。。所以我猜,目录没有正确构建?谢谢你的回答,这确实是明智的。然而,出于某种原因,它并不能解决这个问题。。尽管创建了init文件,但我仍然会遇到相同的错误。您的init文件在本地运行吗?它也不会在本地运行,我会遇到完全相同的错误:/It应该运行我尝试了相同的文件夹和文件结构ok我发现了:我必须执行添加模型和添加utils utils
。结合您的解决方案,它现在可以工作了:)谢谢您的帮助!