#Django3.xでrunserverエラー
Djangoを 2.x から 3.x にバージョンアップして新しくプロジェクトを立ち上げたときのことです。
$django-admin startproject mysite
からの
$python manage.py runserver
をしたところ、2.xであればそのままサーバーが起動したところ、以下のようなエラーが出ました。
$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 "C:\Users\xxxxx\anaconda3\envs\myconda\lib\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 "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\template\backends\django.py", line 121, in get_package_libraries
module = import_module(entry[1])
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\contrib\admin\templatetags\admin_static.py", line 5, in <module>
from django.utils.deprecation import RemovedInDjango30Warning
ImportError: cannot import name 'RemovedInDjango30Warning' from 'django.utils.deprecation' (C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\utils\deprecation.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\core\management\base.py", line 392, in check
all_issues = checks.run_checks(
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\contrib\admin\checks.py", line 78, in check_dependencies
for engine in engines.all():
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\template\utils.py", line 90, in all
return [self[alias] for alias in self]
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\template\utils.py", line 90, in <listcomp>
return [self[alias] for alias in self]
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\template\utils.py", line 81, in __getitem__
engine = engine_cls(params)
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\template\backends\django.py", line 25, in __init__
options['libraries'] = self.get_templatetag_libraries(libraries)
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\template\backends\django.py", line 43, in get_templatetag_libraries
libraries = get_installed_libraries()
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\template\backends\django.py", line 108, in get_installed_libraries
for name in get_package_libraries(pkg):
File "C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\template\backends\django.py", line 123, in get_package_libraries
raise InvalidTemplateLibrary(
django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'django.contrib.admin.templatetags.admin_static': cannot import name 'RemovedInDjango30Warning' from 'django.utils.deprecation' (C:\Users\xxxxx\anaconda3\envs\myconda\lib\site-packages\django\utils\deprecation.py)
なんじゃこのエラーは・・・
#ネットで検索してみる
どうやら、django.utils.deprecation から RemovedInDjango30Warning が import できない、というエラーのよう。
英語のサイトで同じような質問をしている人がいたが、その回答が
python3.x/site-packages/django/contrib/admin/templatetags/admin_static.py
python3.x/site-packages/django/contrib/staticfiles/templatetags/staticfiles.py
の
from django.utils.deprecation import RemovedInDjango30Warning
をコメントアウトすればいいよ~
というもので、「その方法は他のが起動しなくなる恐れがあるから危険でしょ」と突っ込まれていて、結局明快な回答がありませんでした。
#deprecation.py の中身を見てみる
自力で解決するしかないかと思い、まず deprecation.py の中身を見るかと思い、開いたところ
.
.
.
class RemovedInNextVersionWarning(DeprecationWarning):
pass
class RemovedInDjango40Warning(PendingDeprecationWarning):
pass
class warn_about_renamed_method:
.
.
.
RemovedInDjango30Warning はないが、RemovedInDjango40Warning はあって、しかも何もせず pass するだけの class が・・・
#試しに中身をいじってみる・・・
ものは試しだと思い、deprecation.py に以下を追加してみました。
class RemovedInDjango30Warning(PendingDeprecationWarning):
pass
すると
$python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
April 10, 2021 - 15:57:05
Django version 3.1.2, using settings 'bestname.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
あっさりとサーバーが立ち上がりました(笑)
#まとめ
自分の記録のために残しましたが、Django3.xにバージョンアップしてもし同じようなエラーが出た方は試してみてください。