AWS Lambda:部署后找不到Python包

0 投票
1 回答
22 浏览
提问于 2025-04-12 18:48
我在做什么

我有一个标准的 hello_world 示例代码,这是通过 sam init --runtime <runtime> --name <project-name> 创建的。

我在 requirements.txt 文件里添加了 pytest,并在 app.py 文件里加入了 import pytest

然后我通过 sam build --template-file template.yaml && sam deploy --template-file template.yaml --config-env dev --profile awsprofile 来构建和部署。

问题

当我在 AWS 上运行这个功能时,它在导入 pytest 时失败,提示没有这个包。

这可能是什么问题呢?

备注
  • 本地的 .aws-sam/build 文件夹里有 pytest 包,正如预期的那样。

1 个回答

0
问题:

你通过 sam deploy --template-file ... 来进行部署。当你这样做时,requirements.txt 文件中的包不会被部署。

解决方案:

不要在部署时指定模板。

sam build --template-file template.yaml && sam deploy --config-env dev --profile awsprofile
附注

关于构建步骤:如果你使用 samconfig.toml 文件,知道你可以在里面指定模板文件,比如 template = "template.yaml"

撰写回答