← Back to Python, page 5

python-0098 · Hard senior

A team must upgrade a large service from Python 3.8 to 3.13. Plan the migration.

Upgrade in steps: get the test suite green with warnings enabled on the current version, upgrade dependencies to releases that support 3.13, then run CI on both versions until the new one is clean. Removed modules (the 'dead batteries' such as cgi, imghdr and telnetlib went in 3.13) and deprecations such as datetime.utcnow need code changes. Roll out gradually with monitoring.

Full answer
python
import warnings, datetime, sys, importlib.util

with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter("always")
    datetime.datetime.utcnow()
print(sys.version_info[:2], [w.category.__name__ for w in caught])
print(caught[0].message.args[0].split(".")[0])
print({m: importlib.util.find_spec(m) is not None for m in ["cgi", "imghdr", "telnetlib", "tomllib"]})
print(datetime.datetime.now(datetime.UTC).tzinfo)

Output:

text
(3, 13) ['DeprecationWarning']
datetime
{'cgi': False, 'imghdr': False, 'telnetlib': False, 'tomllib': True}
UTC

On 3.13, datetime.utcnow() emits a DeprecationWarning because it returns a naive datetime; the replacement is datetime.now(datetime.UTC), which is timezone-aware. The module check shows that cgi, imghdr and telnetlib no longer exist, while tomllib (added in 3.11) does. A migration plan built on this:

  1. Run tests on 3.8 with -W error::DeprecationWarning to surface problems early.
  2. Inventory dependencies; upgrade or replace those without 3.13 wheels.
  3. Add 3.13 to the CI matrix; fix removed modules, changed defaults and C-extension builds.
  4. Use pyupgrade or ruff --target-version py313 to modernise syntax mechanically.
  5. Deploy to a canary, compare error rates and latency, then roll out and drop the old version from CI.
python
import warnings, datetime, sys, importlib.util

with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter("always")
    datetime.datetime.utcnow()
print(sys.version_info[:2], [w.category.__name__ for w in caught])
print(caught[0].message.args[0].split(".")[0])
print({m: importlib.util.find_spec(m) is not None for m in ["cgi", "imghdr", "telnetlib", "tomllib"]})
print(datetime.datetime.now(datetime.UTC).tzinfo)

Run on Python 3.13.5, twice in fresh interpreters, with identical output.

Key points

  • Enable warnings before upgrading
  • Upgrade dependencies first
  • Run CI on both versions
  • Handle removed modules and deprecations
  • Canary rollout with monitoring

Common mistakes

  • Jumping versions without running tests with warnings on
  • Upgrading Python before checking dependency support

Follow-ups

  • How would you handle a dependency that is abandoned?
  • What changed in asyncio between 3.8 and 3.13?

Tips

  • Name concrete removals