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.


Solution 1:

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)