Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Flask error: "ImportError: cannot import name _compare_digest"

With Windows, I am following this Flask tutorial when I came across the following error:

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

There are SO questions and answers, but they are for OS X/Django. Has anyone see or resolved this issue for PC/Flask before?

like image 429
jds Avatar asked Oct 23 '14 18:10

jds


1 Answers

You appear to have half the changes made for issue 21306 (backporting hmac.compare_digest to 2.7).

Your hmac module has the lines:

from operator import _compare_digest as compare_digest

at the top, but your sys.version_info shows you are running Python 2.7.6; quoting our quick chat session:

Me: Next simple check:

import sys
print(sys.version_info)

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

The hmac version you have is for Python 2.7.7 and up, however!

You'll want to reinstall your Python; download 2.7.8 and reinstall it to make sure you have the correct binary executable for your standard library files.

like image 160
Martijn Pieters Avatar answered Sep 17 '22 17:09

Martijn Pieters