导入错误:无法导入名称md5。

30

我不太清楚这里发生了什么事情,我需要将我的Flask应用程序部署到弹性Beanstalk上,但某种方式更改了路径,现在无法再运行python application.py。

[dotnet --info]
.NET Core SDK (reflecting any global.json):
 Version:   2.1.701
 Commit:    8cf7278aa1

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.14
 OS Platform: Darwin
 RID:         osx.10.14-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.1.701/

Host (useful for support):
  Version: 2.1.12
  Commit:  ccea2e606d

[brew -v]
Homebrew 2.2.0
Homebrew/homebrew-core (git revision 43ad0; last commit 2019-11-30)

我也已经完成了这些步骤:

LDFLAGS:  -L/usr/local/opt/openssl/lib
CPPFLAGS: -I/usr/local/opt/openssl/include

依然存在这个错误:

File "/Users/ipchelsea/Library/Python/2.7/lib/python/site-packages/urllib3/util/ssl_.py", line 8, in <module>
    from hashlib import md5, sha1, sha256
ImportError: cannot import name md5
当我执行[brew link --force openssl]时,我得到以下输出:
Warning: Refusing to link macOS-provided software: openssl@1.1

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
Traceback (most recent call last):
  File "application.py", line 4, in <module>
    import urllib3
  File "/Users/ipchelsea/Library/Python/2.7/lib/python/site-packages/urllib3/__init__.py", line 8, in <module>
    from .connectionpool import (
  File "/Users/ipchelsea/Library/Python/2.7/lib/python/site-packages/urllib3/connectionpool.py", line 29, in <module>
    from .connection import (
  File "/Users/ipchelsea/Library/Python/2.7/lib/python/site-packages/urllib3/connection.py", line 38, in <module>
    from .util.ssl_ import (
  File "/Users/ipchelsea/Library/Python/2.7/lib/python/site-packages/urllib3/util/__init__.py", line 6, in <module>
    from .ssl_ import (
  File "/Users/ipchelsea/Library/Python/2.7/lib/python/site-packages/urllib3/util/ssl_.py", line 8, in <module>
    from hashlib import md5, sha1, sha256
ImportError: cannot import name md5
4个回答

29

我正在使用macOS,通过 brew unlink openssl && brew reinstall python@2 来解决这个问题。


从评论中得知,python@2 已不再可用。尝试使用以下方式: brew unlink openssl && brew reinstall https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/python@2.rb


5
我不确定 brew reinstall python@2 是否仍然有效,因为我认为 Brew 已经移除了 Python 2。 - oliversm
5
@oliversm 是正确的。Python 2 已不再提供支持。FormulaUnavailableError: 名为 "/usr/local/opt/python@2/.brew/python@2.rb" 的配方不可用。 - Guido

17

我个人使用并推荐pyenv来管理Python版本。起初我不确定如何解决这个问题,试图像这里建议的那样使用brew重新安装。然而,我想起自己曾经使用pyenv安装过,所以我能够通过它来修复错误。

pyenv install -f 2.7.15

-f选项强制安装该版本,即使它已经存在于您的系统中。您需要使用2.7.16或您想要使用的任何版本。

pyenv使用的python-build将使用brew提供的openssl库(如果可用)。


5

我在使用 Python 2.7.14 时出现了这个错误。升级到 2.7.17 后,问题得以解决。


0

我正在使用 macOS,您可以使用 brew uninstall python@2 卸载 Python 2。之后,您可以使用 brew install {username}/python2/python@2.7.17 安装。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接