last day (14 days later) » 

2:59 PM
0
Q: Python/Flask error: "ImportError: cannot import name _compare_digest"

ggundersenWith 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\Docum...

 
@Doobeh, thanks. I tried to executing distribute_setup.py, which I hadn't done—but I had used that page to install easy_install and pip. When I just use pip, I do get another error: C:\Python27\lib\distutils\__init__.py:14: UserWarning: The virtualenv distutils package at %s appears to be in the same location as the system distutils? "The virtualenv distutils package at %s appears to be in the same location as the system distutils?").
 
It looks like you have a local module operator.py and it is masking the standard library module. What does import operator; print operator produce?
 
@Martijn Pieters: >>> import operator >>> print operator <module 'operator' (built-in)> >>> operator.__file__ Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute '__file__'
 
@ggundersen: yet when you run for Flask project you do have such a module masking the built-in. Are you certain your sys.path is the same as when you run your Flask project?
 
@MartijnPieters, I'm not certain. Could you outline how I should check this or point me to a resource?
 
2:59 PM
@ggundersen: add the lines to your run.py and see what turns up then. Or catch the ImportError exception and print sys.modules['operator'] to see if that is still the built-in.
 
@MartijnPieters, it looks like it is built-in in the exception: Traceback (most recent call last): mFile "flask-test.py", line 6, in <module> raise Exception(sys.modules['operator']) Exception: <module 'operator' (built-in)>.
 
Next step to verify: from operator import _compare_digest. If that fails your Python installation is well beyond my help and a re-install would be required.
If it succeeds, you still have a module masking problem.
Or you are running the wrong Python version for the hmac library (less likely but not impossible)
welcome to chat!
see my remarks ^
This is just easier to help you troubleshoot this.
 
Yeah, it is. Thanks again. So I get:
Traceback (most recent call last):
File "flask-test.py", line 1, in <module>
from operator import _compare_digest
ImportError: cannot import name _compare_digest
 
you can use CTRL-K on text to indent, but put just code in a chat post.
right, so your operator is built-in and doesn't have _compare_digest.
Next simple check:
import sys
print(sys.version_info)
(sorry, enter key failure)
 
sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)
 
3:07 PM
hrmz, that should definitely have operator._compare_digest; double checking the Python revision history there.
the method is recent.
Which means your hmac.py module is actually too modern.
 
So I use an older version of Python to run Flask on my machine?
 
You have half the change from that ticket.
I'd (re)-install the latest Python version on the machine.
 
Okay. I'm kind of a newbie to Python. Should I get Python 3 instead? or do you mean just the latest 2.7 version?
 
written this out as an answer; you have a FrankenPython installation.
Your hmac file is from Python 2.7.7 or 2.7.8 but you are running the Python 2.7.6 binary instead. That is causing your breakage.
you could try and see what the binary location is:
import sys
print sys.executable
 
Okay. So this has nothing to do with Flask and something to do with a weird install. I'll remove Python entirely—and clear out that binary file if that's not automatically done—and try to reinstall. I'll respond to / accept your answer in a bit if this works.
 
3:20 PM
I've not worked enough with Windows and Python lately to give you any alternatives here, sorry
 
3:43 PM
Dude! It worked! I'll write more in the comment, but thanks very much for hopping on a chat. Very helpful.
 

last day (14 days later) »