Python / Flask错误:" ImportError:无法导入名称_compare_digest"

时间:2014-10-23 18:40:16

标签: python python-2.7 flask

对于Windows,当我遇到以下错误时,我正在关注this Flask tutorial

C:\Users\Gregory Gundersen\Documents\Research\flask-test>python run.py
Traceback (most recent call last):
  File "run.py", line 2, in <module>
    from app import app
  File "C:\Users\Gregory Gundersen\Documents\Research\flask-test\app\__init__.py
", line 1, in <module>
    from flask import Flask
  File "C:\Python27\lib\site-packages\flask\__init__.py", line 21, in <module>
    from .app import Flask, Request, Response
  File "C:\Python27\lib\site-packages\flask\app.py", line 26, in <module>
    from . import json
  File "C:\Python27\lib\site-packages\flask\json.py", line 25, in <module>
    from itsdangerous import json as _json
  File "C:\Python27\lib\site-packages\itsdangerous.py", line 14, in <module>
    import hmac
  File "C:\Python27\lib\hmac.py", line 8, in <module>
    from operator import _compare_digest as compare_digest
ImportError: cannot import name _compare_digest

SO questions and answers,但它们适用于OS X / Django。有没有人在PC / Flask之前看到或解决过这个问题?

2 个答案:

答案 0 :(得分:13)

您似乎issue 21306 (backporting hmac.compare_digest to 2.7)所做的更改。

您的hmac模块包含以下行:

from operator import _compare_digest as compare_digest

位于顶部,但您的sys.version_info显示您正在运行Python 2.7.6;引用我们的快速聊天会话:

  

我:接下来的简单检查:

import sys
print(sys.version_info)
     

您:sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)

您拥有的hmac版本适用于Python 2.7.7及以上版本!

您需要重新安装Python;下载2.7.8并重新安装它以确保您的标准库文件具有正确的二进制可执行文件。

答案 1 :(得分:4)

当您更新python而不重建virtualenv时,可能会发生这种情况。在这种情况下,只需重新生成你的virtualenv。