Fix AttributeError: ‘bytes’ object has no attribute ‘b64encode’ – Python Tutorial

By | August 23, 2019

In this tutorial, we will introduce how to fix AttributeError: ‘bytes’ object has no attribute ‘b64encode’, you may also can find this error when you are learning how to use base64 encoding in python.

This error likes this:

bytes object has no attribute b64encode

The example code is:

base64 = base64.b64encode(str.encode(encoding='utf-8', errors='strict'))
print(base64)

def urlsafe_b64encode(data):
    data = base64.b64encode(data.encode())

Why this error occur?

Because there is a variable called base64, which is the same as base64 library.

How to fix this error?

Change the variable name base64 to base64_nosafe.

Then this error is fixed.

Leave a Reply

Your email address will not be published. Required fields are marked *