我正在尝试在 python 后端使用库。 我用无服务器框架部署了它。这是我的配置:
无服务器.yml:
plugins:
- serverless-offline
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: non-linux
zip: true
functions:
function1:
handler: handlerPV.function1
events:
- httpApi:
path: /function1
method: post
function2:
handler: handlerPV.function2
events:
- httpApi:
path: /function2
method: post
function3:
handler: handlerPV.function3
events:
- httpApi:
path: /function3
method: post
(我已经更改了函数的名称以在此处显示)
我的.requirements.txt:
cachetools==5.3.2
certificate==2023.11.17
charset-normalizer==3.3.2
dnspython==2.3.0
idna==3.6
numpy==1.21.6
numpy-financial==1.0.0
pymongo==4.6.1
requests==2.31.0
urllib3>=1.26.0,<2.0.0
boto3==1.33.13
我遇到的问题是,当我使用
serverless deploy
命令将其上传到 AWS 时,仅在某些 lambda 函数中出现此错误。这很奇怪,因为有些函数使用这个库并且它可以工作,但在其他函数中,它不起作用..
这是cloudWatch 的错误:
[ERROR] Runtime.ImportModuleError: Unable to import module 'handlerSimulationDatabase': No module named 'cachetools' Traceback (most recent call last):
编辑
我将告诉您我所做的但尚未成功的更改:我尝试将需求上传到 lambda 层,但我的问题也没有取得任何成功。我已经切换到 python 3.8,并且一直使用 pipelinev 而不是 python 的本机 venv。这是新的 serverless.yml。我还尝试删除 lambda 函数并再次创建它们。我仍然感到惊讶,因为在某些函数中使用了cachetools库并且没有错误,但在其他函数中,使用使用cachetools的相同函数会出现错误
service: service-name
frameworkVersion: "3"
provider:
name: aws
runtime: python3.8
stage: staging
region: us-east-1
timeout: 29
memorySize: 1024
httpApi:
cors: true
package:
patterns:
# Folder
- "!node_modules/**"
- "!Lib/**"
- "!build/**"
- "!__pycache__/**"
- "!Include/**"
- "!venv/**"
- "!.serverless/**"
- "!.vscode/**"
- "!preproduction/**"
- "!production/**"
# Files
- "!README.md"
- "!cspell.json"
- "!package-lock.json"
- "!package.json"
plugins:
- serverless-offline
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: non-linux
usePipenv: true
zip: true
我已经解决了。我只是简单评论了python需求插件的自定义配置部分。我不明白错误是什么,因为它只出现在库的某些函数中。
plugins:
- serverless-offline
- serverless-python-requirements
# custom:
# pythonRequirements:
# dockerizePip: non-linux
# zip: true