Python 3.12

DeprecationWarning: datetime.datetime.utcnow() is deprecated

DeprecationWarning: datetime.datetime.utcnow() is deprecated

What it means

datetime.utcnow() is deprecated in Python 3.12 because it returns a naive (timezone-unaware) datetime.

Why it happens

Python 3.12 deprecated utcnow() and utcfromtimestamp() to push code toward timezone-aware datetimes. The warning becomes noise in logs and is scheduled for eventual removal.

How to fix it

  1. Replace `datetime.datetime.utcnow()` with `datetime.datetime.now(datetime.timezone.utc)`.
  2. Replace `datetime.datetime.utcfromtimestamp(t)` with `datetime.datetime.fromtimestamp(t, datetime.timezone.utc)`.
  3. If you need a naive value for a legacy API, call `.replace(tzinfo=None)` explicitly after constructing an aware datetime.

Related deadline: Lambda Python 3.9 EOL2027-02-01.

Find every instance in your project

The free EOLkits scanner runs in your browser (nothing uploaded) and flags this and related breakages across your IaC and dependency files.

Primary source: https://docs.python.org/3/whatsnew/3.12.html

Get the full migration audit — $299, hash-anchored PDF →