django错误无法导入名称'RemovedInDjango30Warning'

时间:2019-12-02 23:33:39

标签: django python-3.x pycharm

伙计们对Django来说还很陌生,我刚开始从事个人项目,并决定使用不当的pycharm(我认为它与错误有关,或与错误无关)。

运行python manage.py runserver时出现以下错误。我做了一些谷歌搜索,看起来它是由与Django版本不一致引起的。

我目前拥有Django 3.0,并且在全局和虚拟环境中都进行了检查。

我试图在py charm之外启动一个项目,而我遇到了同样的问题。 idk我需要做些什么才能再次开始使用Django。有人碰到这个吗?是因为pycharm吗?如果可以,该怎么办才能解决此问题?

(venv) aiden@aiden-XPS-15-9570:~/PycharmProjects/NewsAggregator$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/home/aiden/.local/lib/python3.6/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 "/home/aiden/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 121, in get_package_libraries
    module = import_module(entry[1])
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/aiden/.local/lib/python3.6/site-packages/django/contrib/admin/templatetags/admin_static.py", line 5, in <module>
    from django.utils.deprecation import RemovedInDjango30Warning
ImportError: cannot import name 'RemovedInDjango30Warning'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/core/management/base.py", line 395, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/aiden/.local/lib/python3.6/site-packages/django/core/management/base.py", line 382, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/contrib/admin/checks.py", line 76, in check_dependencies
    for engine in engines.all():
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/utils.py", line 90, in all
    return [self[alias] for alias in self]
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/utils.py", line 90, in <listcomp>
    return [self[alias] for alias in self]
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/utils.py", line 81, in __getitem__
    engine = engine_cls(params)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 25, in __init__
    options['libraries'] = self.get_templatetag_libraries(libraries)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 43, in get_templatetag_libraries
    libraries = get_installed_libraries()
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 108, in get_installed_libraries
    for name in get_package_libraries(pkg):
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 125, in get_package_libraries
    "trying to load '%s': %s" % (entry[1], e)
django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'django.contrib.admin.templatetags.admin_static': cannot import name 'RemovedInDjango30Warning'

6 个答案:

答案 0 :(得分:7)

这是由Django版本中的冲突引起的

尝试卸载Django

sudo pip uninstall django

并使用低于3.0的版本重新安装

sudo pip install django==2.2

答案 1 :(得分:1)

从django.utils.deprecation导入RemovedInDjango30Warning

在以下文件中注释此行

python3.6 / site-packages / django / contrib / admin / templatetags / admin_static.py .python3.6 / sitepackages / django / contrib / staticfiles / templatetags / staticfiles.py

答案 2 :(得分:1)

这是由Django版本引起的。您可能已对其进行升级。

如果您不想返回到版本2,请创建一个虚拟环境并执行pip install django == 2.2

答案 3 :(得分:0)

此错误取决于python,pip和django版本。此错误主要是在python2上运行最新的django版本引起的。

安装python3版本

使用

之类的命令

pip3安装Django

python3 manage.py运行服务器

答案 4 :(得分:0)

我遇到了同样的问题。 由于2版本的python或django会出现此问题。 因此,如果您通过虚拟环境(venv)运行项目,请检查其中的python版本。 通过直接输入以下内容:

myproject/venv/lib/pythonx.y

请确保x.y是您在项目中使用的python版本。 或者,如果您不是通过venv运行项目的,请检查项目正在使用并在其上运行的python版本。

python --version

答案 5 :(得分:0)

TL;DR 这似乎来自站点包中损坏的 Django 安装。删除 Django 并重新安装。

对我来说,我正在将现有项目从 Django 2.2.6 升级到 3.1.7。看来 2.2.6 中的文件不知何故仍在徘徊。我必须运行 pip unintall django 两次才能恢复原状,然后 pip install django 才能安装最新版本。