如何解决找不到无服务器的Python本地模块
我希望将本地python模块文件导入到我的无服务器项目中的处理程序文件中,但是尽管此本地文件与处理程序文件一起位于父目录中,但似乎无法识别该模块。我对python无服务器设置相对较新,并且想知道无服务器文件导入的工作方式是否缺失。
以下是文件:
/ (parent directory)
data.py
gather_keys_oauth2.py
错误消息:
Proxy Handler could not detect JSON: File "/Users/user/.nvm/versions/node/v12.14.0/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/invoke.py",line 72,in <module>
module = import_module(args.handler_path.replace('/','.'))
File "/Users/user/miniconda3/lib/python3.7/importlib/__init__.py",line 127,in import_module
return _bootstrap._gcd_import(name[level:],package,level)
File "<frozen importlib._bootstrap>",line 1006,in _gcd_import
File "<frozen importlib._bootstrap>",line 983,in _find_and_load
File "<frozen importlib._bootstrap>",line 967,in _find_and_load_unlocked
File "<frozen importlib._bootstrap>",line 677,in _load_unlocked
File "<frozen importlib._bootstrap_external>",line 728,in exec_module
File "<frozen importlib._bootstrap>",line 219,in _call_with_frames_removed
File "./data.py",line 4,in <module>
import gather_keys_oauth2 as Oauth2
ModuleNotFoundError: No module named 'gather_keys_oauth2'
data.py:
import json
from datetime import timedelta,datetime
import math
import gather_keys_oauth2 as Oauth2
import fitbit
def auth(event,context):
CLIENT_ID = '*client*'
CLIENT_SECRET = '*secret*'
server = Oauth2.OAuth2Server(CLIENT_ID,CLIENT_SECRET)
server.browser_authorize()
ACCESS_TOKEN = str(server.fitbit.client.session.token['access_token'])
REFRESH_TOKEN = str(server.fitbit.client.session.token['refresh_token'])
auth2_client = fitbit.Fitbit(CLIENT_ID,CLIENT_SECRET,oauth2=True,access_token=ACCESS_TOKEN,refresh_token=REFRESH_TOKEN)
print(auth2_client)
body = {
"message": "Auth","input": event
}
response = {
"statusCode": 200,"body": body['message']
}
return response
解决方法
您的lambda没有包装您要导入的模块。您可以为此使用serverless-python-requirements插件。
它可以本地安装或通过以下方式安装在管道上
sls plugin install -n serverless-python-requirements
您可以将其添加到serverless.yml文件中,然后尝试部署
# this part might not be needed depending on size of utils
custom:
pythonRequirements:
zip: true
# This plugin allows us import dependencies
plugins:
- serverless-python-requirements
在此处查看有关插件的指南
https://www.serverless.com/blog/serverless-python-packaging
https://www.npmjs.com/package/serverless-python-requirementsYou可以使用
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。