"errorMessage":“无法导入模块'lambda_function' [英] "errorMessage": "Unable to import module 'lambda_function'

查看:460
本文介绍了"errorMessage":“无法导入模块'lambda_function'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试我的lambda函数时,出现以下错误消息:

Upon testing my lambda function I get the following error message:

{
  "errorMessage": "Unable to import module 'lambda_function'"
}

我上传了一个.zip,其中仅包含以下依赖关系:requestsboto3PILPillow-4.0.0.dist-info和lambda函数lambda_function.py-这是从AWS复制的示例:

I uploaded a .zip which only includes the following dependancies: requests, boto3, PIL, Pillow-4.0.0.dist-info & the lambda function lambda_function.py - which is a copied example from AWS:

from __future__ import print_function
import boto3
import os
import sys
import uuid
from PIL import Image
import PIL.Image

s3_client = boto3.client('s3')

def resize_image(image_path, resized_path):
    with Image.open(image_path) as image:
        image.thumbnail(tuple(x / 2 for x in image.size))
        image.save(resized_path)

def handler(event, context):
    for record in event['Records']:
        bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key'] 
        download_path = '/tmp/{}{}'.format(uuid.uuid4(), key)
        upload_path = '/tmp/resized-{}'.format(key)

        s3_client.download_file(bucket, key, download_path)
        resize_image(download_path, upload_path)
        s3_client.upload_file(upload_path, '{}resized'.format(bucket), key)

我的处理程序是:lambda_function.lambda_handler

有什么问题吗?

推荐答案

首先:

默认情况下,lambda使用名为 lambda_handler 的特定关键字运行处理程序.您也可以在AWS Lambda控制台中更改处理程序条目.

By default lambda runs the handler with the specific keyword named: lambda_handler you can change the handler entry as well in the AWS Lambda console.

或者您必须更改处理程序的名称:

Or you have to change the name of your handler:

def handler(event, context):
    for record in event['Records']:
    ,,,

def lambda_handler(event,context):  
    '''

您可以在使用某些框架时使用自定义处理程序,并明确提及处理程序名称.

You can use custom handler while working with some framework and explicitly mention the handler name.

例如,在无服务器的情况下:

For example with serverless:

your_action:
    handler: path/lambda_function.handler

第二:

部分问题是您必须检查是否包含要导入的模块.您必须看到详细的错误输出.您的情况的立即响应将是:无法导入lambda_function.

part of the issue is that you have to check for the modules you have been importing are included. You have to see a detailed error output for that. The immediate response for your scenario would be: unable to import lambda_function.

检查错误日志,您可能会看到以下内容: 无法导入模块"lambda_function":没有名为PIL的模块

Checking error log you might see something like this: Unable to import module 'lambda_function': No module named PIL

您还必须在此处签出有关导入PIL

You also must checkout this question here regarding importing PIL

这篇关于"errorMessage":“无法导入模块'lambda_function'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