ImportError: cannot import name 'six' from 'django.utils'
Recently, I upgraded the version of Django framework from 2.0.6
to 3.0
and suddenly after calling python manage.py shell
command, I got this exception:
ImportError: cannot import name 'six' from 'django.utils' (/path-to-project/project/venv/lib/python3.7/site-packages/django/utils/init.py)
Full trace:
Traceback (most recent call last):
File "manage.py", line 13, in <module>
execute_from_command_line(sys.argv)
File "/path-to-project/project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/path-to-project/project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/path-to-project/project/venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/path-to-project/project/venv/lib/python3.7/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/path-to-project/project/venv/lib/python3.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/path-to-project/project/venv/lib/python3.7/site-packages/corsheaders/__init__.py", line 1, in <module>
from .checks import check_settings # noqa: F401
File "/path-to-project/project/venv/lib/python3.7/site-packages/corsheaders/checks.py", line 7, in <module>
from django.utils import six
Similar Questions:
I read this Question and this django-3.0, release note , but those resources couldn't help me.
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,
- Most importantly, any of your installed packages are using the
django.utils.six
module - 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.
Install this library: django-utils-six
2.0 for Django >= 3.
pip install django-utils-six