我有一个简单的应用程序,其中同时使用了django和django-rest-framework。
经常,当我尝试启动本地服务器(python manage.py runserver
)时,出现以下异常:
Watching for file changes with StatReloader
Exception in thread Thread-1:
Traceback (most recent call last):
File "/project/venv/lib/python3.7/site-packages/django/template/utils.py", line 66, in __getitem__
return self._engines[alias]
KeyError: 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/project/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/project/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/project/venv/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/project/venv/lib/python3.7/site-packages/django/contrib/admin/checks.py", line 80, in check_dependencies
for engine in engines.all():
File "/project/venv/lib/python3.7/site-packages/django/template/utils.py", line 90, in all
return [self[alias] for alias in self]
File "/project/venv/lib/python3.7/site-packages/django/template/utils.py", line 90, in <listcomp>
return [self[alias] for alias in self]
File "/project/venv/lib/python3.7/site-packages/django/template/utils.py", line 81, in __getitem__
engine = engine_cls(params)
File "/project/venv/lib/python3.7/site-packages/django/template/backends/django.py", line 25, in __init__
options['libraries'] = self.get_templatetag_libraries(libraries)
File "/project/venv/lib/python3.7/site-packages/django/template/backends/django.py", line 43, in get_templatetag_libraries
libraries = get_installed_libraries()
File "/project/venv/lib/python3.7/site-packages/django/template/backends/django.py", line 108, in get_installed_libraries
for name in get_package_libraries(pkg):
File "/project/venv/lib/python3.7/site-packages/django/template/backends/django.py", line 121, in get_package_libraries
module = import_module(entry[1])
File "/project/venv/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/project/venv/lib/python3.7/site-packages/rest_framework/templatetags/rest_framework.py", line 15, in <module>
from rest_framework.renderers import HTMLFormRenderer
File "/project/venv/lib/python3.7/site-packages/rest_framework/renderers.py", line 20, in <module>
from django.test.client import encode_multipart
File "/project/venv/lib/python3.7/site-packages/django/test/client.py", line 23, in <module>
from django.test import signals
File "<frozen importlib._bootstrap>", line 980, in _find_and_load
File "<frozen importlib._bootstrap>", line 149, in __enter__
File "<frozen importlib._bootstrap>", line 94, in acquire
_frozen_importlib._DeadlockError: deadlock detected by _ModuleLock('django.test.signals') at 4420467792
Performing system checks...
重试几次后,服务器成功启动。因此,它不是止步不前,但很烦人。
由于我是Django的新手,所以我想知道是否有防止这种错误的方法。
答案 0 :(得分:4)
我遇到了同样的问题,无法可靠地复制它。我尝试删除.pyc文件,但仍然遇到相同的问题。我将“ rest_framework”移到了INSTALLED_APPS列表的末尾,这似乎对我有用。
答案 1 :(得分:2)
该错误是由于Django Rest Framework引起的,您可以通过将其删除并查看错误是否消失来进行检查。 更具体地说,就我而言,只需删除
from rest_framework.urlpatterns import format_suffix_patterns
urls.py中的解决了该问题,并且仍安装了Django Rest Framework。
答案 2 :(得分:0)
我遇到了同样的问题,我还不知道如何重现它以及它为什么会发生。 但这似乎与Django新版本有关。
从this comment开始,似乎是与虚拟环境中的.pyc文件有关的问题。
要解决此问题,这就是我所做的:
使用以下命令删除项目中的所有.pyc文件:find . -regex '*.pyc' -delete
停用虚拟环境并重新激活它。
问题消失了