AttributeError module 'base64' has no attribute 'b64encode'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The Python "AttributeError module 'base64' has no attribute 'b64encode'" occurs when we have a local file named base64.py and try to import from the base64 module. To solve the error, make sure to rename any local files named base64.py.

attributeerror module base64 has no attribute b64encode

Here is an example of how the error occurs in a file called base64.py.

base64.py
import base64 encoded = base64.b64encode(b'some data here') # ⛔️ AttributeError: module 'base64' has no attribute 'b64encode' print(encoded)

The most likely cause of the error is having a local file named base64.py which shadows the base64 module from the standard library.

Make sure to rename your local file to something other than base64.py to solve the error.

main.py
import base64 encoded = base64.b64encode(b'some data here') print(encoded) # 👉️ b'c29tZSBkYXRhIGhlcmU='

You can access the __file__ property on the imported module to see whether it is shadowed by a local file.

main.py
import base64 print(base64.__file__) # ⛔️ result if shadowed by local file # /home/borislav/Desktop/bobbyhadz_python/base64.py # ✅ result if pulling in correct module # /usr/lib/python3.10/base64.py

A good way to start debugging is to print(dir(your_module)) and see what attributes the imported module has.

Here is what printing the attributes of the base64 module looks like when I have a file base64.py in the same directory.

main.py
import base64 # ⛔️ ['__builtins__', '__cached__', '__doc__', '__file__', # '__loader__', '__name__', '__package__', '__spec__'] print(dir(base64))

If you pass a module object to the dir() function, it returns a list of names of the module's attributes.

If you try to access any attribute that is not in this list, you would get the "AttributeError: module has no attribute".

We can see that the imported base64 module doesn't have a b64encode attribute, which makes it evident that we are shadowing the official base64 module with our local base64.py file.

If you try to import the base64 module in a file called base64.py, you would get a little different error message that means the same thing.

base64.py
import base64 # ⛔️ AttributeError: partially initialized module 'base64' has no attribute 'b64encode' (most likely due to a circular import) encoded = base64.b64encode(b'some data here')

Renaming your file solves the error.

You can use the sys module to print all of the built-in module names.

main.py
import sys # 👇️ print all built-in module names print(sys.builtin_module_names)

Conclusion #

The Python "AttributeError module 'base64' has no attribute 'b64encode'" occurs when we have a local file named base64.py and try to import from the base64 module. To solve the error, make sure to rename any local files named base64.py.

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.