1

I'm trying run this code adapted from this answer.

from datetime import datetime
from pytz import timezone
import pytz

def lambda_handler(event, context):

    date_format='%m/%d/%Y %H:%M:%S %Z'
    date = datetime.now(tz=pytz.utc)
    print('Current date & time is:', date.strftime(date_format))

    date = date.astimezone(timezone('US/Pacific'))

    print('Local date & time is  :', date.strftime(date_format))

I am running on AWS Lambda. I am getting this error:

Response:
{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'pytz'",
  "errorType": "Runtime.ImportModuleError"
}

How can I fix this, using only imports available in AWS Lambda? I want to keep the code very minimalistic so that it can be copy-pasted directly into the AWS Console, without having to add instructions on how to setup packages etc.

1

1 Answer 1

6

If you don't have access to pytz in AWS Lambda, you can use python-dateutil.
In that case you can do:

import datetime
import dateutil

date_format='%m/%d/%Y %H:%M:%S %Z'

IST = dateutil.tz.gettz('Asia/Kolkata')
print('Current date & time is : ',datetime.datetime.now(tz=IST).strftime(date_format))

or

eastern = dateutil.tz.gettz('US/Eastern')
print('Current date & time is : ',datetime.datetime.now(tz=eastern).strftime(date_format))

Here's the preview: preview

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.