Django:ImportError:无法导入名称_compare_digest

时间:2014-07-20 17:24:29

标签: python django macos

我已经从网站上安装了PIP和Python 2.7.8的Django 1.6.5。

我运行django-admin.py startproject test123,切换到test123目录,并运行命令python manage.py runserver,然后我明白了:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/management/__init__.py", line 261, in fetch_command
    commands = get_commands()
  File "/Library/Python/2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/management/__init__.py", line 107, in get_commands
    apps = settings.INSTALLED_APPS
  File "/Library/Python/2.7/site-packages/Django-1.6.5-py2.7.egg/django/conf/__init__.py", line 54, in __getattr__
    self._setup(name)
  File "/Library/Python/2.7/site-packages/Django-1.6.5-py2.7.egg/django/conf/__init__.py", line 50, in _setup
    self._configure_logging()
  File "/Library/Python/2.7/site-packages/Django-1.6.5-py2.7.egg/django/conf/__init__.py", line 72, in _configure_logging
    from django.utils.log import DEFAULT_LOGGING
  File "/Library/Python/2.7/site-packages/Django-1.6.5-py2.7.egg/django/utils/log.py", line 7, in <module>
    from django.views.debug import ExceptionReporter, get_exception_reporter_filter
  File "/Library/Python/2.7/site-packages/Django-1.6.5-py2.7.egg/django/views/debug.py", line 10, in <module>
    from django.http import (HttpResponse, HttpResponseServerError,
  File "/Library/Python/2.7/site-packages/Django-1.6.5-py2.7.egg/django/http/__init__.py", line 2, in <module>
    from django.http.request import (HttpRequest, QueryDict, UnreadablePostError,
  File "/Library/Python/2.7/site-packages/Django-1.6.5-py2.7.egg/django/http/request.py", line 11, in <module>
    from django.core import signing
  File "/Library/Python/2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/signing.py", line 45, in <module>
    from django.utils.crypto import constant_time_compare, salted_hmac
  File "/Library/Python/2.7/site-packages/Django-1.6.5-py2.7.egg/django/utils/crypto.py", line 6, in <module>
    import hmac
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hmac.py", line 8, in <module>
    from operator import _compare_digest as compare_digest
ImportError: cannot import name _compare_digest

发现该运算符是标准的python库。为什么不能导入呢?

P.S。我在命令行中尝试过,我可以导入运算符模块,但是我在这个语句中收到错误:from operator import _compare_digest as compare_digest

4 个答案:

答案 0 :(得分:3)

按照这个SO回答: Uninstall python.org version of python2.7 in favor of default OS X python2.7

然后将我的.bash_profile Python路径更改为/usr/lib/python以获取默认的OSX python路径。

卸载了Django和MySQL-Python:

sudo pip uninstall django
sudo pip uninstall MySQL-Python 

然后再次重新安装所有内容,但MySQL-Python是第一个和第二个Django。

完成这些步骤后,一切正常。

答案 1 :(得分:3)

我在尝试使用startproject时将anaconda作为我的默认python和django1.7得到此错误。 我删除了venv并用

重新创建了它
virtualenv -p /usr/bin/python2.7 venv

startproject再次运作。

答案 2 :(得分:0)

您很可能在operator.py上有一个名为PYTHONPATH的文件(可能在当前工作目录中),这会影响标准库operator模块..

删除或重命名文件。

答案 3 :(得分:0)

对于那些不想切换到Apple的python的人来说,deleting the virtualenv and rebuilding it对我来说很好。

提示:如果您尚未跟踪包装要求,请不要忘记pip freeze > requirements.txt。这样您就可以pip install -r requirements.txt快速启动并再次运行。