python error "AttributeError: 'module' object has no attribute 'sha1'"

Droid picture Droid · Mar 2, 2014 · Viewed 16.4k times · Source

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'
>>> 

Answer

Viktor M picture Viktor M · Nov 25, 2019

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