gpt4 book ai didi

python - 无法导入模块 'lambda_function' : No module named xlrd

转载 作者:行者123 更新时间:2023-12-04 04:19:38 25 4
gpt4 key购买 nike

我正在尝试使用 python 运行 aws lambda 函数

这是我的代码:

import boto3
import xlrd
import re
from PIL import Image

source_bucket = "general"
destination_bucket = "sarit-sh-filtered-images"
# Get the bucket and object key from the Event
# bucket = event['Records'][0]['s3']['bucket']['name']
# key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'])
def remove_nonalfabetic(name):
clean_name = re.sub("[^a-zA-Z]+", "", name)
return clean_name

def get_employees_list(excel_file):
book = xlrd.open_workbook(excel_file)
employees_list = book.sheet_by_name('names').col_values(0,1)
return employees_list

def add_logo(employee_pic,logo):
mimage = Image.open(employee_pic)
limage = Image.open(logo)

# resize logo
wsize = int(min(mimage.size[0], mimage.size[1]) * 0.25)
wpercent = (wsize / float(limage.size[0]))
hsize = int((float(limage.size[1]) * float(wpercent)))

simage = limage.resize((wsize, hsize))
mbox = mimage.getbbox()
sbox = simage.getbbox()

# right bottom corner
box = (mbox[2] - sbox[2], mbox[3] - sbox[3])
mimage.paste(simage, box)
mimage.save(employee_pic)

def lambda_handler(event, context):
s3 = boto3.resource('s3')
bucket = s3.Bucket(source_bucket)
dest_bucket = s3.Bucket(destination_bucket)
employees_list = get_employees_list('employees.xlsx')

for obj in bucket.objects.filter(Prefix=source_bucket+'/',Delimiter='/',Suffix='.png'):
dest_key = obj.key
employees_name = remove_nonalfabetic(obj.name)
print(dest_key , employees_name)
if employees_name in employees_list:
s3.Object(dest_bucket.employees_name, dest_key).copy_from(CopySource = {'Bucket': obj.bucket_name, 'Key': obj.key})

当我尝试测试此功能时出现错误:

Unable to import module 'lambda_function': No module named xlrd

函数正在内联运行,没有文件或 zip

你能帮我解决这个问题吗?谢谢

最佳答案

xlrd 不是 AWS lambda 中的标准库。你需要用 virtualenv 创建一个部署包

https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html

关于python - 无法导入模块 'lambda_function' : No module named xlrd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59702157/

25 4 0