Python Cannot Import Timezone but Can Import Datetime

if I write

from datetime import timezone

I get the error ImportError: cannot import name timezone

Of course calling datetime.timezone does not work either.

How do I debug this? I have wasted an hour and it is already late here.

6

1 Answer

datetime.timezone was added in Python 3.2. So it is normal to get an import error in e.g. Python 2.7.

In Python 2.7, you can use the pytz library.

import datetime
import pytz

myDate = datetime.datetime.now(pytz.utc)
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest