LookupError: unknown encoding in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
2 min

banner

# LookupError: unknown encoding in Python

The Python "LookupError: unknown encoding" occurs when we specify an encoding that is not supported.

To solve the error, use the utf-8 encoding or pick any of the other standard encodings that suit your use case, e.g. latin-1 or ascii.

lookuperror unknown encoding

Here is an example of how the error occurs.

main.py
# ⛔️ LookupError: unknown encoding: example with open('example.txt', 'w', encoding='example') as my_file: my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n')

not supported encoding

We specified an encoding that is not in the list of standard encodings which caused the error.

# Use the utf-8 encoding instead

Chances are you meant to use the utf-8 encoding which is capable of encoding over a million valid character code points in Unicode.

main.py
# ✅ Specify 'utf-8' encoding with open('example.txt', 'w', encoding='utf-8') as my_file: my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n')

use utf 8 encoding instead

You can view all of the standard encodings in this table of the official docs.

Some of the common encodings are ascii, latin-1, utf-16 and utf-32.

# Calling the encode() method with an unknown encoding

The error also occurs when you call the encode() method with an unknown encoding.

main.py
# ⛔️ LookupError: unknown encoding: utf100 my_bytes = 'bobbyhadz.com'.encode('utf100')

passing unknown encoding to encode method

The solution is the same, make sure to pass the correct encoding in the call to str.encode(), e.g. utf-8.

main.py
my_bytes = 'bobbyhadz.com'.encode('utf-8') print(my_bytes) # 👉️ b'bobbyhadz.com'

passing correct encoding to str encode

The str.encode() method returns an encoded version of the string as a bytes object. The default encoding is utf-8.

You can view all of the standard encodings in this table of the official docs.

# Set the PYTHONIOENCODING environment variable

If the error persists, try to set the PYTHONIOENCODING environment variable.

shell
# On Linux and macOS export PYTHONIOENCODING=utf-8 # On Windows setx PYTHONIOENCODING=utf-8 setx PYTHONLEGACYWINDOWSSTDIO=utf-8

If the PYTHONIOENCODING environment variable is set before running the interpreter, it overrides the encoding used for stdin and stdout.

If you are on Windows, you also have to set the PYTHONLEGACYWINDOWSSTDIO environment variable.

You can also set the encoding globally by adding the following lines to the top of your file.

main.py
import sys sys.stdin.reconfigure(encoding='utf-8') sys.stdout.reconfigure(encoding='utf-8')

The 3 lines of code override the encoding used for stdin and stdout.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.