環境
Python 3.8.1
Django 3.0.5
Djangoを2から3にアップグレードしたらタイトルのようなエラーが出た
スタックトレースをみてみると以下の一文が。
.../corsheaders/checks.py", line 8, in
from django.utils import six
どうやらcorsheaders
のcheaks.pyの中でimportしようとしたらそれないよって言われたみたいです。
ひとまず公式ドキュメントを確認してみると、
Removed private Python 2 compatibility APIs¶
While Python 2 support was removed in Django 2.0, some private APIs weren’t removed from Django so that third party apps could continue using them until the Python 2 end-of-life.
Since we expect apps to drop Python 2 compatibility when adding support for Django 3.0, we’re removing these APIs at this time.
- django.test.utils.str_prefix() - Strings don’t have ‘u’ prefixes in Python 3.
- django.test.utils.patch_logger() - Use unittest.TestCase.assertLogs() instead.
- django.utils.lru_cache.lru_cache() - Alias of functools.lru_cache().
- django.utils.decorators.available_attrs() - This function returns functools.WRAPPER_ASSIGNMENTS.
- django.utils.decorators.ContextDecorator - Alias of contextlib.ContextDecorator.
- django.utils._os.abspathu() - Alias of os.path.abspath().
- django.utils._os.upath() and npath() - These functions do nothing on Python 3.
- django.utils.six - Remove usage of this vendored library or switch to six.
- django.utils.encoding.python_2_unicode_compatible() - Alias of six.python_2_unicode_compatible().
- django.utils.functional.curry() - Use functools.partial() or functools.partialmethod. See 5b1c389603a353625ae1603.
- django.utils.safestring.SafeBytes - Unused since Django 2.0.
どうやらPython2に互換性のあるAPIを削除したようです。
代わりとしてしてこれ使ってね!ってことみたいですね。
そこでエラーを出してるcorsheaders
がDjango3.0に対応しているか調べてみると、
Requirements
Python 3.5 to 3.8 supported.
Django 1.11 to 3.0 suppported
あれ・・・?対応してるやん・・・?
なんで???なんで???
まさか、最新版がインストールされてない?
と思い
pip freeze
してみると
........
django-cors-headers==3.0.2
........
あっ・・・
恐る恐る
pip install django-cors-headers==3.2.1
を実行してみると・・・
通りました。
Django2系でずっと作業していたのでアップグレードしたらちゃんと他のパッケージもアップグレードしましょうね、という教訓を得ました。
他にももしかしたら詰まっている人がいるかもしれないと思ったので記事にしました。
私のようにcorsheader
ではない可能性もあるのできちんとエラーを見てから対応すると解決も早いかと思います!