236

When trying to use any hg Mercurial commands on the console, I keep getting this error. I installed Python using Homebrew and I am running Mac OS Catalina v. 10.15.1.

Any reference would be appreciated. Here is the error I'm getting:

hg commit --amend
ERROR:root:code for hash md5 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type md5
ERROR:root:code for hash sha1 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha1
ERROR:root:code for hash sha224 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha224
ERROR:root:code for hash sha256 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha256
ERROR:root:code for hash sha384 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha384
ERROR:root:code for hash sha512 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha512
Traceback (most recent call last):
  File "/usr/local/bin/hg", line 43, in <module>
    dispatch.run()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 150, in __getattr__
    self._load()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 94, in _load
    _origimport, head, globals, locals, None, level)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 43, in _hgextimport
    return importfunc(name, globals, *args, **kwargs)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/dispatch.py", line 625, in <module>
    class lazyaliasentry(object):
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/dispatch.py", line 636, in lazyaliasentry
    @util.propertycache
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 150, in __getattr__
    self._load()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 94, in _load
    _origimport, head, globals, locals, None, level)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 43, in _hgextimport
    return importfunc(name, globals, *args, **kwargs)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/util.py", line 180, in <module>
    'md5': hashlib.md5,
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 151, in __getattr__
    return getattr(self._module, attr)
AttributeError: 'module' object has no attribute 'md5'

I also tried following the instruction on this issue but none of the solutions seem to work

