Import pytz into AWS lambda function

◇◆丶佛笑我妖孽 提交于 2019-12-10 13:29:00

问题


I'm writing a lambda function that works with datetimes and trying to import pytz so I can have timezone be accounted for when comparing.

import boto3
import pytz
from datetime import timedelta, date, datetime
from boto3.dynamodb.conditions import Key, Attr

causes this to display

{errorMessage=Unable to import module 'lambda_function'}

but when I remove import pytz the function fires (it just doesn't work properly without timezone info)


回答1:


You need to install the pytz package so it's available for your lambda. The way you do this is having pip install it into the directory you are going to zip and upload to AWS (i.e. peered with the file containing your lambda function).

pip install -t path/to/your/lambda pytz

Then when you zip it up and upload it, it will be available.

Editing to add that I created a tool to do a lot of this for you - you can find it here: https://github.com/jimjkelly/lambda-deploy




回答2:


If you don't have access to pytz in your environment, maybe you have access to python-dateutil. In that case you can do:

import datetime
import dateutil.tz

eastern = dateutil.tz.gettz('US/Eastern')
datetime.datetime.now(tz=eastern)

REF. How to get current time in Pacific Timezone when import pytz fails?



来源:https://stackoverflow.com/questions/34906589/import-pytz-into-aws-lambda-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!