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

Why this error/exception?

From django-3.0release notes,

django.utils.six - Remove usage of this vendored library or switch to six.

means, django.utils.six module was removed from django-3.0 onwards.

My codebase isn't using "django.utils.six" module, then why this error?

This import error could be raised because of two reasons,

  1. Most importantly, any of your installed packages are using the django.utils.six module
  2. or maybe your codebase using the django.utils.six module

NOTE: Most of the time the first reason is the villain 😖😖

How can I identify which package is causing the error/exception?

The easy way is, look into your last few lines of error traceback, and it will tell you which package is causing the exceptions.

Examples

Corsheaders

In this example, corsheaders module caused the the import error

  File "/path-to-project/project/venv/lib/python3.7/site-packages/corsheaders/__init__.py", line 1, in 
    from .checks import check_settings  # noqa: F401
  File "/path-to-project/project/venv/lib/python3.7/site-packages/corsheaders/checks.py", line 7, in 
    from django.utils import six

Example-2

In this example, jsonfield module caused the the import error

  File "d:\production\myproject\venv\lib\site-packages\jsonfield\fields.py", line 21, in 
    from .encoder import JSONEncoder
  File "d:\production\myproject\venv\lib\site-packages\jsonfield\encoder.py", line 2, in 
    from django.utils import six, timezone
ImportError: cannot import name 'six' from 'django.utils' (d:\production\myproject\venv\lib\site-packages\django\utils\__init__.py)

Example-3

In this example parler module caused the import error

...
File "/path/to/project/venv/lib/python3.8/site-packages/parler/utils/conf.py", line 10, in 
    from django.utils import six
ImportError: cannot import name 'six' from 'django.utils' (/path/to/project/venv/lib/python3.8/site-packages/django/utils/__init__.py)

Example-4

In this example django_mysql module caused the import error

  File "/home/jerin/.virtualenvs/webscraperio/lib/python3.6/site-packages/django_mysql/checks.py", line 9, in 
    from django_mysql.utils import collapse_spaces
  File "/home/jerin/.virtualenvs/webscraperio/lib/python3.6/site-packages/django_mysql/utils.py", line 17, in 
    from django.utils import six
ImportError: cannot import name 'six'

What is the solution?

If the error raised because of some third-party packages like django-cors-headers,django-jsonfield, etc upgrade the corresponding package versions to latest versions. If you are already using the latest version, report an issue with the developer.

If the error raised because from your codebase, use six package instead of django.utils.six module


The Django 3.0.0 release notes specify that certain private Python 2 compatibility APIs were removed. Among those was django.utils.six.

For this error specifically, @WillemVanOnsem noted that the module corsheaders was referencing this module.

For others encountering this same thing, looking at the file path on the last line of the stack trace can help with identifying the problematic module. Another example of this I've seen is:

...
File "/path/to/project/venv/lib/python3.8/site-packages/parler/utils/conf.py", line 10, in <module>
    from django.utils import six
ImportError: cannot import name 'six' from 'django.utils' (/path/to/project/venv/lib/python3.8/site-packages/django/utils/__init__.py)

The module causing the issue, in this case, was parler. I hope this helps any others who encounter this issue.