Tom Smets
10/13/2025, 12:19 PMMarvin
10/13/2025, 12:19 PMMarvin
10/13/2025, 12:20 PMdatetime.datetime.utcnow() in favor of timezone-aware datetimes.
What to do instead:
- Python 3.11+:
from datetime import datetime, UTC
now_utc = datetime.now(UTC)
- Python 3.10 and earlier:
from datetime import datetime, timezone
now_utc = datetime.now(timezone.utc)
Replacements for other deprecated UTC helpers:
- datetime.utcfromtimestamp(ts) → datetime.fromtimestamp(ts, UTC) (or timezone.utc on older Pythons)
from datetime import datetime, UTC
dt = datetime.fromtimestamp(ts, UTC)
- If you truly have a naive datetime that you know is already UTC and need to mark it as such (avoid unless you’re sure):
from datetime import UTC
aware = naive_dt.replace(tzinfo=UTC)
If the warning is coming from a dependency (e.g., Prefect or another library):
- Upgrade the package to the latest version (many libraries have already addressed this).
- If you must suppress temporarily, do it narrowly so you don’t hide other important deprecations:
import warnings
warnings.filterwarnings(
"ignore",
category=DeprecationWarning,
message=r".*utcnow\(\) is deprecated.*",
module=r"^the_dependency_name(\.|$)",
)
Helpful references:
- Python docs: datetime.utcnow (deprecated)
- Python docs: aware vs. naive datetimes
If this warning is popping up while using Prefect, can you share the stack trace or the exact line that triggers it? We can point to the exact fix or confirm if it’s from a dependency that needs updating.