Tutorial Example

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

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:

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.