AttributeError: 'bytes' object has no attribute 'encode'

avatar

Borislav Hadzhiev

Last updated: May 1, 2022

banner

Check out my new book

AttributeError: 'bytes' object has no attribute 'encode' #

The Python "AttributeError: 'bytes' object has no attribute 'encode'" occurs when we call the encode() method on a bytes object. To solve the error, remove the call to the encode() method as the string is already encoded.

attributeerror bytes object has no attribute encode

Here is an example of how the error occurs.

main.py
my_str = 'hello world' my_bytes = my_str.encode('utf-8') print(type(my_bytes)) # 👉️ <class 'bytes'> # ⛔️ AttributeError: 'bytes' object has no attribute 'encode'. Did you mean: 'decode'? result = my_bytes.encode('utf-8')

The issue in the code sample is that we are trying to encode a bytes object.

Encoding is the process of converting a string to a bytes object and decoding is the process of converting a bytes object to a string.

To solve the error, remove the call to the encode() method.

main.py
my_str = 'hello world' print(type(my_str)) # 👉️ <class 'str'> my_bytes = my_str.encode('utf-8') print(type(my_bytes)) # 👉️ <class 'bytes'>

If you aren't sure where you have a bytes object or a string, use a try/except statement to handle the possible AttributeError.

main.py
my_str = 'hello world' print(type(my_str)) # 👉️ <class 'str'> try: encoded = my_str.encode('utf-8') print(encoded) # 👉️ b'hello world' print(type(encoded)) # 👉️ <class 'bytes'> except AttributeError: pass

We try to encode the value and if it doesn't have an encode() attribute (it's not a string), we handle the AttributeError.

Encoding is the process of converting a string to a bytes object and decoding is the process of converting a bytes object to a string.

In other words, you can use the str.encode() method to go from str to bytes and bytes.decode() to go from bytes to str.

main.py
my_text = 'hello' my_binary_data = my_text.encode('utf-8') print(my_binary_data) # 👉️ b'hello' my_text_again = my_binary_data.decode('utf-8') print(my_text_again) # 👉️ 'hello'

The str.encode() method is the opposite of bytes.decode() and returns a bytes representation of the unicode string, encoded in the requested encoding.

You can also use bytes(s, encoding=...) and str(b, encoding=...).

main.py
my_text = 'hello' my_binary_data = bytes(my_text, encoding='utf-8') print(my_binary_data) # 👉️ b'hello' my_text_again = str(my_binary_data, encoding='utf-8') print(my_text_again) # 👉️ 'hello'

The str class returns a string version of the given object. If an object is not provided, the class returns an empty string.

The syntax for using the bytes class is the same, except that a b prefix is added.

Ever since Python 3, the language uses the concepts of text and binary data instead of unicode strings and 8-bit strings.

All text in Python is unicode, however encoded Unicode is represented as binary data.

You can use the str type to store text and the bytes type to store binary data.

In Python 3 you can no longer use u'...' literals for Unicode text because all strings are now unicode.

However, you must use b'...' literals for binary data.

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.