Fix AttributeError: module ‘base64’ has no attribute ‘b64encode’ – Python Tutorial

By | August 11, 2019

When we are writing an example to illustrate the usage of python base64 library, here is an error: AttributeError: module ‘base64’ has no attribute ‘b64encode’. In this tutorial, we will introduce you how to fix this error.

Look at example code

import base64
s = 'https://www.tutorialexample.com/'

base64_s = base64.b64encode(s.encode())
print(type(s))
print(base64_s)

Then running this python script.

AttributeError module 'base64' has no attribute 'b64encode'

Check the python script, there is no error in it. Why this error occur?

The key is the name of example python source file is base64.py, which is conflict to the python base64 model.

AttributeError module 'base64' has no attribute 'b64encode' problem

To fix this error, we should change the name of our example python script, for example: base64-test.py.

Then run this script again.

The result is:

<class 'str'>
b'aHR0cHM6Ly93d3cudHV0b3JpYWxleGFtcGxlLmNvbS8='

Leave a Reply