Django:ImportError:无法导入名称 _compare_digest

4 投票
4 回答
5912 浏览
提问于 2025-04-18 14:02

我从网站上用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的一个标准库。为什么它不能导入呢?

附注:我在命令行中试过,可以导入operator模块,但在这条语句上出错了: from operator import _compare_digest as compare_digest

4 个回答

0

如果你不想切换到苹果自带的Python,简单地 删除虚拟环境并重新创建 对我来说是个不错的解决办法。

小提示:如果你还没有记录你的包依赖,别忘了先执行 pip freeze > requirements.txt。这样的话,你可以通过 pip install -r requirements.txt 快速恢复你的环境。

0

你很可能在你的 PYTHONPATH 中有另一个文件,叫做 operator.py(可能就在你当前的工作目录里),这个文件会覆盖掉标准库里的 operator 模块。

你可以把这个文件删除或者改个名字。

3

我在使用Anaconda作为默认的Python和Django 1.7时,尝试用startproject命令时遇到了这个错误。于是我删除了虚拟环境(venv),然后重新创建了一个。

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

这样一来,startproject又可以正常工作了。

3

我按照这个StackOverflow上的回答操作: 卸载python.org版本的python2.7,使用默认的OS X python2.7

然后我把我的 .bash_profile 文件里的Python路径改成了 /usr/lib/python,这样就指向了默认的OS X Python路径。

接着,我卸载了Django和MySQL-Python:

sudo pip uninstall django
sudo pip uninstall MySQL-Python 

然后我又重新安装了一遍所有东西,不过这次是先安装 MySQL-Python,再安装Django。

按照这些步骤操作后,一切都正常运行了。

撰写回答