Import pytz into AWS lambda function

Scott Decker picture Scott Decker · Jan 20, 2016 · Viewed 13.8k times · Source

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)

Answer

yarick picture yarick · Oct 24, 2018

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?