【问题标题】:RuntimeWarning: Parent module 'src' not found while handling absolute import - AWS LambdaRuntimeWarning:处理绝对导入时未找到父模块'src' - AWS Lambda
【发布时间】:2018-01-30 05:41:41
【问题描述】:

我在使用简单的 Python Lambda 函数时遇到问题。 Lambda(由 Zapier 调用)函数基本上创建了一个 Confluence 页面并调用另一个 Zapier Webhook。我在我的 S3 中上传了一个 .zip 文件,其中包含所需包的所有文件夹,然后是一个 src 文件夹,其中包含我的 Python 文件,该文件具有我的处理程序函数。

.zip --> src/lambda_function.py 然后我称之为处理函数。

在我的 lambda_function.py 顶部,我有以下内容:

import string
import json
import pyconfluence as pyco
import requests
import os
import time

def create_page(name, content, label):
    data = {}
    data["type"] = "page"
    data["title"] = name
    data["ancestors"] = [{"id": str(12345678)}] #172949526 is the Parent
    data["space"] = {"key": "PK"}
    data["body"] = {"storage": {"value": content, "representation": "storage"}}
    data["metadata"] = {"labels": [{"name": label}]}
    return pyco.api.rest("/", "POST", json.dumps(data))

def lambda_handler(event, context):
    # Page 12345678 that is in fact a representation of the template
    content = pyco.get_page_content(12345678)
    page_title = event['issue_key'] + ": " + event['issue_title']
    content = string.replace(content, "PK-200", event['issue_key'])  
    create_page(page_title, content, "active-product-requirement")
    api_url = "https://acmein.atlassian.net/rest/api/2/issue/"+event['issue_key']+"/remotelink/"

    webhook = 'https://hooks.zapier.com/hooks/catch/123456/8v6fde/'

    requisite_headers = {'Accept': 'application/json',
                         'Content-Type': 'application/json'}
    auth = (os.environ["PYCONFLUENCE_USER"], os.environ["PYCONFLUENCE_TOKEN"])
    result = requests.get(api_url, headers=requisite_headers, auth=auth).json()

    if len(result) > 0:
        confluence_url = result[0]["object"]["url"]
    else:
        confluence_url = "Error getting the page"
    callback = requests.post(webhook, data={'issue_key': event['issue_key'], 'confluence_url': confluence_url })
    return ["Success"]

然后在我的 CloudWatch 日志中出现以下错误:

【问题讨论】:

  • 如果您需要帮助解决代码中的问题,您需要向我们展示代码。你是在让我们猜测你写了什么,然后告诉你应该写什么。

标签: python amazon-web-services aws-lambda


【解决方案1】:

您必须压缩src 文件夹的内容,而不是src 文件夹本身。您的 python 模块 (lambda_function.py) 应该在您的 zip 中的第一级。 见documentation

压缩目录内容,而不是目录。 Zip 文件的内容可用作 Lambda 函数的当前工作目录。

【讨论】:

    猜你喜欢
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 2015-08-21
    • 2019-02-23
    相关资源
    最近更新 更多