10

i need your help,

How to correct an error AttributeError: 'module' object has no attribute 'sha1',

When I start the command example import random or import hashlib I get such a result

root@thinkad:~# python
Python 2.7.3 (default, Jan  2 2013, 13:56:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/random.py", line 49, in <module>
    import hashlib as _hashlib
  File "hashlib.py", line 3, in <module>
    hasher = hashlib.sha1()
AttributeError: 'module' object has no attribute 'sha1'
>>> import math
>>> import hashlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "hashlib.py", line 3, in <module>
    hasher = hashlib.sha1()
AttributeError: 'module' object has no attribute 'sha1'
>>> 
2
  • 1
    Could you run import hashlib; print dir(hashlib), hashlib.__file__ and include the output in the question. Commented Mar 2, 2014 at 20:43
  • the same AttributeError: 'module' object has no attribute 'sha1' Commented Mar 2, 2014 at 20:49

4 Answers 4

27

The problem appeared after installing some brew cask that did some regular cleanup after. Then node-gyp was failing to rebuild some packages for my node application. Reinstalling python 2 helped me.

On macos:

brew reinstall python@2
Sign up to request clarification or add additional context in comments.

1 Comment

Updating python 2 solved my issue, but it seems python@2 has been removed from homebrew. You can still reinstall it as in this other thread.
3

Cause of the error

When you have a file in the same directory from where you executed the script (or even if it's the script being run itself) named the same as a built-in module, it's loaded instead of the built-in module.

Fix

To fix it you simply need to rename your file hashlib.py to something else and then the Python interpreter will load the built-in module. You may also need to delete the compiled module hashlib.pyc which is located in the same directory as your hashlib.py, otherwise Python will be still loading that module.

Explanation

When you import a module, let's say import hashlib, Python looks for the module hashlib.py in the following locations and in the following order:

  1. directory containing the script being run
  2. built-in modules
  3. directory containing the input script (or the current directory when no file is specified)
  4. PYTHONPATH environment variable (may contain a list of directories)
  5. installation-dependent default path

That means if you execute the script hashlib.py which contains the statement import hashlib, Python imports the script itself instead of built-in module hashlib. In fact, Python compiles your script into the file hashlib.pyc in the same directory and imports that compiled script, so if you just rename hashlib.py and leave haslib.pyc where it is, it will be still loading it. Therefore you also need to delete the haslib.pyc.

Comments

1

It looks like you have a file called hashlib.py that gets in the way of the interpreter finding the standard hashlib module.

4 Comments

yes, bro :D thanks, but How do I? I delete these files?
@PyDroid Why would you delete that file? Doesn't make any sense. It's your source code. Just rename it so it won't collide with the haslib module anymore.
@PyDroid The only file you may need to delete is the compiled module hashlib.pyc in the same directory where you had your hashlib.py file, if it was created.
thanks that helped me i named my file hashlib.py :) oops. Thanks for the help
0

I had the same error in an anaconda environment after an update of a package which also pulled in a new python version. In my case a conda remove python followed by a conda install python=2.7 fixed this.

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.