AttributeError: ‘bytes’ object has no attribute ‘encode’

You see an AttributeError: ‘bytes’ object has no attribute ‘encode’ when you use the encode function on a byte object. Let’s understand the problem in the next section.

 

What causes AttributeError: ‘bytes’ object has no attribute ‘encode‘?

The process of converting strings to bytes is called encoding. Let’s understand this with an example.

str="Welocme to pythonhowto"
print(type(str))

Output :

<class 'str'>

When we encode the string object, we get a byte object as shown below.

# Encode the String object
encoded_str = str.encode()
print(type(encoded_str))

Output:

<class 'bytes'>

You see an error when you try to encode the byte object as shown below.

encoded_str.encode()

Output:

Traceback (most recent call last):
  File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\Encoding_error.py", line 7, in <module>
    encoded_str.encode()
AttributeError: 'bytes' object has no attribute 'encode'. Did you mean: 'decode'?

You might think, “What’s the fuss?” We can fix it by removing the encode() function. Isn’t it? But there is a problem.

In Python 2, strings are treated as byte objects. In Python 3, they are treated as Unicode characters. Refer to this Official Python Guide by on Strings for more details.

Meaning, Python 2 strings won’t need the encode function. However, Python 3 strings need the function.

You will see a problem when you need code that works in both Python 2 and Python 3. Also, in cases where you are unsure if the string-like object is a string object or a byte object.

Now that we’ve understood the problem, let’s figure out different ways to fix it.

 

Fix 1: Define a function to check the type of object and then use encode on string objects.

We can define a function to check for the type of object. If it is a string object, then use the encode function.

def convert_to_bytes(my_str):
    if type(my_str) is bytes:
        return my_str
    elif type(my_str) is str or type(my_str) is unicode:
        return my_str.encode()

print(convert_to_bytes("String"))
print(convert_to_bytes(b'Byte'))
print(convert_to_bytes(u'unicode'))

Output:

String
Byte
unicode

NOTE:

  • The function is tested in Python versions 2.7+ and Python versions 3+
  • If you plan to use the code in Python 2.7+ versions, replace print(“…”) to print “…”

 

Fix 2: Use a try-except block to handle the Attribute Error.

We can use the try-except block. We encode the string and check if there is an Attribute Error. If so, we print a message and leave it as it is. Refer to the below example.

my_str=b'Welocme to pythonhowto'

try:
    encoded_str=my_str.encode()
except AttributeError:
    print("The String is of type", type(my_str))
    encoded_str=my_str

print(encoded_str)

Output:

The String is of type <class 'bytes'>
b'Welocme to pythonhowto'

NOTE:

  • This code is tested in Python versions 2.7+ and Python versions 3+
  • If you plan to use the code in Python 2.7+ versions, replace print(“…”) to print “…”

 

Conclusion

In this article, we’ve learned the causes of the AttributeError: ‘bytes’ object has no attribute ‘encode’. Although the error-fix is straightforward, things become complicated when we need code that works in Python 2 and 3.

We can do one of the following:

  • Have a check and then convert only the string objects.
  • Use Try-except blocks to handle the error.

We hope this article has been informative. Thank you for reading.

You can also check, Attribute Error : ‘str’ object has no attribute ‘decode’ 

If you enjoyed reading, share this article.

Anusha Pai is a Software Engineer having a long experience in the IT industry and having a passion to write. She has a keen interest in writing Python Errorfixes, Solutions, and Tutorials.

Leave a Comment