【问题标题】:Django: ImportError: cannot import name _compare_digestDjango:ImportError:无法导入名称_compare_digest
【发布时间】:2014-07-20 17:24:29
【问题描述】:

我已经从网站上安装了带有 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

发现 operator 是一个标准的 python 库。为什么不能导入?

附:我确实在命令行中尝试过,我可以导入操作员模块,但我在此语句中收到错误:from operator import _compare_digest as compare_digest

【问题讨论】:

    标签: python django macos


    【解决方案1】:

    遵循这个 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。

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

    【讨论】:

    • 我认为这是因为virtualenv。所以这样做来恢复virtualenvvirtualenv /path-to/env-folder - 我是从 debian 邮件列表中获得的。
    【解决方案2】:

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

    virtualenv -p /usr/bin/python2.7 venv
    

    startproject 又开始工作了。

    【讨论】:

      【解决方案3】:

      您很可能在您的PYTHONPATH(可能在当前工作目录中)有另一个名为operator.py 的文件,它隐藏了标准库operator 模块..

      删除或重命名文件。

      【讨论】:

      • 我如何检查我是否这样做?这是操作系统的新安装,我还没有创建任何新文件或任何类型的项目。只是第一次运行 Django。
      • @Edvinauskas 从命令行运行:python -c "import operator; print operator.__file__"。这应该会告诉您正在导入哪个文件。
      • /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/operator.so
      • 它位于此处...我应该尝试删除它吗?但是它看起来是否可以作为默认安装目录。
      【解决方案4】:

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

      提示:如果您还没有跟踪您的包裹要求,请不要忘记先pip freeze &gt; requirements.txt。这样您就可以pip install -r requirements.txt 快速启动并再次运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-26
        • 2020-07-07
        • 2014-12-19
        • 1970-01-01
        • 2014-08-28
        • 2017-04-11
        相关资源
        最近更新 更多