0
import base64


def base64(text):
    newText = base64.b64encode(text.encode())
    return newText

This is function the main calls with input, gets string and returns the encoded string. I keep on getting the following error-

AttributeError: 'function' object has no attribute 'b64encode'

I have tried string or bytes as parameter but the error stays the same.

1
  • Was that supposed to be import base64 (no space)? Commented Aug 10, 2022 at 20:50

2 Answers 2

1

The issue is that you have imported base64 but then created your own function with the same name base64. That means the references to base64 after your definition of the function (including inside of it) will be to your function and not to the module.

That's what the error is refering to a function base64 which does not have the attribute you are referencing b64encode.

In order to fix this, all you have to do is change the name of your function to something else, for example - my_base64 will work :)

Sign up to request clarification or add additional context in comments.

Comments

1

import base64 imports the module and binds it to the module variable "base64". The following def base64(...): compiles a function object and then rebinds the module variable "base64". By the time you use it, its now a function. The solution is to rename your function, perhaps

def my_encoder(text):
    ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.