brew link openssl --force
Warning: Refusing to link macOS-provided software: openssl@1.1
If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
poca
  • 3,630
  • 3
  • 13
  • 19
  • 1
    Your link to issue 1 is missing. – StayOnTarget Dec 12 '19 at 17:18
  • 1
    This sounds like a Python issue: https://stackoverflow.com/questions/20399331/error-importing-hashlib-with-python-2-7-but-not-with-2-6 or https://stackoverflow.com/questions/41798118/errorrootcode-for-hash-md5-was-not-found – StayOnTarget Dec 12 '19 at 17:19
  • 1
    Any solutions for MacOS? I did try these solutions but none of them are working. I keep getting the same error. When I try to link openssl as i pasted in my question I get the error pasted there. I also tried running these 4 commands but nothing happened: echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib" export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include" export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig" – poca Dec 14 '19 at 14:25
  • 1
    Strangely enough I had the same issue with YCM in Macvim and this [answer](https://stackoverflow.com/a/61598827/3383878) solved my problem. – taper Jul 07 '20 at 21:41

15 Answers15

713

Running brew reinstall python@2 didn't work for my existing Python 2.7 virtual environments. Inside them there were still ERROR:root:code for hash sha1 was not found errors.

I encountered this problem after I ran brew upgrade openssl. And here's the fix:

$ ls /usr/local/Cellar/openssl

...which shows

1.0.2t

According to the existing version, run:

$ brew switch openssl 1.0.2t

...which shows

Cleaning /usr/local/Cellar/openssl/1.0.2t
Opt link created for /usr/local/Cellar/openssl/1.0.2t

After that, run the following command in a Python 2.7 virtualenv:

(my-venv) $ python -c "import hashlib;m=hashlib.md5();print(m.hexdigest())"

...which shows

d41d8cd98f00b204e9800998ecf8427e

No more errors.

David Dehghan
  • 22,159
  • 10
  • 107
  • 95
Rockallite
  • 16,437
  • 7
  • 54
  • 48
  • 21
    This helped me on a mac catalina update or maybe the bash -> zsh where tons of things like this were broken. – NathanQ Feb 28 '20 at 16:52
  • 13
    I didn't have 1.0.2q but doing the same with 1.0.2r also worked – Matt Coady Mar 04 '20 at 18:31
  • Thanks, I would never have figured that out. I had this problem after trying to install pipx via homebrew – carpii Mar 05 '20 at 13:37
  • 3
    Yes. This works for me. I have spent days on this and in fear of having to re-image my machine. I'd tried to reinstall python@2 numerous time but still could not fix the problem. This one definitely works. MacOS Mojave 10.14.6. – hb5fa Mar 12 '20 at 20:16
  • 2
    Flawless steps. – Ilya Saunkin Apr 07 '20 at 14:59
  • This helped me too, but my homebrew claimed I had 1.1.1f installed for openssl when trying "brew upgrade openssl". However, switching to the latest 1.0.x version I had allowed me to proceed. – Petter Måhlén Apr 08 '20 at 09:15
  • running brew switch openssl [version] did it for me, didn't need last command – Florent Roques Apr 10 '20 at 21:29
  • 12
    @FlorentRoques that's because you would have had the same version as the example. In my case `ls /usr/local/Cellar/openssl` returned `1.0.2s` so I needed to run `brew switch openssl 1.0.2s` – Jaybeecave Apr 13 '20 at 05:08
  • works had 2 openssl installations in brew which I needed to compile something ellse – gtheys Apr 15 '20 at 04:33
  • 1
    Wow Wow Wow! :-) – Sagar Ranglani Apr 15 '20 at 10:29
  • Thank you! I have both 1.0.2q and 1.0.2r in folder, and did the step for 1.0.2q. It works! – Fileland Apr 24 '20 at 15:38
  • Can you explain a bit what `brew switch` do? I tried your solution and everything works now, but when I run `openssl version` command it still shows I am using LibreSSL 2.6.5. I am confused about how python picks up openssl 1.0.2 instead of LibreSSL in my system @Rockallite – Alex Apr 29 '20 at 22:07
  • You sir, deserve a medal! Clearly laying out the steps here is really helpful. It was `1.0.2s` for me :) – Patrick May 15 '20 at 09:12
  • Thanks! This problem occured in my Zshell on 10.15.4. I checked openssl which is 1.0.2s on my machine and `brew switch openssl 1.0.2s` did the job. – user2401543 May 22 '20 at 11:34
  • Thank you vvvvvery much ! I met this error when i run `gclient sync`. The openssl on my Mac is 1.0.2s. So i run `brew switch openssl 1.0.2s`. – guozqzzu Jul 01 '20 at 05:09
  • I want to give a medal to this guy. Thanks a ton for the help. – Tarique Shamim Jul 03 '20 at 18:37
  • Third time in a few months I am saved by this! :'D – Mahdi Jul 16 '20 at 07:38
  • Uninstalling python, and then clearing the `pyenv` directory seemed to solve it for me. `rm -R ~/.pyenv` – keshav.bahadoor Jul 17 '20 at 18:01
  • 1
    So Now with the installation of zsh and Calalina there is no `/usr/local/Cellar/openssl` what's a 2.7 junkie to do? – Cheruvim Jul 20 '20 at 17:45
  • I have no idea what's going on, but this worked with 1.0.2q! Thanks! – ET Come Back Sep 10 '20 at 02:23
  • 1
    Thanks! I got this error after the macOS update. Catalina 10.15.6 – Sangbeom Han Sep 15 '20 at 10:18
  • Any way to double up-vote this? I saw the same as @quozqzzu; 1.0.2s. Thanks! – davidbitton Sep 18 '20 at 15:35
  • It worked for me, in my case `openssl` version was `1.1.1h`, so command to fix was `brew switch openssl 1.1.1h` – loretoparisi Nov 23 '20 at 15:17
  • 7
    `brew switch` was removed https://github.com/Homebrew/discussions/discussions/339 – Fixee Jan 23 '21 at 01:17
  • Following the reinstall instructions in the answer on this SO post: [link](https://stackoverflow.com/questions/60298514/how-to-reinstall-python2-from-homebrew) , along with reinstall of openssl@1.1 worked to get me past these hashlib errors. – Wayne Jan 31 '21 at 19:21
  • 2
    In the absence of `brew switch`, and a missing `1.0.2t` locally for me, I used `brew install openssl@1.0`, which installed `1.0.2t`. You might need to `brew tap rbenv/tap` to get the recipe. After this I need to add openssl 1.0 to my path. For me, this was `echo 'export PATH="/usr/local/opt/openssl@1.0/bin:$PATH"' >> ~/.zshrc`. – chadlagore May 26 '21 at 22:59
  • It's freaking me out, not working for me on MacOS – Raymond Dec 06 '21 at 16:26
100

Managed to fix this by first unlinking openssl

brew unlink openssl

And then reinstalling python

brew reinstall python@2

I also noticed that when running 'brew doctor' there was a warning related to an openssl folder found in /usr/local/include/node/. I deleted this folder before running the above commands (not sure if related)

poca
  • 3,630
  • 3
  • 13
  • 19
53

The case for me is that when I install dependencies of a django web app, it messes up the environment. When I type cd, it shows the same error.

The problem was the openssl library, it can not find the correct ones.

If you are on Macintosh, you can type

ls /usr/local/Cellar/openssl

to see all the versions,

brew switch openssl 1.0.XXXX

to choose the available openssl version.

Then the error is gone :)

Bingyu Shen
  • 655
  • 5
  • 6
39

Just uninstall python2

$ brew uninstall python@2

If there is any error :

