簡體   English   中英

找不到無服務器 Python 本地模塊

[英]Serverless Python Local Module Not Found

我希望將我的本地 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'

數據.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-requirements 你可以使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM