LoginSignup
2
2

More than 3 years have passed since last update.

ImportError: cannot import name 'six' from 'django.utils'

Posted at

環境

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

django-cors-headers 3.2.1

あれ・・・?対応してるやん・・・?

なんで???なんで???

まさか、最新版がインストールされてない?
と思い
pip freeze
してみると

........
django-cors-headers==3.0.2
........

あっ・・・

恐る恐る

pip install django-cors-headers==3.2.1

を実行してみると・・・

通りました。
Django2系でずっと作業していたのでアップグレードしたらちゃんと他のパッケージもアップグレードしましょうね、という教訓を得ました。

他にももしかしたら詰まっている人がいるかもしれないと思ったので記事にしました。
私のようにcorsheaderではない可能性もあるのできちんとエラーを見てから対応すると解決も早いかと思います!

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2