ImportError: cannot import name 'UTC' from 'datetime' (tested fix)

ImportError: cannot import name 'UTC' from 'datetime' means the code runs on Python 3.10 or older, and datetime.UTC only arrived in 3.11. We ran the import and the fixes on Python 3.8, 3.11 and 3.13.

1. datetime.UTC only exists in Python 3.11+

import sys
print(sys.version.split()[0])
try:
    from datetime import UTC
    print("ok:", UTC)
except ImportError as e:
    print(f"{type(e).__name__}: {e}")

Output on the official python:3.8-alpine image:

3.8.20
ImportError: cannot import name 'UTC' from 'datetime' (/usr/local/lib/python3.8/datetime.py)

Output on Python 3.13.5 (and the same on python:3.11-alpine):

3.13.5
ok: UTC

datetime.UTC was added in Python 3.11 as an alias for datetime.timezone.utc. On 3.10 and older the name does not exist, so the import fails. Check python3 --version on the machine (or Docker image, CI runner, Lambda runtime) where the error happens; it is often older than your laptop.

2. Fix: timezone.utc works on every Python 3

from datetime import datetime, timezone
print(timezone.utc)
print(datetime(2026, 10, 22, 12, 0, tzinfo=timezone.utc).isoformat())

Output:

UTC
2026-10-22T12:00:00+00:00

It is the same object:

import datetime
print(datetime.UTC is datetime.timezone.utc)

Output:

True

3. Fix for code that must run on old and new Python

try:
    from datetime import UTC
except ImportError:              # Python < 3.11
    from datetime import timezone
    UTC = timezone.utc
from datetime import datetime
print(datetime(2026, 10, 22, 12, 0, tzinfo=UTC).isoformat())

Output:

2026-10-22T12:00:00+00:00

We ran this block unchanged on 3.8 and 3.13; both printed the line above.

4. Related mistake: datetime.UTC on the class

from datetime import datetime
try:
    datetime.now(datetime.UTC)    # UTC lives on the module, not the class
except AttributeError as e:
    print(f"{type(e).__name__}: {e}")
import datetime as dt
print(dt.datetime.now(dt.UTC).tzinfo)

Output:

AttributeError: type object 'datetime.datetime' has no attribute 'UTC'
UTC

With from datetime import datetime, the name datetime is the class, and UTC lives on the module. Use import datetime as dt and dt.UTC, or import UTC directly.

Related: ImportError: cannot import name (all causes).

Frequently asked questions

Why can't I import UTC from datetime?

datetime.UTC was added in Python 3.11. On 3.10 and older it does not exist.

What should I use instead of datetime.UTC?

datetime.timezone.utc. It works on every Python 3 version and is the same object as datetime.UTC on 3.11+.

Why does datetime.UTC raise AttributeError?

After from datetime import datetime, datetime is the class. UTC is on the module: use import datetime as dt; dt.UTC.

Written with AI assistance. Measured on 2026-10-22 with Python 3.13.5 on Debian and the official python:3.8-alpine (3.8.20) and python:3.11-alpine (3.11.17) images: importing UTC, timezone.utc, a try/except fallback, and datetime.UTC on the class. Every block was run verbatim and its output pasted unedited. PlainHeaders is our extension; Python is free.