$ brew uninstall --ignore-dependencies python@2

Ravi D
  • 863
  • 8
  • 16
6

My problem was having pyenv installed and pointing to an old python 2 version

Uninstall python@2 (python2 was EOL since Jan 1st, 2020)

$ brew uninstall python@2
Uninstalling /usr/local/Cellar/python@2/2.7.15_1... (4,169 files, 76.0MB)

then

$ pyenv versions
  system
* 2.7.12 (set by /Users/admin/.python-version)
  3.4.5
  3.7.7

$ pyenv local system
$ pyenv global system
user9869932
  • 6,571
  • 3
  • 55
  • 49
3

I had this issue recently (2020, May) with Google's GCP CLI. It also works on the obsolete Python 2.7.

This is a borked Python 2 installation problem. mBrew no longer correctly installs Python 2 gracefully, as no one cares about abondonware.

Conda still installs Python 2 in an environment; I did that.

Charles Merriam
  • 19,908
  • 6
  • 73
  • 83
3

The next solution worked for me on MacOS Catalina:

  1. Unlink openssl
brew unlink openssl
  1. Reinstall python 2.7 (removed from homebrew and can't be installed via brew install python@2)
brew tap-new <user>/homebrew-python2
brew extract python@2 <user>/homebrew-python2
brew reinstall /usr/local/Homebrew/Library/Taps/<user>/homebrew-python2/Formula/python@2.7.17.rbl
sgl0v
  • 1,357
  • 11
  • 13
  • 1
    I did the same but instead step 2) I did `brew remove python@2` :) macOS Catalina ships Python 2.7 built-in and it seemed less issue prone then to rely on that directly. To me python support via brew seemed to always cause some issue somewhere in that past xD – Tchakabam Oct 27 '20 at 20:37
3

For my case, I just reinstall python2 using pyenv

pyenv uninstall 2.7.18
pyenv install 2.7.18

then everything seems to hooks up.

wdanxna
  • 10,699
  • 2
  • 23
  • 24
2

My problem was multiple versions of python installed (python was still aliased to python2 but i wanted pip to use python3). i also have a python3 bin installed on my system

# use pip with python3
$ python3 -m pip install fish

see Dealing with multiple Python versions and PIP? for details

random-forest-cat
  • 33,652
  • 11
  • 120
  • 99
0

When I would import hashlib I would see an error message stating that hash md5 was not found.

I was able to fix this problem by first unlinking openssl: brew unlink openssl

Then I uninstalled python 2.7 using MacPorts: sudo port uninstall python27

Then I installed python 2.7 using MacPorts: sudo port install python27

Now importing hashlib works :)

user1766438
  • 492
  • 4
  • 13
0

On macOS Mojave (10.14.6), this worked for me:

  • Save the file as python@2.rb in your current directory.
  • brew reinstall python@2.rb
  • I was using virtual environments, so I had to link them back to /usr/local/bin/python and /usr/local/bin/pip for python and pip binaries.
0

After installing openssl I got the same problem. This answer didn't help me, but after manual linking of libcrypto.1.1.dylib and libssl.1.1.dylib everything start working. In my case it was:

ln -s /usr/local/opt/openssl/lib/libcrypto.1.1.dylib /usr/local/lib
ln -s /usr/local/opt/openssl/lib/libssl.1.1.dylib /usr/local/lib
discort
  • 678
  • 1
  • 8
  • 26
0

In my case, my environment was using python 2.7.15. When I switched my pyenv version to 2.7.18 it just worked.

Aerodyno
  • 471
  • 3
  • 12
0

This fixed it for me, I use port not brew:

sudo port upgrade openssl

Jonathan
  • 6,741
  • 7
  • 52
  • 69
0

One reason: an older libssl is needed

When this error occurs, Python is not able to import the _hashlib.py module. One reason for this is libssl changing its ABI during version transition. This affects at least Python2.7.

Solution: Download libssl version 1.1.0, compile and install it in a different location. Then tell python during its configure process to use this old libssl library.

For me a non standard prefix like ./configure --prefix=/home/anyuser/python for the python code tree was sufficient (i installed libssl to /usr/local), as the parallel installed old libssl library in a usual place (/usr/local) was automatically found and used during the configuration and compile process.

This is a good example for why having /usr and /usr/local directories in parallel. I always wondered about that. /usr for the standard system stuff, /usr/local for additional parallel things.

After compiling python2.7 there appears a summary in the terminal where it tells you what went wrong during compilaton/linking. There should be no sign of a missed _hashlib anymore.

Torsten
  • 124
  • 9