33

This is my code:

import base64

with open('/Users/Bob/test.txt') as f:
    encoded = base64.b64encode(f.readlines())
    print(encoded)

I've based it on the base64 documentation. However, when I try running it with Python 2.7.1 and Python 3.2.2 I get the error:

    import base64
  File "/Users/Bob/test.txt'", line 4, in <module>
    encoded = base64.b64encode(f.readlines())
AttributeError: 'module' object has no attribute 'b64encode'

Why can't the b64encode attribute be found?

1 Answer 1

103

You have a script named base64.py which is shadowing the stdlib module. Rename it.

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

7 Comments

Wow, that was super quick! How did you figure out the problem? I did indeed call the file base64.py.
You're not the first to do this.
@IgnacioVazquez-Abrams and as evidenced by the continued upvotes, not the last one to do this either :)
I would also like to note that the "f.readlines()" should be "f.read()" as the encoder needs a string. (Tested on mine and had the issue.)
@Yetti definitely not the last. 7 years later I keep making the same mistakes sometimes
|

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.