[Note] The solution for Python on MacOSX where import hashlib causes an error

Introduction

This article is for those who are simply suffering from the above error. It's a pretty rare case.

problem

import hashlib Then there are times when you die.

result


Python 2.7.16 (default, Mar  4 2019, 09:01:38) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
ERROR:root:code for hash md5 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.16/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.16/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.16/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.16/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.16/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.16/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.16/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.16/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.16/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.16/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.16/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.16/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

There are various causes for this, such as Pyenv and homebrew. First of all, note that Python on MacOS does not go to read python, but to read python2. Since it is specified in Pyenv, the real thing does not come out even if you do which python. The basic python2 is called whether it is aliased with python2. You can find out what it is by using which python2. Why is there Python3 in the first place, but python2 is in pyenv! I'm talking about that.

Well, these are only indirect causes, and I don't know the direct cause of the break.

Solution

Upgrade with brew

Limited to this.

brew upgrade python@2

With this, the environment is quite good for the time being.

It's still under resolution, so I'll let you know as soon as there is a follow-up report.

Recommended Posts

[Note] The solution for Python on MacOSX where import hashlib causes an error
Build an environment for machine learning using Python on MacOSX
Solution for pip install error [Python] [Mac]
Solution for "Import Error: No module named requests"
(Windows) Causes and workarounds for UnicodeEncodeError on Python 3
A special case where pickle causes an error
[Python] Predict the appropriate rent for an apartment
What to do if the print command itself causes an error in Maya python
I tried Python on Mac for the first time.
[Note] Anaconda & VScode has trouble with python import error
I get an Import Error in Python Beautiful Soup
I tried python on heroku for the first time
Information for controlling the motor with Python on RaspberryPi
Electron is the best solution for Python multi-platform development
Notes on writing config files for Python Note: configparser
Solution if the module is installed in Python but you get an error in Jupyter notebook
Solution for MacOS Catalina (10.15.2), python pip causes OpenSSL error in environment updated from old OS
[Python] for statement error
[Note] Import of a file in the parent directory in Python
I got an error when I tried to process luigi in parallel on windows, but the solution
How to easily create an environment where python code runs on Jupyter without polluting the local environment