<@ULVA73B9P> Is there a fix for warning datetime.d...
# ask-marvin
t
@Marvin Is there a fix for warning datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC). ?
m
thought for 50.8 seconds
Yes—this warning comes from Python deprecating naive UTC helpers like
datetime.datetime.utcnow()
in favor of timezone-aware datetimes. What to do instead: - Python 3.11+:
Copy code
from datetime import datetime, UTC

  now_utc = datetime.now(UTC)
- Python 3.10 and earlier:
Copy code
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)
Copy code
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):
Copy code
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:
Copy code
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.