python encoding_error ai_generated true

LookupError: unknown encoding: utf8

ID: python/lookuperror-unknown-encoding

Also available as: JSON · Markdown
95%Fix Rate
95%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
311 active

Root Cause

Misspelled encoding name. utf8 → utf-8, ascii → ascii, latin1 → latin-1.

generic

Workarounds

  1. 98% success Use the correct encoding name: 'utf-8' (with hyphen), not 'utf8'
    open('file.txt', encoding='utf-8')  # not 'utf8' or 'UTF_8'

    Sources: https://docs.python.org/3/library/codecs.html#standard-encodings

  2. 90% success Check Python's list of standard encodings for the correct name
    Check Python's list of standard encodings for the correct name

    Sources: https://docs.python.org/3/library/codecs.html#standard-encodings

Dead Ends

Common approaches that don't work:

  1. Install a codec package 80% fail

    Standard encodings are built into Python — no package needed

  2. Set PYTHONIOENCODING environment variable 70% fail

    Doesn't fix the encoding name in your code

Error Chain

Frequently confused with: